-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar.py
49 lines (40 loc) · 1.76 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
41
42
43
44
45
46
47
48
49
''' A* pathfinding. '''
from queue import PriorityQueue
def a_star_search(cells, start, goal, passable_metric):
''' Find the fastest path from the start to the goal in the given area.
A slightly modified version of http://www.redblobgames.com/pathfinding/a-star/implementation.html .
Args:
cells (dict<Point, Cell>): A map of points (locations) to cells.
start (Point): The starting point of the search.
goal (Point): The intended destination of the search.
passable_metric (function<Cell:bool>): A function returning True iff a cell can be pathed through.
Returns:
An ordered list of points to travel to reach the goal.
Includes the goal, but does not include the start.
'''
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == goal:
break
if not passable_metric(cells[current]):
continue # don't path through impassable cells!
neighbors = [loc for loc in cells if loc.adjacent(current)] # inefficient
for next_loc in neighbors:
new_cost = cost_so_far[current] + 1
if next_loc not in cost_so_far or new_cost < cost_so_far[next_loc]:
cost_so_far[next_loc] = new_cost
priority = new_cost + (goal - next_loc).abs()
frontier.put(next_loc, priority)
came_from[next_loc] = current
if goal not in came_from: # couldn't find any path
return None
path = [goal]
while came_from[path[-1]] != start:
path.append(came_from[path[-1]])
return list(reversed(path))