-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.py
67 lines (49 loc) · 1.65 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from queue import PriorityQueue
graph = {
'S': {'1': 3, '4': 4},
'1': {'2': 4, '4': 5},
'2': {'1': 4, '5': 5, '3': 4},
'3': {'2': 4},
'4': {'S': 4, '1': 5, '5': 2},
'5': {'4': 2, '2': 5, '6': 4},
'6': {'5': 4, '7': 3},
'7': {'6': 3}
}
h = {
'S': 15, '1': 14, '2': 10, '3': 8, '4': 12, '5': 10, '6': 10, '7': 0
}
def a_star(start, goal):
open_list = PriorityQueue()
open_list.put((0+h[start], start, 0))
closed_list = {}
cost = {start: 0}
parent = {start: None}
while not open_list.empty():
curr_f, current_node, current_cost = open_list.get()
closed_list[current_node] = (current_cost, h[current_node])
if current_node == goal:
path = []
while current_node is not None:
path.append(current_node)
current_node = parent[current_node]
path.reverse()
return path, cost[goal]
for neighbor, neighbor_cost in graph[current_node].items():
tentative_cost = current_cost + neighbor_cost
if neighbor in closed_list:
continue
if neighbor in cost and tentative_cost >= cost[neighbor]:
continue
open_list.put((tentative_cost+h[neighbor], neighbor, tentative_cost))
cost[neighbor] = tentative_cost
parent[neighbor] = current_node
print('Open List:', list(open_list.queue))
print('Closed List:', closed_list)
print()
return None
start = input("Enter Start Node : ")
goal = input("Enter Goal Node : ")
print()
path, cost = a_star(start, goal)
print('Path:', path)
print('Cost:', cost)