2017-04-03 3 views
0

モノトーングラフ(networkx)にnumpyの配列を変換し、そして私は、以下の条件でNetworkXを使用してグラフに、この配列を変換したい: 私は1と0の単純な配列を持っている

  • 方向性

    • モノトーン
    • 加重グラフ(行く/ノーゴーエリア)左下隅に
    • 開始し、右

    を作品functiに建てがあります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.

  • 答えて

    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 
    

    enter image description here

    +0

    ので最初の入力のために、私は私の1/0の配列を取るだろうし、値が0の場合は、Falseに設定し、どこそれは1がTrueに設定されて? allowed_stepsだけ使用できる座標のセットのステップですか?だから{(0,0)、(0,1)、(1,1)...}? –

    +0

    1)はい、1と0の配列をブール値に変換するだけです(test()関数のように、ここで指定した例を使用します)。 2)許容されるステップは、許容される座標ポイントではありません。原則として任意の壁を無視することが許されるステップに対応するインデックス増分です。したがって、(1、0)は最初のインデックスの増分であり、1番目のインデックスの増分であり、2番目のインデックスの増分ではありません(事実上、1つのタイルを迷路内に移動します)。 – Paul

    関連する問題