-
Notifications
You must be signed in to change notification settings - Fork 1
/
AstarAlgorithm.py
388 lines (366 loc) · 12.7 KB
/
AstarAlgorithm.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import numpy as np
import math
#import matplotlib.pyplot as plt
class cell:
def __init__(self):
self.parent = [-1,-1]
self.f = math.inf
self.g = math.inf
self.h = math.inf
'''**********************************
Function name : isValid
Functionality : checks whether the cell with given coordinates is within the range of maze and is unblocked
Arguments : coordinates - list of 2 elements [x,y]
Return Value : True/False
***********************************'''
def isValid(coordinates):
return (coordinates[0] in range(dims[0]) and coordinates[1] in range(dims[1]) and maze[coordinates[0]][coordinates[1]])
'''**********************************
Function name : grassFire
Functionality : used as heuristics for Astar
Note: Here we have not used simple heruistic functions as the are not very effective
Arguments : cells, src
Return Value : None
***********************************'''
def grassFire(cells,src):
tempList = []
tempList.append(src)
while(len(tempList)):
temp = tempList[0]
x,y = temp
tempList.pop(0)
if(isValid([x-1,y]) and cells[x-1][y].h == math.inf):
cells[x-1][y].h = min(cells[x][y].h + 1.0,cells[x-1][y].h)
tempList.append([x-1,y])
if(isValid([x,y-1]) and cells[x][y-1].h == math.inf):
cells[x][y-1].h = min(cells[x][y].h + 1.0,cells[x][y-1].h)
tempList.append([x,y-1])
if(isValid([x,y+1]) and cells[x][y+1].h == math.inf):
cells[x][y+1].h = min(cells[x][y].h + 1.0,cells[x][y+1].h)
tempList.append([x,y+1])
if(isValid([x+1,y]) and cells[x+1][y].h == math.inf):
cells[x+1][y].h = min(cells[x][y].h + 1.0,cells[x+1][y].h)
tempList.append([x+1,y])
def calculateHeuristics(coordinates,dest):
return abs(coordinates[0]-dest[0]) + abs(coordinates[1]-dest[1])
'''**********************************
Function name : tracePath
Functionality : Used to track the path obtained used Astar
Arguments : cells, dest
Return Value : Final shortest path
***********************************'''
def tracePath(cells,dest):
x,y = dest
path = []
while(not (cells[x][y].parent == [x,y])):
path.append([x,y])
x,y = cells[x][y].parent
return path[::-1]
'''**********************************
Function name : astarAlgorithm
Functionality : Assigns value of f,g to all cells of the maze
Arguments : src, dest
Return Value : None
***********************************'''
def astarAlgorithm(src,dest):
if(isValid(src) == False):
print("Invalid Source")
return
if(isValid(dest) == False):
print("Invalid Destination")
return
if(src == dest):
print("Already at the destination")
return
closedList = np.zeros(dims)
cells = []
#cells = [[cell()]*dims[1]]*dims[0]
for i in range(dims[0]):
temp = []
for j in range(dims[1]):
temp.append(cell())
cells.append(temp)
#src.reverse()
x,y = src
#dest.reverse()
cells[x][y].parent=[x,y]
cells[x][y].f = 0.0
cells[x][y].g = 0.0
cells[x][y].h = 0.0
grassFire(cells,src)
openList = []
openList.append([0,src])
foundDest = False
while(len(openList)):
openList.sort(key = lambda li: li[0])
temp = openList[0]
#print(openList)
x,y = temp[1]
openList.pop(0)
closedList[x][y] = 1
'''8 Possible successors of this cell
N.W. N N.E
\ | /
\ | /
W - ---Cell - ---E
/ | \
/ | \
S.W S S.E.
Cell -->Popped Cell(i, j)'''
#CASE1: N.W
x -= 1
y -= 1
if(isValid([x,y])):
if(dest == [x,y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if(not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g+1.0
hNew = cells[x][y].h
fNew = gNew + hNew
#print(cells[x][y].f)
if(cells[x][y].f > fNew):
openList.append([fNew,[x,y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE2: N
y += 1
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE3: N.E
y += 1
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE4: W
x += 1
y -= 2
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE5: E
y += 2
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE6: S.W
x += 1
y -= 2
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE7: S
y += 1
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
#CASE8: S.E
y += 1
if (isValid([x, y])):
if (dest == [x, y]):
cells[x][y].parent = temp[1]
foundDest = True
break
if (not closedList[x][y]):
gNew = cells[temp[1][0]][temp[1][1]].g + 1
hNew = cells[x][y].h
fNew = gNew + hNew
if (cells[x][y].f > fNew):
openList.append([fNew, [x, y]])
cells[x][y].parent = temp[1]
cells[x][y].f = fNew
cells[x][y].g = gNew
#cells[x][y].h = hNew
np.transpose(cells)
'''for i in range(0,5):
for j in range(3,9):
print(cells[i][j].f,end=' ')
print('')'''
if(foundDest):
return tracePath(cells,dest) #If destination is found, trace it
else:
print("Failed to reach the destination")
def convertCoordinates(coordinates):
coordinates[0] = math.floor(4*(coordinates[0]+11))
coordinates[1] = math.floor(4*(coordinates[1]+11))
return
def modify_path(src,path):
if(len(path) < 3):
return path
tempList = []
i = 1
prev = src
while(i < len(path)):
fromX = min(prev[0],path[i][0])
toX = prev[0]+path[i][0]-fromX+1
fromY = min(prev[1],path[i][1])
toY = prev[1]+path[i][1]-fromY+1
total = 0
for x in range(fromX,toX):
for y in range(fromY,toY):
total += maze[x][y]
if(total < (toX-fromX)*(toY-fromY)):
prev = path[i-1]
i += 1
else:
path.pop(i-1)
for i in range(len(path)):
x,y = path[i]
if(maze[x][y-1] == 0): # Wall on left
path[i][1] += 2
elif(maze[x][y+1] == 0): #Wall on right
path[i][1] -= 2
if(maze[x-1][y] == 0): #Wall on upper side
path[i][0] += 2
elif(maze[x+1][y] == 0): # Wall on down side
path[i][0] -= 2
return path
def calculateShortestPath(src,dest):
'''Description of Maze -
1 --> Cell is not blocked
0 --> Che cell is blocked'''
global maze
maze = [[1, 1, 1, 1, 1],
[1 ,1 ,1 ,0 ,1],
[0, 0, 1, 0, 1],
[1 ,1 ,1 ,0 ,1],
[1, 0, 1, 0, 1],
[1 ,0 ,1 ,1 ,1],
[1, 0, 1, 0, 1],
[1 ,1 ,1 ,1 ,1 ]]
mat = np.ones((89, 89))
# making boundary and maze
for y in range(89):
mat[0, y] = 0
mat[88, y] = 0
for x in range(89):
mat[x, 0] = 0
mat[x, 88] = 0
for y in range(24, 65):
mat[12, y] = 0
for y in range(24, 65):
mat[76, y] = 0
for y in range(8, 33):
mat[28, y] = 0
for y in range(56, 82):
mat[28, y] = 0
for y in range(8, 33):
mat[60, y] = 0
for y in range(56, 82):
mat[60, y] = 0
for x in range(36, 53):
mat[x, 20] = 0
for x in range(36, 53):
mat[x, 68] = 0
maze = mat
'''output = open('maze1.txt','w')
for row in maze:
temp = []
for a in row:
temp += str(int(a))
temp += '\n'
temp = ''.join(temp)
output.write(temp)
output.close()'''
global dims
dims = [89,89]
convertCoordinates(src)
convertCoordinates(dest)
# print(src,dest)
path = astarAlgorithm(src, dest)
# print(path)
path = modify_path(src,path)
# print('')
# print(path)
# print('')
return path[:]
x = [x[0] for x in path[:-1]]
y = [y[1] for y in path[:-1]]
#Just extra function to plot the maze for crossverification
'''plt.pcolormesh(maze)
plt.plot(maze)
plt.axes().set_aspect('equal') # set the x and y axes to the same scale
plt.xticks([]) # remove the tick marks by setting to an empty list
plt.yticks([]) # remove the tick marks by setting to an empty list
plt.axes().invert_yaxis() # invert the y-axis so the first row of data is at the top
plt.plot(src[0],src[1],'bo')
plt.plot(dest[0],dest[1],'go')
plt.plot(y,x,'ro')
plt.show()'''
#The algorithm can be improved in terms of time complexity if fibonacci heap is used as openlist