-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaStar.py
177 lines (123 loc) · 5.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from google.colab import drive
drive.mount("/content/drive")
# In[2]:
get_ipython().run_line_magic("pip", "install haversine")
# Earth is almost a spherical body so we should use haversine distance between two points
from haversine import haversine
# In[ ]:
# read the node.csv file
import pandas as pd
latlonData = pd.read_csv("/content/drive/My Drive/AI Assignment/nodes.csv")
# function to get h(node)
def calculateHeuristic(currNode):
[currLat, currLon] = latlonData[latlonData["id"] == currNode].iloc[0][
["lat", "lon"]
]
curr = (currLat, currLon)
dest = (17.240673, 78.432342)
return haversine(curr, dest)
# In[4]:
# to construct graph from edges.csv
get_ipython().run_line_magic("pip", "install networkx")
import networkx as nx
# read the edges.csv file
graphData = pd.read_csv("/content/drive/My Drive/AI Assignment/edges.csv")
# we require only three columns to make a weighted graph
graphData = graphData[["source", "target", "length"]]
# making graph
graphType = nx.Graph()
g = nx.from_pandas_edgelist(graphData, edge_attr="length", create_using=graphType)
# using openSet as a heap
import heapq
# function to create final route
def createPath(cameFrom, current):
path = [current]
while current in cameFrom.keys():
current = cameFrom[current]
path.insert(0, current)
return path
# A* algorithm implementation
def aStar(srcNode, destNode):
# set of discovered node that may need to be re-expanded. Implemented as heap.
openSet = []
# cameFrom[node] is the parent node of current node.
cameFrom = {}
cameFrom[srcNode] = None
# tentative gScore
# i.e. in while loop this need to be changed if new gScore for the same node is less via some other optimal path
cost = {}
cost[srcNode] = 0
# fScore(node) = gScore(node) + h(node)
fScore = {}
fScore[srcNode] = 0
# cost to reach current node from the source node following an optimal path
gScore = {}
gScore[srcNode] = 0
# pushing current node to openSet which is implemented as a heap
heapq.heappush(openSet, (srcNode, fScore))
# search begins
while len(openSet) > 0:
# popping out the visited node from openSet
currentNode = heapq.heappop(openSet)
# if arrived at destination then create path and return it
if currentNode[0] == destNode:
return createPath(cameFrom, currentNode[0])
# fetch neighbours of current node from graph
neighbourData = list(g.neighbors(currentNode[0]))
# in neighbourData
for item in neighbourData:
# distance = length of edge between currentNode & neighbourNode
# neighbourNode is osm id of the node
neighbourNode = item
distance = g[currentNode[0]][neighbourNode]["length"]
# if neighbourNode has not been visited before
if neighbourNode not in cameFrom:
# cost = distance from srcNode to neighbourNode through currentNode i.e. tentative gScore of neighbourNode
cost[neighbourNode] = gScore[currentNode[0]] + distance
# if we have a neighbour that has been visited already by some other path
# and we are visiting it again via some new path, we check which path is optimum
if cost[neighbourNode] < gScore.get(neighbourNode, float("inf")):
# if true then this path is better one than the previous
# so we update everything
cameFrom[neighbourNode] = currentNode[0]
gScore[neighbourNode] = cost[neighbourNode]
fScore[neighbourNode] = gScore[neighbourNode] + calculateHeuristic(
neighbourNode
)
if neighbourNode not in openSet:
heapq.heappush(openSet, (neighbourNode, fScore))
# return openSet: it is empty, so destination was never reached. [Failure]
return openSet
# In[ ]:
# osm id of source node
srcNode = 7065632060
# osm ide of destination node
destNode = 5711258337
# call to aStar
route = aStar(srcNode, destNode)
# popping the None
route.pop(0)
# if path not found, inform the user
if len(route) == 0:
print(f"Fatal Error: Path doesn't exist")
# In[6]:
# aStar function return list(route) which contains osm id's so we need to obtain lat lons from latlonData for each osm id
latlonRoute = []
for node in route:
[lat, lon] = latlonData[latlonData["id"] == node].iloc[0][["lat", "lon"]]
latlonRoute.append((lat, lon))
# to find meanLat and meanLon so as to display middle location on map
from statistics import mean
meanLat = mean(point[0] for point in latlonRoute)
meanLon = mean(point[1] for point in latlonRoute)
get_ipython().run_line_magic("pip", "install gmplot")
import gmplot
routeLats, routeLons = zip(*latlonRoute)
gmap = gmplot.GoogleMapPlotter(meanLat, meanLon, 13)
# uncomment the foloowing line to make a scatter plot the nodes along thr route
# gmap.scatter(routeLats, routeLons, '#FF0000', size = 50, marker = False )
gmap.plot(routeLats, routeLons, "cornflowerblue", edge_width=3.0)
gmap.draw("route.html")