-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.py
40 lines (33 loc) · 1.23 KB
/
astar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import heapq
class AStar:
def __init__(self, graph):
self.graph = graph
def heuristic(self, a, b):
# Simple Euclidean distance heuristic
return ((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** 0.5
def find_path(self, start, goal):
frontier = []
heapq.heappush(frontier, (0, start))
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while frontier:
current = heapq.heappop(frontier)[1]
if current == goal:
break
for next in self.graph.neighbors(current):
new_cost = cost_so_far[current] + self.graph.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + self.heuristic(goal, self.graph.get_node_pos(next))
heapq.heappush(frontier, (priority, next))
came_from[next] = current
path = []
current = goal
while current != start:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path