-
Notifications
You must be signed in to change notification settings - Fork 0
/
aStarSearchAlgorithm.py
365 lines (329 loc) · 15.9 KB
/
aStarSearchAlgorithm.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
import tkinter as tk
import math
class Cell:
def __init__(self, _parent_i, _parent_j, _f, _g, _h):
self.parent_i = _parent_i
self.parent_j = _parent_j
self.f = _f
self.g = _g
self.h = _h
class AStarSearchAlgorithm:
def __init__(self):
self.root = tk.Tk()
self.root.title("A* Search Algorithm")
self.root.geometry("800x800")
self.root.resizable(False, False)
self.matrix = [[1 for x in range(80)] for y in range(80)]
self.start = [-1,-1]
self.dest = [-1,-1]
self.cellSize = 10
self.cellKind = 0
self.gridSize = 80
self.map = {0: "black", 2: "green", 3: "red"}
tools = tk.Toplevel(self.root)
tools.title("Tools")
tools.geometry("420x30")
tools.resizable(False, False)
# A Label widget to show in toplevel
f = tk.Frame(tools, width=800)
self.b1 = tk.Button(f, text ="Start cell", bg='green', fg = "white",command=self._toStart)
self.b2 = tk.Button(f, text ="dest cell", bg='red', fg = "white", command=self._toDest)
self.b3 = tk.Button(f, text ="Obstacle cell", bg='black', fg = "white", command=self._toObstacle)
self.b3 = tk.Button(f, text ="Obstacle cell", bg='black', fg = "white", command=self._toObstacle)
self.b4 = tk.Button(f, text ="Run Algorithm", bg='blue', fg = "white", command=self._runAlgorithm)
self.b1.pack(side = tk.LEFT)
self.b2.pack(side = tk.LEFT)
self.b3.pack(side = tk.LEFT)
self.b4.pack(side = tk.BOTTOM)
f.pack()
self.wn=tk.Canvas(self.root, width=800, height=800, bg='white')
self._initialBoundery()
self.wn.bind('<Button-1>', self._paint)
self.wn.bind('<B1-Motion>', self._paint)
self.wn.pack()
self.root.mainloop()
def _initialBoundery(self):
for x in range(self.gridSize):
y = 0
self.matrix[y][x] = 1
x = x * self.cellSize
y = y * self.cellSize
x1, y1 = x, y
x2, y2 = (x + self.cellSize), (y + self.cellSize)
self.wn.create_rectangle(x1, y1, x2, y2, fill="black", outline="black")
for x in range(self.gridSize):
y = self.gridSize - 1
self.matrix[y][x] = 1
x = x * self.cellSize
y = y * self.cellSize
x1, y1 = x, y
x2, y2 = (x + self.cellSize), (y + self.cellSize)
self.wn.create_rectangle(x1, y1, x2, y2, fill="black", outline="black")
for y in range(self.gridSize):
x = 0
self.matrix[y][x] = 1
x = x * self.cellSize
y = y * self.cellSize
x1, y1 = x, y
x2, y2 = (x + self.cellSize), (y + self.cellSize)
self.wn.create_rectangle(x1, y1, x2, y2, fill="black", outline="black")
for y in range(self.gridSize):
x = self.gridSize - 1
self.matrix[y][x] = 1
x = x * self.cellSize
y = y * self.cellSize
x1, y1 = x, y
x2, y2 = (x + self.cellSize), (y + self.cellSize)
self.wn.create_rectangle(x1, y1, x2, y2, fill="black", outline="black")
def _toObstacle(self):
self.cellKind = 0
def _toStart(self):
self.cellKind = 2
def _toDest(self):
self.cellKind = 3
def _isValid(self, y, x):
return (y >= 0) and (y < self.gridSize) and (x >= 0) and (x < self.gridSize)
def _isUnBlocked(self, y, x):
if self.matrix[y][x] == 1:
return True
return False
def _isDestination(self, y, x):
if (y == self.dest[0] and x == self.dest[1]):
return True
return False
def _calculateHValue(self, y, x):
return math.sqrt((y - self.dest[0])**2 + (x - self.dest[1])**2)
def _tracePath(self, cellDetails):
y = self.dest[0]
x = self.dest[1]
path = []
while(not((cellDetails[y][x].parent_i == y and cellDetails[y][x].parent_j == x))):
path.append([y,x])
temp_y = cellDetails[y][x].parent_i;
temp_x = cellDetails[y][x].parent_j;
y = temp_y;
x = temp_x;
path.append([y, x])
color = "blue"
maxVal = len(path)
while(len(path) > 0):
p = path.pop()
yy, xx = p
yy *= self.cellSize
xx *= self.cellSize
if len(path) > 0 and len(path) < maxVal -1:
self.wn.create_rectangle(xx, yy, xx + self.cellSize, yy + self.cellSize, fill=color, outline=color)
def _aStarSearch(self):
if not self._isValid(self.start[0], self.start[1]):
print("Start is invalid")
return
if not self._isValid(self.dest[0], self.dest[1]):
print("Destination is invalid")
return
if self._isUnBlocked(self.start[0], self.start[1]) == False or self._isUnBlocked(self.dest[0], self.dest[1]) == False:
print("Source or the destination is blocked")
return
if self._isDestination(self.start[0], self.start[1]):
print("We are already at the destination")
return
closedList = [[False for x in range(self.gridSize)] for y in range(self.gridSize)]
cellDetails = [[Cell(-1, -1, math.inf, math.inf, math.inf) for x in range(self.gridSize)] for y in range(self.gridSize)]
i, j = self.start[0], self.start[1]
cellDetails[i][j].f = 0.0
cellDetails[i][j].g = 0.0
cellDetails[i][j].h = 0.0
cellDetails[i][j].parent_i = i
cellDetails[i][j].parent_j = j
openList = set()
openList.add((0.0, i, j))
foundDest = False
while len(openList) > 0:
p = openList.pop()
i = p[1]
j = p[2]
closedList[i][j] = True
gNew, hNew, fNew = 0, 0, 0
if self._isValid(i - 1, j):
if self._isDestination(i - 1, j):
cellDetails[i - 1][j].parent_i = i
cellDetails[i - 1][j].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return
elif closedList[i - 1][j] == False and self._isUnBlocked(i - 1, j) == True:
gNew = cellDetails[i][j].g + 1.0;
hNew = self._calculateHValue(i - 1, j)
fNew = gNew + hNew
if cellDetails[i - 1][j].f == math.inf or cellDetails[i - 1][j].f > fNew:
openList.add((fNew, i - 1, j))
cellDetails[i - 1][j].f = fNew
cellDetails[i - 1][j].g = gNew
cellDetails[i - 1][j].h = hNew
cellDetails[i - 1][j].parent_i = i
cellDetails[i - 1][j].parent_j = j
if self._isValid(i + 1, j):
if self._isDestination(i + 1, j):
cellDetails[i + 1][j].parent_i = i
cellDetails[i + 1][j].parent_j = j
self._tracePath(cellDetails);
foundDest = True;
return;
elif closedList[i + 1][j] == False and self._isUnBlocked(i + 1, j) == True:
gNew = cellDetails[i][j].g + 1.0
hNew = self._calculateHValue(i + 1, j)
fNew = gNew + hNew
if cellDetails[i + 1][j].f == math.inf or cellDetails[i + 1][j].f > fNew:
openList.add((fNew, i + 1, j))
cellDetails[i + 1][j].f = fNew
cellDetails[i + 1][j].g = gNew
cellDetails[i + 1][j].h = hNew
cellDetails[i + 1][j].parent_i = i
cellDetails[i + 1][j].parent_j = j
if self._isValid(i, j + 1):
if self._isDestination(i, j + 1):
cellDetails[i][j + 1].parent_i = i
cellDetails[i][j + 1].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return;
elif closedList[i][j + 1] == False and self._isUnBlocked(i, j + 1) == True:
gNew = cellDetails[i][j].g + 1.0
hNew = self._calculateHValue(i, j + 1)
fNew = gNew + hNew
if cellDetails[i][j + 1].f == math.inf and cellDetails[i][j + 1].f > fNew:
openList.add((fNew, i, j + 1))
cellDetails[i][j + 1].f = fNew
cellDetails[i][j + 1].g = gNew
cellDetails[i][j + 1].h = hNew
cellDetails[i][j + 1].parent_i = i
cellDetails[i][j + 1].parent_j = j
if self._isValid(i, j - 1):
if self._isDestination(i, j - 1):
cellDetails[i][j - 1].parent_i = i
cellDetails[i][j - 1].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return;
elif closedList[i][j - 1] == False and self._isUnBlocked(i, j - 1) == True:
gNew = cellDetails[i][j].g + 1.0;
hNew = self._calculateHValue(i, j - 1)
fNew = gNew + hNew;
if cellDetails[i][j - 1].f == math.inf or cellDetails[i][j - 1].f > fNew:
openList.add((fNew, i, j - 1))
cellDetails[i][j - 1].f = fNew
cellDetails[i][j - 1].g = gNew
cellDetails[i][j - 1].h = hNew
cellDetails[i][j - 1].parent_i = i
cellDetails[i][j - 1].parent_j = j
if self._isValid(i - 1, j + 1):
if self._isDestination(i - 1, j + 1):
cellDetails[i - 1][j + 1].parent_i = i
cellDetails[i - 1][j + 1].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return
elif closedList[i - 1][j + 1] == False and self._isUnBlocked(i - 1, j + 1) == True:
gNew = cellDetails[i][j].g + 1.414;
hNew = self._calculateHValue(i - 1, j + 1)
fNew = gNew + hNew
if cellDetails[i - 1][j + 1].f == math.inf or cellDetails[i - 1][j + 1].f > fNew:
openList.add((fNew, i - 1, j + 1))
cellDetails[i - 1][j + 1].f = fNew
cellDetails[i - 1][j + 1].g = gNew
cellDetails[i - 1][j + 1].h = hNew
cellDetails[i - 1][j + 1].parent_i = i
cellDetails[i - 1][j + 1].parent_j = j
if self._isValid(i - 1, j - 1):
if self._isDestination(i - 1, j - 1):
cellDetails[i - 1][j - 1].parent_i = i
cellDetails[i - 1][j - 1].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return
elif closedList[i - 1][j - 1] == False and self._isUnBlocked(i - 1, j - 1) == True:
gNew = cellDetails[i][j].g + 1.414
hNew = self._calculateHValue(i - 1, j - 1)
fNew = gNew + hNew
if cellDetails[i - 1][j - 1].f == math.inf or cellDetails[i - 1][j - 1].f > fNew:
openList.add((fNew, i - 1, j - 1))
cellDetails[i - 1][j - 1].f = fNew
cellDetails[i - 1][j - 1].g = gNew
cellDetails[i - 1][j - 1].h = hNew
cellDetails[i - 1][j - 1].parent_i = i
cellDetails[i - 1][j - 1].parent_j = j
if self._isValid(i + 1, j + 1):
if self._isDestination(i + 1, j + 1):
cellDetails[i + 1][j + 1].parent_i = i
cellDetails[i + 1][j + 1].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return
elif closedList[i + 1][j + 1] == False and self._isUnBlocked(i + 1, j + 1) == True:
gNew = cellDetails[i][j].g + 1.414
hNew = self._calculateHValue(i + 1, j + 1)
fNew = gNew + hNew
if cellDetails[i + 1][j + 1].f == math.inf or cellDetails[i + 1][j + 1].f > fNew:
openList.add((fNew, i + 1, j + 1))
cellDetails[i + 1][j + 1].f = fNew
cellDetails[i + 1][j + 1].g = gNew
cellDetails[i + 1][j + 1].h = hNew
cellDetails[i + 1][j + 1].parent_i = i
cellDetails[i + 1][j + 1].parent_j = j
if self._isValid(i + 1, j - 1):
if self._isDestination(i + 1, j - 1):
cellDetails[i + 1][j - 1].parent_i = i
cellDetails[i + 1][j - 1].parent_j = j
self._tracePath(cellDetails)
foundDest = True
return
elif closedList[i + 1][j - 1] == False and self._isUnBlocked(i + 1, j - 1) == True:
gNew = cellDetails[i][j].g + 1.414;
hNew = self._calculateHValue(i + 1, j - 1);
fNew = gNew + hNew;
if cellDetails[i + 1][j - 1].f == math.inf or cellDetails[i + 1][j - 1].f > fNew:
openList.add((fNew, i + 1, j - 1))
cellDetails[i + 1][j - 1].f = fNew
cellDetails[i + 1][j - 1].g = gNew
cellDetails[i + 1][j - 1].h = hNew
cellDetails[i + 1][j - 1].parent_i = i
cellDetails[i + 1][j - 1].parent_j = j
if not foundDest:
print("Failed to find the Destination Cell")
return
def _runAlgorithm(self):
print("Runing...")
self.matrix[self.start[0]][self.start[1]] = 1
self.matrix[self.dest[0]][self.dest[1]] = 1
print(self.start)
print(self.dest)
self._aStarSearch()
def _paint(self, event):
x,y = event.x,event.y
xm = (x * 80)//800
ym = (y * 80)//800
x = xm * self.cellSize
y = ym * self.cellSize
if xm >= 0 and xm <= 79 and ym >= 0 and ym <= 79:
x1, y1 = (x), (y)
x2, y2 = (x + self.cellSize), (y + self.cellSize)
color = self.map[self.cellKind]
if self.cellKind == 2:
if self.start[0] == -1 and self.start[1] == -1:
self.start = [ym, xm]
self.wn.create_rectangle(x1, y1, x2, y2, fill=color, outline=color)
else:
self.wn.create_rectangle(self.start[1]*self.cellSize,self.start[0]*self.cellSize,self.start[1]*self.cellSize + self.cellSize,self.start[0]*self.cellSize + self.cellSize, fill="white", outline="white")
self.start = [ym, xm]
self.wn.create_rectangle(x1, y1, x2, y2, fill=color, outline=color)
elif self.cellKind == 3:
if self.dest[0] == -1 and self.dest[1] == -1:
self.dest = [ym, xm]
self.wn.create_rectangle(x1, y1, x2, y2, fill=color, outline=color)
else:
self.wn.create_rectangle(self.dest[1]*self.cellSize,self.dest[0]*self.cellSize,self.dest[1]*self.cellSize + self.cellSize,self.dest[0]*self.cellSize + self.cellSize, fill="white", outline="white")
self.dest = [ym, xm]
self.wn.create_rectangle(x1, y1, x2, y2, fill=color, outline=color)
else:
self.matrix[ym][xm] = 0
self.wn.create_rectangle(x1, y1, x2, y2, fill=color, outline=color)
a = AStarSearchAlgorithm()