-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathing.py
38 lines (35 loc) · 1.26 KB
/
pathing.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
"""Generalized pathing functions."""
import heapq
def a_star(start, goal, adjacent, heuristic, success):
"""A* pathing algorithm"""
open_heap = []
seen = set()
came_from = {}
g_score = {}
f_score = {}
g_score[start] = 0
f_score[start] = g_score[start] + heuristic(start, goal)
open_heap.append((f_score[start], start))
seen.add(start)
while True:
node = heapq.heappop(open_heap)[1]
if success(node, goal):
print("heap: " + str(len(open_heap)))
return _reconstruct_path(came_from, node)
for adj, action in adjacent(node):
if adj in seen:
continue
seen.add(adj)
came_from[adj] = (node, action)
# adjacency is based on a constant time step
g_score[adj] = g_score[node] + 1
h_score = heuristic(adj, goal)
f_score[adj] = g_score[adj] + h_score
heapq.heappush(open_heap, (f_score[adj], adj))
def _reconstruct_path(came_from, node):
"""trace back the path built by A*"""
if node in came_from:
path = _reconstruct_path(came_from, came_from[node][0])
return path + [(node, came_from[node][1])]
else:
return [node]