当前位置: 首页>>代码示例>>Python>>正文


Python networkx.astar_path方法代码示例

本文整理汇总了Python中networkx.astar_path方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.astar_path方法的具体用法?Python networkx.astar_path怎么用?Python networkx.astar_path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.astar_path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: path_search

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def path_search(self, origin, destination):
        """
        This function finds the shortest path connecting origin and destination
        using A* search with distance heuristic.
        origin      :   tuple containing x, y co-ordinates of start position
        desitnation :   tuple containing x, y co-ordinates of end position
        return      :   path as list of node ids (as int) of the graph self._graph
        connecting origin and destination
        """
        xo, yo = origin
        xd, yd = destination
        start = self.localise(xo, yo)
        end = self.localise(xd, yd)

        route = nx.astar_path(
            self._graph, source=self._id_map[start['entry']],
            target=self._id_map[end['exit']],
            heuristic=self._distance_heuristic,
            weight='length')

        return route 
开发者ID:praveen-palanisamy,项目名称:macad-gym,代码行数:23,代码来源:global_route_planner.py

示例2: test_random_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_random_graph(self):

        def dist(a, b):
            (x1, y1) = a
            (x2, y2) = b
            return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

        G = nx.Graph()

        points = [(random(), random()) for _ in range(100)]

        # Build a path from points[0] to points[-1] to be sure it exists
        for p1, p2 in zip(points[:-1], points[1:]):
            G.add_edge(p1, p2, weight=dist(p1, p2))

        # Add other random edges
        for _ in range(100):
            p1, p2 = choice(points), choice(points)
            G.add_edge(p1, p2, weight=dist(p1, p2))

        path = nx.astar_path(G, points[0], points[-1], dist)
        assert path == nx.dijkstra_path(G, points[0], points[-1]) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:24,代码来源:test_astar.py

示例3: test_astar_undirected3

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_undirected3(self):
        XG4=nx.Graph()
        XG4.add_edges_from([ [0,1,{'weight':2}],
                             [1,2,{'weight':2}],
                             [2,3,{'weight':1}],
                             [3,4,{'weight':1}],
                             [4,5,{'weight':1}],
                             [5,6,{'weight':1}],
                             [6,7,{'weight':1}],
                             [7,0,{'weight':1}] ])
        assert nx.astar_path(XG4,0,2)==[0, 1, 2]
        assert nx.astar_path_length(XG4,0,2)==4


# >>> MXG4=NX.MultiGraph(XG4)
# >>> MXG4.add_edge(0,1,3)
# >>> NX.dijkstra_path(MXG4,0,2)
# [0, 1, 2] 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:20,代码来源:test_astar.py

示例4: _path_search

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def _path_search(self, origin, destination):
        """
        This function finds the shortest path connecting origin and destination
        using A* search with distance heuristic.
        origin      :   carla.Location object of start position
        destination :   carla.Location object of of end position
        return      :   path as list of node ids (as int) of the graph self._graph
        connecting origin and destination
        """

        start, end = self._localize(origin), self._localize(destination)

        route = nx.astar_path(
            self._graph, source=start[0], target=end[0],
            heuristic=self._distance_heuristic, weight='length')
        route.append(end[1])
        return route 
开发者ID:bitsauce,项目名称:Carla-ppo,代码行数:19,代码来源:global_route_planner.py

示例5: test_random_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_random_graph(self):
        """Tests that the A* shortest path agrees with Dijkstra's
        shortest path for a random graph.

        """

        G = nx.Graph()

        points = [(random(), random()) for _ in range(100)]

        # Build a path from points[0] to points[-1] to be sure it exists
        for p1, p2 in pairwise(points):
            G.add_edge(p1, p2, weight=dist(p1, p2))

        # Add other random edges
        for _ in range(100):
            p1, p2 = choice(points), choice(points)
            G.add_edge(p1, p2, weight=dist(p1, p2))

        path = nx.astar_path(G, points[0], points[-1], dist)
        assert_equal(path, nx.dijkstra_path(G, points[0], points[-1])) 
开发者ID:holzschu,项目名称:Carnets,代码行数:23,代码来源:test_astar.py

示例6: path

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def path(self, src: dict, dst: dict):
        if not self.__is_dst(src, dst):
            return nx.astar_path(self.directed, src, dst) 
开发者ID:leopepe,项目名称:GOApy,代码行数:5,代码来源:Planner.py

示例7: astar_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def astar_path_length(G, source, target, heuristic=None, weight='weight'):
    """Return the length of the shortest path between source and target using
    the A* ("A-star") algorithm.

    Parameters
    ----------
    G : NetworkX graph

    source : node
       Starting node for path

    target : node
       Ending node for path

    heuristic : function
       A function to evaluate the estimate of the distance
       from the a node to the target.  The function takes
       two nodes arguments and must return a number.

    Raises
    ------
    NetworkXNoPath
        If no path exists between source and target.

    See Also
    --------
    astar_path

    """
    path = astar_path(G, source, target, heuristic, weight)
    return sum(G[u][v].get(weight, 1) for u, v in zip(path[:-1], path[1:])) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:33,代码来源:astar.py

示例8: test_astar_directed

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_directed(self):
        assert nx.astar_path(self.XG,'s','v')==['s', 'x', 'u', 'v']
        assert nx.astar_path_length(self.XG,'s','v')==9 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:5,代码来源:test_astar.py

示例9: test_astar_multigraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_multigraph(self):
         G=nx.MultiDiGraph(self.XG)
         assert_raises((TypeError,nx.NetworkXError),
                      nx.astar_path, [G,'s','v'])
         assert_raises((TypeError,nx.NetworkXError),
                      nx.astar_path_length, [G,'s','v']) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:8,代码来源:test_astar.py

示例10: test_astar_undirected

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_undirected(self):
        GG=self.XG.to_undirected()
        # make sure we get lower weight
        # to_undirected might choose either edge with weight 2 or weight 3
        GG['u']['x']['weight']=2
        GG['y']['v']['weight'] = 2
        assert_equal(nx.astar_path(GG,'s','v'),['s', 'x', 'u', 'v'])
        assert_equal(nx.astar_path_length(GG,'s','v'),8) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:10,代码来源:test_astar.py

示例11: test_astar_undirected2

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_undirected2(self):
        XG3=nx.Graph()
        XG3.add_edges_from([ [0,1,{'weight':2}],
                             [1,2,{'weight':12}],
                             [2,3,{'weight':1}],
                             [3,4,{'weight':5}],
                             [4,5,{'weight':1}],
                             [5,0,{'weight':10}] ])
        assert nx.astar_path(XG3,0,3)==[0, 1, 2, 3]
        assert nx.astar_path_length(XG3,0,3)==15 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:12,代码来源:test_astar.py

示例12: test_astar_w1

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_w1(self):
        G=nx.DiGraph()
        G.add_edges_from([('s','u'), ('s','x'), ('u','v'), ('u','x'),
            ('v','y'), ('x','u'), ('x','w'), ('w', 'v'), ('x','y'),
            ('y','s'), ('y','v')])
        assert nx.astar_path(G,'s','v')==['s', 'u', 'v']
        assert nx.astar_path_length(G,'s','v')== 2 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:9,代码来源:test_astar.py

示例13: test_astar_nopath

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_astar_nopath(self):
        p = nx.astar_path(self.XG,'s','moon') 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:4,代码来源:test_astar.py

示例14: test_cycle

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def test_cycle(self):
        C=nx.cycle_graph(7)
        assert nx.astar_path(C,0,3)==[0, 1, 2, 3]
        assert nx.dijkstra_path(C,0,4)==[0, 6, 5, 4] 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:6,代码来源:test_astar.py

示例15: get_shortest_path

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import astar_path [as 别名]
def get_shortest_path(self, pose, goal_pose):
        path = nx.astar_path(self.graph, pose[:3], goal_pose[:3],
                heuristic=lambda nodea, nodeb: (abs(nodea[0] - nodeb[0]) + abs(nodea[1] - nodeb[1])),
                weight='weight')

        # Remove last rotations
        '''
        if not constants.USE_NAVIGATION_AGENT:
            while len(path) > 1:
                if (path[-1][0] == path[-2][0] and
                    path[-1][1] == path[-2][1]):
                    path.pop()
                else:
                    break
        '''

        max_point = 1
        for ii in range(len(path) - 1):
            weight = self.graph[path[ii]][path[ii + 1]]['weight']
            if path[ii][:2] != path[ii + 1][:2]:
                if abs(self.memory[path[ii + 1][1] - self.yMin, path[ii + 1][0] - self.xMin, 0] - weight) > 0.001:
                    print(self.memory[path[ii + 1][1] - self.yMin, path[ii + 1][0] - self.xMin, 0], weight)
                    if constants.USE_NAVIGATION_AGENT:
                        print('nxgraph weights and memory do not match, check that both were updated at all times.')
                    else:
                        print('constants.USE_NAVIGATION_AGENT was False. It may need to be true to get the shortest path.')
                    pdb.set_trace()
            if weight == MAX_WEIGHT:
                break
            max_point += 1
        path = path[:max_point]

        actions = [self.get_plan_move(path[ii], path[ii + 1]) for ii in range(len(path) - 1)]
        return actions, path 
开发者ID:danielgordon10,项目名称:thor-iqa-cvpr-2018,代码行数:36,代码来源:graph_obj.py


注:本文中的networkx.astar_path方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。