Last active
April 15, 2022 15:42
-
-
Save jdp/1687840 to your computer and use it in GitHub Desktop.
A* pathfinding over any arbitrary graph structure, with example Cartesian grid implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AStar(object): | |
def __init__(self, graph): | |
self.graph = graph | |
def heuristic(self, node, start, end): | |
raise NotImplementedError | |
def search(self, start, end): | |
openset = set() | |
closedset = set() | |
current = start | |
openset.add(current) | |
while openset: | |
current = min(openset, key=lambda o:o.g + o.h) | |
if current == end: | |
path = [] | |
while current.parent: | |
path.append(current) | |
current = current.parent | |
path.append(current) | |
return path[::-1] | |
openset.remove(current) | |
closedset.add(current) | |
for node in self.graph[current]: | |
if node in closedset: | |
continue | |
if node in openset: | |
new_g = current.g + current.move_cost(node) | |
if node.g > new_g: | |
node.g = new_g | |
node.parent = current | |
else: | |
node.g = current.g + current.move_cost(node) | |
node.h = self.heuristic(node, start, end) | |
node.parent = current | |
openset.add(node) | |
return None | |
class AStarNode(object): | |
def __init__(self): | |
self.g = 0 | |
self.h = 0 | |
self.parent = None | |
def move_cost(self, other): | |
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from astar import AStar, AStarNode | |
from math import sqrt | |
class AStarGrid(AStar): | |
def heuristic(self, node, start, end): | |
return sqrt((end.x - node.x)**2 + (end.y - node.y)**2) | |
class AStarGridNode(AStarNode): | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
super(AStarGridNode, self).__init__() | |
def move_cost(self, other): | |
diagonal = abs(self.x - other.x) == 1 and abs(self.y - other.y) == 1 | |
return 14 if diagonal else 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from astar_grid import AStarGrid, AStarGridNode | |
from itertools import product | |
def make_graph(width, height): | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (C) 2012 Justin Poliey <[email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mapinfo.height/width should probably be mapinfo['height/width'] ?
i made the bugfix that vindolin suggested as well as improved pretty printing and added an obstacle to the gridworld example in my fork: https://gist.github.com/benaxelrod/57dd9202a93e8295c2c1
It always returns "No path found" for me. and it doesn't go inside the for loop- "for node in self.graph[current]: " (checked)
What may be the reason..
Damn, these are the prettiest lines of code that I've seen in a long time.
Great work!
hm, does it affect the results at all that you say each node is adjacent to itself?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great code!