-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_StarAlgorithm.py
More file actions
49 lines (39 loc) · 1.88 KB
/
A_StarAlgorithm.py
File metadata and controls
49 lines (39 loc) · 1.88 KB
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
from math import inf, sqrt
from heapq import heappop, heappush
from manhattan_graph import manhattan_graph, penn_station, grand_central_station
from euclidean_graph import euclidean_graph, bengaluru, jaipur
# Manhattan Heuristic:
def heuristic(start, target):
x_distance = abs(start.position[0] - target.position[0])
y_distance = abs(start.position[1] - target.position[1])
return x_distance + y_distance
# Euclidean Heuristic:
#def heuristic(start, target):
# x_distance = abs(start.position[0] - target.position[0])
# y_distance = abs(start.position[1] - target.position[1])
# return sqrt(x_distance * x_distance + y_distance * y_distance)
def a_star(graph, start, target):
print("Starting A* algorithm!")
count = 0
paths_and_distances = {}
for vertex in graph:
paths_and_distances[vertex] = [inf, [start.name]]
paths_and_distances[start][0] = 0
vertices_to_explore = [(0, start)]
while vertices_to_explore and paths_and_distances[target][0] == inf:
current_distance, current_vertex = heappop(vertices_to_explore)
for neighbor, edge_weight in graph[current_vertex]:
new_distance = current_distance + edge_weight + heuristic(neighbor, target)
new_path = paths_and_distances[current_vertex][1] + [neighbor.name]
if new_distance < paths_and_distances[neighbor][0]:
paths_and_distances[neighbor][0] = new_distance
paths_and_distances[neighbor][1] = new_path
heappush(vertices_to_explore, (new_distance, neighbor))
count += 1
print("\nAt " + vertices_to_explore[0][1].name)
print("Found a path from {0} to {1} in {2} steps: ".format(start.name, target.name, count), paths_and_distances[target][1])
return paths_and_distances[target][1]
# uncomment manhattan graph
a_star(manhattan_graph, penn_station, grand_central_station)
# uncomment euclidean graph
# a_star(euclidean_graph, bengaluru, jaipur)