python - 在二进制网格中扩展 block ( dilation )

标签 python algorithm matrix

对于 A* 实现(为“汽车”机器人生成路径),我需要调整我的模型以考虑汽车的“宽度”,从而避开障碍物。

我的一个想法是将所有障碍物扩大汽车的宽度,这样所有太靠近障碍物的单元格也会被标记为障碍物。

我尝试使用两种朴素的算法来做到这一点,但它仍然太慢(尤其是在大网格上),因为它多次经过相同的单元格:

    unreachable = set()
    # I first add all the unreachables to a set to avoid 'propagation'
    for line in self.grid:
        for cell in line:
            if not cell.reachable:
                unreachable.add(cell)

    for cell in unreachable:
        # I set as unreachable all the cell's neighbours in a certain radius
        for nCell in self.neighbours( cell, int(radius/division) ):
            nCell.reachable = False

这里是邻居的定义:

def neighbours(self, cell, radius = 1, unreachables = False):
    neighbours = set()
    for i in xrange(-radius, radius + 1):
        for j in xrange(-radius, radius + 1):
            x = cell.x + j
            y = cell.y + i
            if 0 <= y < self.height and 0 <= x < self.width and (self.grid[y][x].reachable or unreachables )) :
                neighbours.add(self.grid[y][x])

    return neighbours

是否有任何顺序算法(或 O(n.log(n)))可以做同样的事情?

最佳答案

您正在寻找的是所谓的Minkowski sum。 ,如果你的障碍物和汽车是凸的,有一个线性算法来计算它。

关于python - 在二进制网格中扩展 block ( dilation ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919617/

相关文章:

python - 未绘制直方图

Python super() 不适用于 Enum 参数

algorithm - 给定高度时间序列,如何计算 "take off"和 "landing"时间?

python - 有没有办法找出A是否是B的子矩阵?

python - 将 pandas 组保存到单独的 CSV 文件

algorithm - 什么决定了递归关系中的常数?

c# - 每天生成唯一的 6 位数字

c# - 使用 MathNet.Numerics 的矩阵求逆

python - 通过从 Excel 读取数据创建矩阵

python - 如何让opcua在python中更加高效?