-
Notifications
You must be signed in to change notification settings - Fork 1
/
astar.py
41 lines (33 loc) · 1.15 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
from queue import PriorityQueue
#
# A* implementation.
#
def astar(start, goal, alpha = None, classifier = None):
start.g = 0
start.f = start.g + start.h(goal)
frontier = PriorityQueue()
frontier.put(start)
while not frontier.empty():
current = frontier.get()
if current == goal:
while current.parent is not None:
parent = current.parent
parent.child = current
current = parent
return True
# Since PriorityQueue in python doesn't support removing nodes, there might be duplicates in the queue.
# We skip those visited nodes here.
if current.visited:
continue
current.visited = True
for neighbor in current.neighbors:
if neighbor.visited:
continue
new_g = current.g + current.cost(neighbor, alpha, classifier)
if new_g < neighbor.g:
neighbor.g = new_g
neighbor.f = new_g + neighbor.h(goal)
frontier.put(neighbor)
neighbor.inqueue = True
neighbor.parent = current
return False