- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个(子)空间,其中填充了 N
线段。这些线段始终是凸多边形的一部分。它可能看起来像这样:
我想做的是开发一种启发式方法来选择用于分割空间的线段。所选线段的支撑线将分割空间。有两个相互矛盾的启发式因素:
一个例子:
蓝线:完美的自由,非常糟糕的平衡。
红线:非常糟糕的自由度,平庸的平衡。
绿线:自由度差,平衡性好。
紫线:良好的自由度,体面的平衡。
在上面的示例中,组合启发式可能会选择紫色线。
现在我可以遍历每个线段并将其与其他所有线段进行比较(查看它与哪些线段相交以及它们在每一侧的平衡程度)。但这需要 O(N^2)
操作。我更喜欢在 O(N log N)
中运行的东西。
关于遍历线段并给它们打分的 O(N log N)
算法有什么想法吗?我的一个想法是对这些段进行三次排序并形成一些象限:
象限中心给出了大多数线段所在的位置。因此,也许可以使用它们在该中心附近找到一条线段,并检查其相对于象限的方向是否正确。不知何故。这会给出一个不错的平衡分数。
对于交叉点,我考虑过为线段创建边界框并将它们分类到树中,从而可能加快交叉点估计速度?
一些额外的提示(很多时候我的输入数据看起来像什么)
感谢您提供任何新想法或见解 - 数据结构或策略的最小提示都会有很大帮助!
最佳答案
我发现了一种启发式算法,它非常适合我的 BSP 树用途,而且分析起来也非常有趣。在下面的代码中,我首先尝试使用 AABB 树来询问“这条线与哪些线段相交”。但即使这样也太慢了,所以最后我只是采用了一个代价高昂的初始 O(N^2)
比较算法,该算法随着 BSP 树的构建而快速加速,使用了一个有点聪明的观察!
posCount
、negCount
、introduced
和saved
。让它也有一个对另一个片段的 partner
引用,以防它被拆分(否则它是 null
)。O(N^2)
算法初始化根节点处的拆分器(即所有拆分器):算法 calcRelationCounts(splitter S)
:O(N^2)
for all splitters s in S
s.posCount = s.negCount = s.introduced = s.saved = 0
for all splitters vs in S
if (s == vs) continue
if vs is fully on the positive side of the plane of s
s.posCount++
else if vs is fully on the negative side of the plane of s
s.negCount++
else if vs intersects the plane of s
s.negCount++, s.posCount++, s.introduced++
else if vs is coplanar with s
s.saved++
算法 evaluate(...)
其中 treeDepth = floor(log2(splitterCountAtThisNode))
:O(1)
evaluate(posCount, negCount, saved, introduced, treeDepth) {
float f;
if (treeDepth >= EVALUATE_X2) {
f = EVALUATE_V2;
} else if (treeDepth >= EVALUATE_X1) {
float r = treeDepth - EVALUATE_X1;
float w = EVALUATE_X2 - EVALUATE_X1;
f = ((w-r) * EVALUATE_V1 + r * EVALUATE_V2) / w;
} else {
f = EVALUATE_V1;
}
float balanceScore = -f * BALANCE_WEIGHT * abs(posCount - negCount);
float freedomScore = (1.0f-f) * (SAVED_WEIGHT * saved - INTRO_WEIGHT * introduced);
return freedomScore + balanceScore;
}
我的优化算法使用了以下魔数(Magic Number):
#define BALANCE_WEIGHT 437
#define INTRO_WEIGHT 750
#define SAVED_WEIGHT 562
#define EVALUATE_X1 3
#define EVALUATE_X2 31
#define EVALUATE_V1 0.0351639f
#define EVALUATE_V2 0.187508f
positives
、negatives
和remnants
:算法distributeSplitters()
:
for all splitters s at this node
s.partner = null
if s == SEL then add s to "remnants"
else
if s is fully on the positive side of SEL
add s to "positives"
else if s is fully on the negative side of SEL
add s to "negatives
else if s intersects SEL
split s into two appropriate segments sp and sn
sp.partner = sn, sn.partner = sp
add sn to "negatives", sp to "positives" and s to "remnants"
else if s coplanar with SEL
add s to "remnants"
// the clever bit
if (positives.size() > negatives.size())
calcRelationCounts(negatives)
updateRelationCounts(positives, negatives, remnants)
else
calcRelationCounts(positives)
updateRelationCounts(negatives, positives, remnants)
add positives and negatives to appropriate child nodes for further processing
我在这里意识到的聪明之处在于,通常,尤其是使用上述启发式方法进行的前几次拆分,会产生非常不平衡的拆分(但非常自由)。问题是你得到 "O(N^2) + O((N-n)^2)"+ ...
当 n
很小的时候,这是可怕的!相反,我意识到不这样做,我们可以硬重新计算最小的拆分,它采用 O(n^2)
这还不错,然后简单地遍历每个位拆分拆分器,减去计数从较小的拆分部分,它只需要 O(Nn)
这比 O(N^2)
好得多!以下是 updateRelationCounts()
的代码:
算法updateRelationCounts()
:
updateRelationCounts(toUpdate, removed, remnants) {
for all splitters s in toUpdate
for all splitters vs in removed, then remnants
if vs has a partner
if the partner intersects s
s.posCount++, s.negCount++, s.introduced++
else if the partner is fully on the positive side of s
s.posCount++
else if the partner is fully on the negative side of s
s.negCount++
else if the partner is coplanar with s
s.saved++
else
if vs intersects s
s.posCount--, s.negCount--, s.introduced--
else if vs is fully on the positive side of s
s.posCount--
else if vs is fully on the negative side of s
s.negCount--
else if vs is coplanar with s
s.saved--
我现在已经仔细测试过了,看起来逻辑是正确的,因为更新正确地修改了 posCount
等,这样它们就和硬的时候一样了-再次计算!
关于algorithm - 快速空间分区启发式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794445/
是一种在 Neo4j 分区之间进行物理分离的方法吗? 这意味着以下查询将转到 node1: Match (a:User:Facebook) 虽然此查询将转到另一个节点(可能托管在 docker 上)
我尝试在我的 SQL 服务器上使用分区函数对我的一个大表进行分区,但我收到一条错误消息 “只能在SQL Server企业版中创建分区功能。只有SQL Server企业版支持分区。” 所以我想知道没有企
在hadoop文件系统中,我有两个文件,分别是X和Y。通常,hadoop制作的文件X和Y的大小为64 MB。是否可以强制hadoop划分两个文件,以便从X的32 MB和Y的32 MB中创建一个64 M
据我了解,如果我们有一个主键,则使用该键对数据进行分区并将其存储在节点中(例如使用随机分区器)。 现在我不确定的是,如果我有多个键(又名复合键),是用于分区数据的键的组合还是它将是第一个主键? 例如,
我正在向我的 SSAS 多维数据集添加分区,我想知道是否有多个分区可以保留在下面?多少太多了,最佳实践限制是 20 还是 200?有没有人可以分享任何真实世界的知识? 最佳答案 这是 another
我有一个包含大约 200 万条记录的大表,我想对其进行分区。 我将 id 列设置为 PRIMARY AUTO_INCRMENT int (并且它必须始终是唯一的)。我有一列“theyear”int(4
我正在做 mysql 列表分区。我的表数据如下 ---------------------------------------- id | unique_token | city | student_
我有一个表,我们每天在其中插入大约 2000 万个条目(没有任何限制的盲插入)。我们有两个外键,其中一个是对包含大约 1000 万个条目的表的引用 ID。 我打算删除此表中超过一个月的所有数据,因为不
我想在一款足球奇幻游戏中尝试使用 MySQL Partitioning,该游戏的用户分布在联赛中,每个联赛都有一个用户可以买卖球员的市场。当很多用户同时玩时,我在这张表中遇到了一些僵局(在撰写本文时大
我是 jQuery 的新手,想知道是否可以获取一些变量并将它们的除法作为 CSS 宽度。到目前为止我在这里: var x = $(".some-container").length; var y =
所以我正在做家庭作业,我需要为分区、斯特林数(第一类和第二类)和第一类的切比雪夫多项式创建递归函数。我的程序应该能够让用户输入一个正整数 n,然后创建名为 Partitions.txt、Stirlin
我在数据框中有一列,其中包含大约 1,4M 行聊天对话,其中每个单元格中的一般格式为 (1): “名称代理 : 对话” 但是,并非列中的所有单元格都采用这种格式。有些单元格只是 (2): “对话” 我
我在尝试隐藏 a 时遇到了一些问题,直到用户单击某个元素为止。 HTML 看起来像: BRAND item 1 item 2 item 3
一.为什么kafka要做分区? 因为当一台机器有可能扛不住(类比:就像redis集群中的redis-cluster一样,一个master抗不住写,那么就多个master去抗写)
我有一些销售数据,我需要发送存储在单独表中的可用槽中的数量。 销售数据示例: id数量112131415369 create table sales (id serial primary key, q
我计划设置多个节点以使用 glusterfs 创建分布式复制卷 我使用主(也是唯一)分区上的目录在两个节点上创建了一个 gluster 复制卷。 gluster volume create vol_d
我正在尝试使用 sum() over (partition by) 但在总和中过滤。我的用例是将每个产品的 12 个月累计到一个月的条目,因此: ITEM MONTH SALES Item
是否可以创建多个 Enumerators出单Enumerator ? 我正在寻找的相当于 List.partition返回 (List[A], List[A]) ,比如 List().partitio
我正在创建一个基于 x86 的非常简单的 Yocto 图像。 我希望/文件系统是只读的,所以我设置了 IMAGE_FEATURES_append = " read-only-rootfs " 在原件的
是否可以使用一次 collect 调用来创建 2 个新列表?如果没有,我该如何使用分区来做到这一点? 最佳答案 collect(在TraversableLike上定义并在所有子类中可用)与集合和Par
我是一名优秀的程序员,十分优秀!