-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSearchAlgorithms.py
590 lines (476 loc) · 24.2 KB
/
SearchAlgorithms.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import collections
from queue import PriorityQueue
import math
class Node:
id = None # Unique value for each node.
up = None # Represents value of neighbors (up, down, left, right).
down = None
left = None
right = None
previousNode = None # Represents value of neighbors.
edgeCost = None # Represents the cost on the edge from any parent to this node.
gOfN = None # Represents the total edge cost
hOfN = None # Represents the heuristic value
heuristicFn = None # Represents the value of heuristic function
def __init__(self, value):
self.value = value
class SearchAlgorithms:
''' * DON'T change Class, Function or Parameters Names and Order
* You can add ANY extra functions,
classes you need as long as the main
structure is left as is '''
path = [] # Represents the correct path from start node to the goal node.
fullPath = [] # Represents all visited nodes from the start node to the goal node.
totalCost = -1 # Represents the total cost in case using UCS, AStar (Euclidean or Manhattan)
maze = []
board = None
rows = []
cols = []
def __init__(self, mazeStr, edgeCost=None):
""" mazeStr contains the full board
The board is read row wise,
the nodes are numbered 0-based starting
the leftmost node"""
rows = mazeStr.split(' ')
board = []
maze = []
for i in range(len(rows)):
cols = rows[i].split(',')
for j in range(len(cols)):
maze.append(cols[j])
index = 0
for i in range(len(rows)):
board.append([])
for j in range(len(cols)):
node = Node(maze[index])
node.id = index
board[i].append(node)
index += 1
for i in range(len(rows)):
for j in range(len(cols)):
if i == 0:
if j == 0:
board[i][j].up = None
board[i][j].down = board[i + 1][j]
board[i][j].left = None
board[i][j].right = board[i][j + 1]
elif j == (len(cols) - 1):
board[i][j].up = None
board[i][j].down = board[i + 1][j]
board[i][j].left = board[i][j - 1]
board[i][j].right = None
else:
board[i][j].up = None
board[i][j].down = board[i + 1][j]
board[i][j].left = board[i][j - 1]
board[i][j].right = board[i][j + 1]
elif i == (len(rows) - 1):
if j == 0:
board[i][j].up = board[i - 1][j]
board[i][j].down = None
board[i][j].left = None
board[i][j].right = board[i][j + 1]
elif j == (len(cols) - 1):
board[i][j].up = board[i - 1][j]
board[i][j].down = None
board[i][j].left = board[i][j - 1]
board[i][j].right = None
else:
board[i][j].up = board[i - 1][j]
board[i][j].down = None
board[i][j].left = board[i][j - 1]
board[i][j].right = board[i][j + 1]
else:
if j == 0:
board[i][j].up = board[i - 1][j]
board[i][j].down = board[i + 1][j]
board[i][j].left = None
board[i][j].right = board[i][j + 1]
elif j == (len(cols) - 1):
board[i][j].up = board[i - 1][j]
board[i][j].down = board[i + 1][j]
board[i][j].left = board[i][j - 1]
board[i][j].right = None
else:
board[i][j].up = board[i - 1][j]
board[i][j].down = board[i + 1][j]
board[i][j].left = board[i][j - 1]
board[i][j].right = board[i][j + 1]
self.rows = rows
self.cols = cols
self.board = board
if not (edgeCost is None):
x = 0
for i in range(len(self.rows)):
for j in range(len(self.cols)):
self.board[i][j].edgeCost = edgeCost[x]
x += 1
def DFS(self):
# Fill the correct path in self.path
# self.fullPath should contain the order of visited nod
# es
rows_count = len(self.rows)
columns_count = len(self.cols)
visited = [0] * rows_count * columns_count
stack = []
for i in range(rows_count):
for j in range(columns_count):
if self.board[i][j].value == 'S':
stack.append(self.board[i][j])
while len(stack) > 0:
length = len(stack)
current_node = stack[length - 1]
visited[current_node.id] = 1
self.fullPath.append(current_node.id)
if current_node.up is not None and current_node.up.value != '#' and visited[current_node.up.id] == 0:
current_node.up.previousNode = current_node
if current_node.up.value == 'E':
self.fullPath.append(current_node.up.id)
self.path.append(current_node.up.id)
break
elif current_node.up.value == '.':
stack.append(current_node.up)
elif current_node.down is not None and current_node.down.value != '#' \
and visited[current_node.down.id] == 0:
current_node.down.previousNode = current_node
if current_node.down.value == 'E':
self.fullPath.append(current_node.down.id)
self.path.append(current_node.down.id)
break
elif current_node.down.value == '.':
stack.append(current_node.down)
elif current_node.left is not None and current_node.left.value != '#' \
and visited[current_node.left.id] == 0:
current_node.left.previousNode = current_node
if current_node.left.value == 'E':
self.fullPath.append(current_node.left.id)
self.path.append(current_node.left.id)
break
elif current_node.left.value == '.':
stack.append(current_node.left)
elif current_node.right is not None and current_node.right.value != '#' \
and visited[current_node.right.id] == 0:
current_node.right.previousNode = current_node
if current_node.right.value == 'E':
self.fullPath.append(current_node.right.id)
self.path.append(current_node.right.id)
break
elif current_node.right.value == '.':
stack.append(current_node.right)
else:
stack.pop()
while current_node.value != 'S':
self.path.append(current_node.id)
current_node = current_node.previousNode
self.path.append(current_node.id)
self.path.reverse()
temp_list = []
for i in range(len(self.fullPath)):
if self.fullPath[i] not in temp_list:
temp_list.append(self.fullPath[i])
self.fullPath.clear()
self.fullPath = temp_list
return self.path, self.fullPath
def BFS(self):
# Fill the correct path in self.path
# self.fullPath should contain the order of visited nodes
self.fullPath.clear()
self.path.clear()
start = None
end = None
for i in range(len(self.rows)):
for j in range(len(self.cols)):
if self.board[i][j].value == 'S':
start = self.board[i][j]
if self.board[i][j].value == 'E':
end = self.board[i][j]
queue = [start]
visited = set()
while len(queue) != 0:
front = queue.pop(0)
visited.add(front.id)
if front.id not in self.fullPath:
self.fullPath.append(front.id)
if front == end:
end = front
break
up = None
down = None
right = None
left = None
for i in range(len(self.rows)):
for j in range(len(self.cols)):
if self.board[i][j].id == front.id:
up = self.board[i][j].up
down = self.board[i][j].down
right = self.board[i][j].right
left = self.board[i][j].left
if up is not None and up.value != '#' and up.id not in visited:
queue.append(up)
up.previousNode = front
if down is not None and down.value != '#' and down.id not in visited:
queue.append(down)
down.previousNode = front
if left is not None and left.value != '#' and left.id not in visited:
queue.append(left)
left.previousNode = front
if right is not None and right.value != '#' and right.id not in visited:
queue.append(right)
right.previousNode = front
# d = end
# self.path.append(end.id)
while end != start:
self.path.append(end.id)
end = end.previousNode
self.path.append(end.id)
self.path.reverse()
return self.path, self.fullPath
def UCS(self):
# Fill the correct path in self.path
# self.fullPath should contain the order of visited nodes
self.fullPath.clear()
self.path.clear()
open_nodes = [] # not visited
visited = []
start = None
end = None
for i in range(len(self.rows)): # searching for start node
for j in range(len(self.cols)):
if self.board[i][j].value == 'S':
start = self.board[i][j]
open_nodes.append(self.board[i][j])
if self.board[i][j].value == 'E':
end = self.board[i][j]
break
while len(open_nodes) > 0:
node = min(open_nodes, key=lambda x: x.edgeCost)
if node not in visited:
visited.append(node)
self.fullPath.append(node.id)
if node.value == 'E':
break
else:
if node.up is not None and node.up.value != '#':
node.up.previousNode = node
if node.up not in open_nodes and node.up not in visited:
open_nodes.append(node.up)
node.up.edgeCost = node.up.edgeCost + node.up.previousNode.edgeCost
else:
if node.up.edgeCost >= node.up.edgeCost + node.up.previousNode.edgeCost:
node.up.edgeCost = node.up.edgeCost + node.up.previousNode.edgeCost
if node.down is not None and node.down.value != '#':
node.down.previousNode = node
if node.down not in open_nodes and node.down not in visited:
open_nodes.append(node.down)
node.down.edgeCost = node.down.edgeCost + node.down.previousNode.edgeCost
else:
if node.down.edgeCost >= node.down.edgeCost + node.down.previousNode.edgeCost:
node.down.edgeCost = node.down.edgeCost + node.down.previousNode.edgeCost
if node.left is not None and node.left.value != '#':
node.left.previousNode = node
if node.left not in open_nodes and node.left not in visited:
open_nodes.append(node.left)
node.left.edgeCost = node.left.edgeCost + node.left.previousNode.edgeCost
else:
if node.left.edgeCost >= node.left.edgeCost + node.left.previousNode.edgeCost:
node.left.edgeCost = node.left.edgeCost + node.left.previousNode.edgeCost
if node.right is not None and node.right.value != '#':
node.right.previousNode = node
if node.right not in open_nodes and node.right not in visited:
open_nodes.append(node.right)
node.right.edgeCost = node.right.edgeCost + node.right.previousNode.edgeCost
else:
if node.right.edgeCost >= node.right.edgeCost + node.right.previousNode.edgeCost:
node.right.edgeCost = node.right.edgeCost + node.right.previousNode.edgeCost
open_nodes.remove(node)
sz = len(visited)
self.totalCost = visited[sz - 1].edgeCost
''' while end !=start:
self.path.append(end.id)
end =end.previousNode
self.path.reverse()'''
return [], self.fullPath, self.totalCost
def AStarEuclideanHeuristic(self):
self.fullPath.clear()
self.path.clear()
c = 0
d = 0
for i in range(len(self.rows)): # searching for end node to save it to calculate H with all points
for j in range(len(self.cols)):
if self.board[i][j].value == 'E':
c = i
d = j
break
for i in range(len(self.rows)): # calculating HofN and edge cost for each node
for j in range(len(self.cols)):
self.board[i][j].hOfN = math.sqrt((math.pow(abs(i - c), 2)) + math.pow(abs(j - d), 2))
open_nodes = [] # not visited
visited = []
start = None
end = None
for i in range(len(self.rows)): # searching for start node
for j in range(len(self.cols)):
if self.board[i][j].value == 'S':
start = self.board[i][j]
self.board[i][j].gOfN = 0
self.board[i][j].heuristicFn = self.board[i][j].hOfN
open_nodes.append(self.board[i][j])
if self.board[i][j].value == 'E':
end = self.board[i][j]
break
while len(open_nodes) > 0:
node = min(open_nodes, key=lambda x: x.heuristicFn)
if node not in visited:
visited.append(node)
self.fullPath.append(node.id)
if node.value == 'E':
self.totalCost = node.heuristicFn
break
else:
if node.up is not None and node.up.value != '#':
node.up.previousNode = node
if node.up not in open_nodes and node.up not in visited:
open_nodes.append(node.up)
node.up.gOfN = node.up.previousNode.gOfN + node.up.edgeCost
node.up.heuristicFn = node.up.hOfN + node.up.gOfN
else:
if node.up.heuristicFn >= node.up.hOfN + (node.up.previousNode.gOfN + node.up.edgeCost):
node.up.heuristicFn = node.up.hOfN + (node.up.previousNode.gOfN + node.up.edgeCost)
if node.down is not None and node.down.value != '#':
node.down.previousNode = node
if node.down not in open_nodes and node.down not in visited:
open_nodes.append(node.down)
node.down.gOfN = node.down.previousNode.gOfN + node.down.edgeCost
node.down.heuristicFn = node.down.hOfN + node.down.gOfN
else:
if node.down.heuristicFn >= node.down.hOfN + (node.down.previousNode.gOfN + node.down.edgeCost):
node.down.heuristicFn = node.down.hOfN + (node.down.previousNode.gOfN + node.down.edgeCost)
if node.left is not None and node.left.value != '#':
node.left.previousNode = node
if node.left not in open_nodes and node.left not in visited:
open_nodes.append(node.left)
node.left.gOfN = node.left.previousNode.gOfN + node.left.edgeCost
node.left.heuristicFn = node.left.hOfN + node.left.gOfN
else:
if node.left.heuristicFn >= node.left.hOfN + (node.left.previousNode.gOfN + node.left.edgeCost):
node.left.heuristicFn = node.left.hOfN + (node.left.previousNode.gOfN + node.left.edgeCost)
if node.right is not None and node.right.value != '#':
node.right.previousNode = node
if node.right not in open_nodes and node.right not in visited:
open_nodes.append(node.right)
node.right.gOfN = node.right.previousNode.gOfN + node.right.edgeCost
node.right.heuristicFn = node.right.hOfN + node.right.gOfN
else:
if node.right.heuristicFn >= node.right.hOfN + (
node.right.previousNode.gOfN + node.right.edgeCost):
node.right.heuristicFn = node.right.hOfN + (
node.right.previousNode.gOfN + node.right.edgeCost)
open_nodes.remove(node)
return self.path, self.fullPath, self.totalCost
def AStarManhattanHeuristic(self):
self.fullPath.clear()
self.path.clear()
for i in range(len(self.rows)): # searching for end node to save it to calculate H with all points
for j in range(len(self.cols)):
if self.board[i][j].value == 'E':
c = i
d = j
break
for i in range(len(self.rows)): # calculating HofN and edge cost for each node
for j in range(len(self.cols)):
self.board[i][j].hOfN = abs(i - c) + abs(j - d)
open_nodes = [] # not visited
visited = []
start = None
end = None
for i in range(len(self.rows)): # searching for start node
for j in range(len(self.cols)):
if self.board[i][j].value == 'S':
start = self.board[i][j]
self.board[i][j].gOfN = 0
self.board[i][j].heuristicFn = self.board[i][j].hOfN
open_nodes.append(self.board[i][j])
if self.board[i][j].value == 'E':
end = self.board[i][j]
break
while len(open_nodes) > 0:
node = min(open_nodes, key=lambda x: x.heuristicFn)
if node not in visited:
visited.append(node)
self.fullPath.append(node.id)
if node.value == 'E':
self.totalCost = node.heuristicFn
break
else:
if node.up is not None and node.up.value != '#':
node.up.previousNode = node
if node.up not in open_nodes and node.up not in visited:
open_nodes.append(node.up)
node.up.gOfN = node.up.previousNode.gOfN + 1
node.up.heuristicFn = node.up.hOfN + node.up.gOfN
else:
if node.up.heuristicFn >= node.up.hOfN + (node.up.previousNode.gOfN + 1):
node.up.heuristicFn = node.up.hOfN + (node.up.previousNode.gOfN + 1)
if node.down is not None and node.down.value != '#':
node.down.previousNode = node
if node.down not in open_nodes and node.down not in visited:
open_nodes.append(node.down)
node.down.gOfN = node.down.previousNode.gOfN + 1
node.down.heuristicFn = node.down.hOfN + node.down.gOfN
else:
if node.down.heuristicFn >= node.down.hOfN + (node.down.previousNode.gOfN + 1):
node.down.heuristicFn = node.down.hOfN + (node.down.previousNode.gOfN + 1)
if node.left is not None and node.left.value != '#':
node.left.previousNode = node
if node.left not in open_nodes and node.left not in visited:
open_nodes.append(node.left)
node.left.gOfN = node.left.previousNode.gOfN + 1
node.left.heuristicFn = node.left.hOfN + node.left.gOfN
else:
if node.left.heuristicFn >= node.left.hOfN + (node.left.previousNode.gOfN + 1):
node.left.heuristicFn = node.left.hOfN + (node.left.previousNode.gOfN + 1)
if node.right is not None and node.right.value != '#':
node.right.previousNode = node
if node.right not in open_nodes and node.right not in visited:
open_nodes.append(node.right)
node.right.gOfN = node.right.previousNode.gOfN + 1
node.right.heuristicFn = node.right.hOfN + node.right.gOfN
else:
if node.right.heuristicFn >= node.right.hOfN + (node.right.previousNode.gOfN + 1):
node.right.heuristicFn = node.right.hOfN + (node.right.previousNode.gOfN + 1)
open_nodes.remove(node)
return self.path, self.fullPath, self.totalCost
def main():
searchAlgo = SearchAlgorithms('S,.,.,#,.,.,. .,#,.,.,.,#,. .,#,.,.,.,.,. .,.,#,#,.,.,. #,.,#,E,.,#,.')
path, fullPath = searchAlgo.DFS()
print('**DFS**\nPath is: ' + str(path) + '\nFull Path is: ' + str(fullPath) + '\n\n')
#######################################################################################
searchAlgo = SearchAlgorithms('S,.,.,#,.,.,. .,#,.,.,.,#,. .,#,.,.,.,.,. .,.,#,#,.,.,. #,.,#,E,.,#,.')
path, fullPath = searchAlgo.BFS()
print('**BFS**\nPath is: ' + str(path) + '\nFull Path is: ' + str(fullPath) + '\n\n')
#######################################################################################
searchAlgo = SearchAlgorithms('S,.,.,#,.,.,. .,#,.,.,.,#,. .,#,.,.,.,.,. .,.,#,#,.,.,. #,.,#,E,.,#,.',
[0, 15, 2, 100, 60, 35, 30, 3
, 100, 2, 15, 60, 100, 30, 2
, 100, 2, 2, 2, 40, 30, 2, 2
, 100, 100, 3, 15, 30, 100, 2
, 100, 0, 2, 100, 30])
path, fullPath, TotalCost = searchAlgo.UCS()
print('** UCS **\nPath is: ' + str(path) + '\nFull Path is: ' + str(fullPath) + '\nTotal Cost: ' + str(
TotalCost) + '\n\n')
#######################################################################################
searchAlgo = SearchAlgorithms('S,.,.,#,.,.,. .,#,.,.,.,#,. .,#,.,.,.,.,. .,.,#,#,.,.,. #,.,#,E,.,#,.',
[0, 15, 2, 100, 60, 35, 30, 3
, 100, 2, 15, 60, 100, 30, 2
, 100, 2, 2, 2, 40, 30, 2, 2
, 100, 100, 3, 15, 30, 100, 2
, 100, 0, 2, 100, 30])
path, fullPath, TotalCost = searchAlgo.AStarEuclideanHeuristic()
print('**ASTAR with Euclidean Heuristic **\nPath is: ' + str(path) + '\nFull Path is: ' + str(
fullPath) + '\nTotal Cost: ' + str(TotalCost) + '\n\n')
#######################################################################################
searchAlgo = SearchAlgorithms('S,.,.,#,.,.,. .,#,.,.,.,#,. .,#,.,.,.,.,. .,.,#,#,.,.,. #,.,#,E,.,#,.')
path, fullPath, TotalCost = searchAlgo.AStarManhattanHeuristic()
print('**ASTAR with Manhattan Heuristic **\nPath is: ' + str(path) + '\nFull Path is: ' + str(
fullPath) + '\nTotal Cost: ' + str(TotalCost) + '\n\n')
main()