-
Notifications
You must be signed in to change notification settings - Fork 10
/
astar.py
52 lines (38 loc) · 1.61 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
import convertJSON as cj
import heapq as heap
import time
def aStar(source, destination):
open_list = []
g_values = {}
path = {}
closed_list = {}
sourceID = cj.getOSMId(source[0], source[1])
destID = cj.getOSMId(destination[0], destination[1])
g_values[sourceID] = 0
h_source = cj.calculateHeuristic(source, destination)
open_list.append((h_source,sourceID))
s = time.time()
while(len(open_list)>0):
curr_state = open_list[0][1]
#print(curr_state)
heap.heappop(open_list)
closed_list[curr_state] = ""
if(curr_state==destID):
print("We have reached to the goal")
break
nbrs = cj.getNeighbours(curr_state, destination)
values = nbrs[curr_state]
for eachNeighbour in values:
neighbourId, neighbourHeuristic, neighbourCost, neighbourLatLon = cj.getNeighbourInfo(eachNeighbour)
current_inherited_cost = g_values[curr_state] + neighbourCost
if(neighbourId in closed_list):
continue
else:
g_values[neighbourId] = current_inherited_cost
neighbourFvalue = neighbourHeuristic + current_inherited_cost
open_list.append((neighbourFvalue, neighbourId))
path[str(neighbourLatLon)] = {"parent":str(cj.getLatLon(curr_state)), "cost":neighbourCost}
open_list = list(set(open_list))
heap.heapify(open_list)
print("Time taken to find path(in second): "+str(time.time()-s))
return path