def get_route_astar_funk(events, dist_matrix, time_in, time_out, copy_dist_matrix): graph, nodes = make_graph(len(events), len(events), events) paths = AStarGrid(graph) start, end = nodes[0][0], nodes[len(events) - 1][len(events) - 1] path = paths.search(time_in, time_out, start, end, dist_matrix, copy_dist_matrix, events) return path
# http://scriptogr.am/jdp/post/pathfinding-with-python-graphs-and-a-star from astar_grid import AStarGrid, AStarGridNode from itertools import product # def make_graph(mapinfo): def make_graph(width, height): # nodes = [[AStarGridNode(x, y) for y in range(mapinfo.height)] for x in range(mapinfo.width)] nodes = [[AStarGridNode(x, y) for y in range(height)] for x in range(width)] graph = {} for x, y in product(range(width), range(height)): node = nodes[x][y] graph[node] = [] for i, j in product([-1, 0, 1], [-1, 0, 1]): if not (0 <= x + i < width): continue if not (0 <= y + j < height): continue graph[nodes[x][y]].append(nodes[x+i][y+j]) return graph, nodes graph, nodes = make_graph(8, 8) paths = AStarGrid(graph) start, end = nodes[1][1], nodes[5][7] path = paths.search(start, end) if path is None: print "No path found" else: print "Path found:", path
from itertools import product def make_graph(mapinfo): nodes = [[AStarGridNode(x, y) for y in range(mapinfo['height'])] for x in range(mapinfo['width'])] graph = {} for x, y in product(range(mapinfo['width']), range(mapinfo['height'])): node = nodes[x][y] graph[node] = [] for i, j in product([-1, 0, 1], [-1, 0, 1]): if not (0 <= x + i < mapinfo['width']): continue if not (0 <= y + j < mapinfo['height']): continue if [x + i, y + j] in mapinfo['obstacle']: continue graph[nodes[x][y]].append(nodes[x + i][y + j]) return graph, nodes graph, nodes = make_graph({ "width": 8, "height": 8, "obstacle": [[2, 5], [3, 5], [4, 5], [5, 5]] }) paths = AStarGrid(graph) start, end = nodes[1][1], nodes[5][7] path = paths.search(start, end) if path is None: print "No path found" else: print "Path found:", path