【问题标题】:Convert Numpy Array to Monotone Graph (networkx)将 Numpy 数组转换为单调图(networkx)
【发布时间】:2017-04-03 16:47:07
【问题描述】:

我有一个简单的 1 和 0 数组,我想使用 NetworkX 将此数组转换为图形,条件如下:

  • 单调
  • 定向
  • 加权图(通过/不通过区域)
  • 从左下角开始,向右工作

有一个名为from_numpy_matrix的内置函数

this

我们的目标是获取这张图,并表明我可以从矩阵的左下角(想想栅格数据集)到右上角,而无需向后或向下移动。

示例数组:

array = [[0,0,1,0,0],
         [1,0,0,1,0],
         [1,0,1,1,0],
         [0,0,1,1,0]]
myarray = np.array(array)

0 means go area, 1 means blocked.

【问题讨论】:

    标签: python numpy networkx


    【解决方案1】:

    那很有趣。

    from_numpy_matrix 无济于事,因为没有从迷宫到邻接矩阵的简单转换。相反,迭代允许的位置(即“非墙壁”)并检查在允许的方向(向上、向右、对角线向上)是否有允许的位置要容易得多。

    import numpy as np
    import matplotlib.pyplot as plt
    import networkx as nx
    
    def maze_to_graph(is_wall, allowed_steps):
        """
        Arguments:
        ----------
        is_wall       -- 2D boolean array marking the position of walls in the maze
        allowed_steps -- list of allowed steps; e.g. [(0, 1), (1, 1)] signifies that
                         from coming from tile (i, j) only tiles (i, j+1) and (i+1, j+1)
                         are reachable (iff there is no wall)
    
        Returns:
        --------
        g             -- networkx.DiGraph() instance
        pos2idx       -- dict mapping (i, j) position to node idx (for testing if path exists)
        idx2pos       -- dict mapping node idx to (i, j) position (for plotting)
        """
    
        # map array indices to node indices and vice versa
        node_idx = range(np.sum(~is_wall))
        node_pos = zip(*np.where(~is_wall))
        pos2idx = dict(zip(node_pos, node_idx))
    
        # create graph
        g = nx.DiGraph()
        for (i, j) in node_pos:
            for (delta_i, delta_j) in allowed_steps: # try to step in all allowed directions
                if (i+delta_i, j+delta_j) in pos2idx: # i.e. target node also exists
                    g.add_edge(pos2idx[(i,j)], pos2idx[(i+delta_i, j+delta_j)])
    
        idx2pos = dict(zip(node_idx, node_pos))
    
        return g, idx2pos, pos2idx
    
    def test():
        arr = np.array([[0,0,1,0,0],
                        [1,0,0,1,0],
                        [1,0,1,1,0],
                        [0,0,1,1,0]]).astype(np.bool)
    
        steps = [(0, 1),  # right
                 (-1, 0), # up
                 (-1, 1)] # diagonal up-right
    
        g, idx2pos, pos2idx = maze_to_graph(arr, steps)
    
        nx.draw(g, pos=idx2pos, node_size=1200, node_color='w', labels=idx2pos)
    
        start = (3, 0)
        stop = (0, 4)
        print "Has path: ", nx.has_path(g, pos2idx[start], pos2idx[stop])
    
        return
    

    【讨论】:

    • 所以对于第一个输入,我将采用我的 1/0 数组,如果值为 0,则将其设置为 False,而在 1 的位置将其设置为 True? allowed_steps 只是一组可以使用的坐标点吗?所以 {(0,0), (0,1), (1,1)...}?
    • 1) 是的,只需将您的 1 和 0 数组转换为布尔值(就像我在 test() 函数中所做的那样,我使用您提供的示例)。 2) 允许的步数不是允许的坐标点。它们是与原则上允许您忽略任何墙壁的步骤相对应的索引增量。所以 (1, 0) 是您的第一个索引的增量为 1,而第二个索引没有增量(实际上,您在迷宫中向下移动 1 个图块)。
    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2017-06-25
    • 2015-02-15
    相关资源
    最近更新 更多