在2D网格上查找节点8个邻居



我需要找到二维数组中任何节点的基数和对角线邻居。取下面的数组。我正在从数组上的[1,1]中搜索。它应该返回周围的0。我可以找到邻居,如下面的一些代码所示,但他们太慢了。

[[0, 0, 0]
 [0, 1, 0]
 [0, 0, 0]]

我可以像这样强行返回周围所有8个节点的坐标。

def ReturnNeighbors(x, y):
    numpy.array([(x-1, y), (x, y-1), (x+1, y), (x, y+1),
               (x-1, y-1), (x+1, y+1), (x-1, y+1), (x+1, y-1)])
def ReturnNeighbors_4(x, y):
    for i in xrange(x - 1, x + 2):
        for j in xrange(y - 1, y + 2):
            if (i, j) != (x, y):
                yield (i, j)

或者通过计算到所有节点的距离并返回具有距离<2,但这些和我找到的其他解决方案都很慢。我学numpy的全部原因是为了速度。我想我需要使用scipy卷积2d或cKDtree,但它们对我来说太复杂了。

在切换到numpy之前,我处理这个问题的方式是,我的数组中充满了作为值的实际节点对象。每个对象都将其x和y坐标存储在网格中,并存储一组相邻对象。就像这样。我不能再使用这种方法了,因为填充一个充满节点的巨大数组需要很长时间。即使是在一个包含10000个节点对象的100x100小贴图中!我计划以后不仅有更大的地图,而且在任何给定的时间都有多个活跃的地图。实际上,由于节点印记,我在尝试创建更大的贴图时内存不足。它适用于小地牢,但不适用于模拟多个地图的世界。

ExampleNode(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.neighbors = set()
        # The neighbors are found and added to the above set later.
node = Example(0, 0)
for neighbor in node.neighbors:
    do thing with each neighbor

由于各种原因,我需要一个节点邻居。使用细胞自动机平滑地图,在战斗中向附近泼洒鲜血,使用广度优先搜索进行寻路等等。这是为一个流氓我正在创造。蛮力方法适用于小型60x60地下城,但现在我正在扩大范围并生成世界地图。它只有200x200,速度慢得令人恼火。

让我们假设输入数组名为A,它包含所有整数,我们只处理其中的1s元素,并尝试获取它们的邻居。这里有一种方法-

# Get offsets for row and column
R_offset,C_offset = np.meshgrid(np.arange(-1,2),np.arange(-1,2))
# Get row and column indices for places where elements are 1s
R_match,C_match = np.nonzero(A==1)
# Store number of matches as it would be frequently used
N = R_match.size
# Get offsetted row, col indices for all matches
R_idx = (R_match[:,None,None] + R_offset).reshape(N,-1)
C_idx = (C_match[:,None,None] + C_offset).reshape(N,-1)
# Based on boundary conditions set invalid ones to zeros
valid_mask = (R_idx>=0) & (C_idx>=0) & (R_idx<A.shape[0]) & (C_idx<A.shape[1])
valid_mask[:,4] = 0 # Set the pivot(self/center) ones to invalid
# Using valid mask, "cut off" elements from each group of 9 elems
cut_idx = valid_mask.sum(1).cumsum()
# Finally form groups
grps_R = np.split(R_idx[valid_mask],cut_idx)[:-1]
grps_C = np.split(C_idx[valid_mask],cut_idx)[:-1]

示例运行和解释如何解释和使用输出-

In [256]: A
Out[256]: 
array([[1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1]])
In [257]: grps_R
Out[257]: [array([1, 0, 1]), array([1, 2, 3, 1, 3, 1, 2, 3]), array([2, 3, 2])]
In [258]: grps_C
Out[258]: [array([0, 1, 1]), array([1, 1, 1, 2, 2, 3, 3, 3]), array([3, 3, 4])]

因此,基于A中的三个1s,我们有三个组。

第一个在左上角-

In [259]: np.column_stack((R_match[0],C_match[0]))  # First point
Out[259]: array([[0, 0]])
In [260]: np.column_stack((grps_R[0],grps_C[0]))    # Its three neighbors
Out[260]: 
array([[1, 0],
       [0, 1],
       [1, 1]])

第二个是在(2,2)-

In [263]: np.column_stack((R_match[1],C_match[1]))  # Second point 
Out[263]: array([[2, 2]])
In [264]: np.column_stack((grps_R[1],grps_C[1]))    # Its eight neighbors
Out[264]: 
array([[1, 1],
       [2, 1],
       [3, 1],
       [1, 2],
       [3, 2],
       [1, 3],
       [2, 3],
       [3, 3]])

第三个是在(4,5)-

In [265]: np.column_stack((R_match[2],C_match[2]))  # Third point 
Out[265]: array([[3, 4]])
In [266]: np.column_stack((grps_R[2],grps_C[2]))    # Its three neighbors
Out[266]: 
array([[2, 3],
       [3, 3],
       [2, 4]])

最新更新