-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathana_star.py
477 lines (378 loc) · 12.7 KB
/
ana_star.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
## ANA* Algorithm
# import libraries
from sys import version_info
if version_info.major == 2:
# We are using Python 2.x
from Tkinter import *
import ttk
elif version_info.major == 3:
# We are using Python 3.x
from tkinter import *
from tkinter import ttk
import time as t
import numpy as np
import math
'''
Define the color scheme for visualization. You may change it but I recommend using the same colors
'''
# white (0) is an unvisited node, black(1) is a wall, blue(2) is a visited node
# yellow(3) is for start node, green(4) is for exit node, red (5) is a node on the completed path
colors = {5: "red", 4: "green", 3: "yellow", 2: "blue", 1: "black", 0: "white"}
'''
Opens the maze file and creates tkinter GUI object
'''
# load maze
with open("easy.txt") as text:
maze = [list(line.strip()) for line in text]
[col, row] = np.shape(maze)
# create map
root = Tk()
size = 800 / row
canvas = Canvas(root, width=(size * row), height=(size * col))
root.title("ANA* Algorithm")
class node:
def __init__(self, x, y):
self.color = None
self.x = x
self.y = y
self.e = 99999999 # a very high value
self.f = None
self.g = 99999999 # a very high value
self.h = None # use Euclidean distance as heuristic
self.parent_x = None
self.parent_y = None
self.flag = False
def _set_color_(self, color_val):
self.color = color_val
def update_ghf(self, goal):
self.g = self.parent.g + 1
self.h = cel_dist(self.x, self.y, goal.x, goal.y)
self.f = self.g + self.h
def cal_dist(x1, y1, x2, y2):
dist = (math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)))
if (dist == 0):
return 0.01
return dist
def draw_canvas(canvas, maze):
'''
Change this according to the data structure of your maze variable.
If you are using a node class like the one mentioned below,
You may have to change fill=colors[int(maze[i][j])] to fill=colors[int(maze[i][j].color)]
'''
for i in range(0, col):
for j in range(0, row):
canvas.create_rectangle(j * size, i * size, (j + 1) * size, (i + 1) * size, fill=colors[int(maze[i][j])])
canvas.pack()
# Global variables
G = 99999999
E = 99999999
grid = []
open_list = []
x1 = 0
y1 = 0
x2 = 0
y2 = 0
start_node = (0, 0)
goal_node = (0, 0)
def is_empty(i, j):
if (int(maze[i][j]) == 1):
return 0
else:
return 1
def is_valid(i, j):
if (i >= 0 and i <= row - 1 and j >= 0 and j <= col - 1):
return 1
else:
return 0
def is_goal(i, j, x2, y2):
global grid
if ((grid[i][j].x == x2) & (grid[i][j].y == y2)):
return 1
else:
return 0
def print_node_info(a, b):
global grid
print("(%i, %i)" % (grid[a][b].x, grid[a][b].y))
print("g : ", grid[a][b].g)
print("h : ", grid[a][b].h)
print("f : ", grid[a][b].f)
print("parent_x :", grid[a][b].parent_x)
print("parent_y : ", grid[a][b].parent_y)
print("flag : ", grid[a][b].flag)
def trace_path(start_x, start_y, end_x, end_y):
global grid
path = []
path.append(grid[end_x][end_y])
c = end_x
d = end_y
while (not (is_goal(start_x, start_y, c, d))):
a = grid[c][d].parent_x
b = grid[c][d].parent_y
path.append(grid[a][b])
c = a
d = b
for z in path:
maze[z.x][z.y] = 5
#print("(%i, %i) --> " % (z.x, z.y))
def trace_path_e():
global G
global E
global grid
global open_list
global x1, y1, x2, y2
global success
global start_node
global goal_node
i = x2
j = y2
while (i != x1 and j != y1):
grid[x2][y2]
def prune():
global G
global E
global grid
global open_list
global x1, y1, x2, y2
global success
global start_node
global goal_node
for each_node in open_list:
r = each_node.x
s = each_node.y
if (grid[r][s].g + grid[r][s].h >= G):
open_list.remove(each_node)
def erase_old_path():
global grid
# Iterate through the Grid
for i in range(row):
for j in range(col):
if (maze[i][j] == 5):
maze[i][j] = 2
def update_e():
global G
global E
global grid
global open_list
global x1, y1, x2, y2
global success
global start_node
global goal_node
for each_node in open_list:
p = each_node.x
q = each_node.y
parent_x = grid[p][q].parent_x
parent_y = grid[p][q].parent_y
grid[p][q].g = grid[parent_x][parent_y].g + 1
grid[p][q].h = cal_dist(p, q, x2, y2)
grid[p][q].e = (G - grid[p][q].g) / grid[p][q].h
def improve_solution():
global G
global E
global grid
global open_list
global x1, y1, x2, y2
global success
global start_node
global goal_node
while (len(open_list) > 0):
# Assign max e(s) from all open list nodes as s
max_e = -1
for node_ in open_list:
#print node_.e
if (node_.e > max_e):
max_e = node_.e
i = node_.x
j = node_.y
#print "Selected node with maximum of e(s) from open list is (%i, %i)"%(i, j)
open_list.remove(grid[i][j])
# print("Node removed from open_list (%i, %i)" %(i, j))
if (grid[i][j].e < E):
E = grid[i][j].e
if (is_goal(i, j ,x2, y2)):
G = grid[i][j].g
trace_path(x1, y1, i, j)
success = True
# Successors of grid[i][j]
# front node i, j+1
if (is_valid(i, j + 1) == 1):
if (grid[i][j + 1].flag == False):
if (is_empty(i, j + 1) == 1):
# Initialize the node(i, j+1)
grid[i][j + 1] = node(i, j + 1)
if ( (grid[i][j].g + 1) < grid[i][j + 1].g):
grid[i][j + 1].g = grid[i][j].g + 1
grid[i][j + 1].parent_x = i
grid[i][j + 1].parent_y = j
grid[i][j + 1].color = 2
grid[i][j + 1].flag = True
grid[i][j + 1].h = cal_dist(grid[i][j + 1].x, grid[i][j + 1].y, grid[x2][y2].x, grid[x2][y2].y)
grid[i][j + 1].e = (G - grid[i][j + 1].g) / grid[i][j + 1].h
if (grid[i][j+1].g + grid[i][j+1].h < G):
open_list.append(grid[i][j + 1])
maze[i][j + 1] = 2
# top node i-1, j
if (is_valid(i - 1, j) == 1):
if (grid[i - 1][j].flag == False):
if (is_empty(i - 1, j) == 1):
# Initialize the node(i-1)(j)
grid[i - 1][j] = node(i - 1, j)
if ( (grid[i][j].g + 1) < grid[i - 1][j].g):
grid[i - 1][j].g = grid[i][j].g + 1
grid[i - 1][j].parent_x = i
grid[i - 1][j].parent_y = j
grid[i - 1][j].color = 2
grid[i - 1][j].flag = True
grid[i - 1][j].h = cal_dist(grid[i - 1][j].x, grid[i - 1][j].y, grid[x2][y2].x, grid[x2][y2].y)
grid[i - 1][j].e = (G - grid[i - 1][j].g) / grid[i - 1][j].h
if (grid[i-1][j].g + grid[i-1][j].h < G):
open_list.append(grid[i - 1][j])
maze[i - 1][j] = 2
# left node i, j-1
if (is_valid(i, j - 1) == 1):
if (grid[i][j - 1].flag == False):
if (is_empty(i, j - 1) == 1):
# Initialize the node(i)(j-1)
grid[i][j - 1] = node(i, j - 1)
if ( (grid[i][j].g + 1) < grid[i][j - 1].g):
grid[i][j - 1].g = grid[i][j].g + 1
grid[i][j - 1].parent_x = i
grid[i][j - 1].parent_y = j
grid[i][j - 1].color = 2
grid[i][j - 1].flag = True
grid[i][j - 1].h = cal_dist(grid[i][j - 1].x, grid[i][j - 1].y, grid[x2][y2].x, grid[x2][y2].y)
grid[i][j - 1].e = (G - grid[i][j - 1].g) / grid[i][j - 1].h
if (grid[i][j - 1].g + grid[i][j-1].h < G):
open_list.append(grid[i][j - 1])
maze[i][j - 1] = 2
# bottom node i+1, j
if (is_valid(i + 1, j) == 1):
if (grid[i + 1][j].flag == False):
if (is_empty(i + 1, j) == 1):
# Initialize the node(i+1)(j)
grid[i + 1][j] = node(i + 1, j)
if ( (grid[i][j].g + 1) < grid[i + 1][j].g):
grid[i + 1][j].g = grid[i][j].g + 1
grid[i + 1][j].parent_x = i
grid[i + 1][j].parent_y = j
grid[i + 1][j].color = 2
grid[i + 1][j].flag = True
grid[i + 1][j].h = cal_dist(grid[i + 1][j].x, grid[i + 1][j].y, grid[x2][y2].x, grid[x2][y2].y)
grid[i + 1][j].e = (G - grid[i + 1][j].g) / grid[i + 1][j].h
if (grid[i + 1][j].g + grid[i + 1][j].h < G):
open_list.append(grid[i + 1][j])
maze[i + 1][j] = 2
if (success == True):
break
"""
print("nodes in the open_list")
for z in open_list:
print("(%i, %i)" % (z.x, z.y))
print ("E = ", E)
print ('\n')
print("-------Iteration------")
print("Parent nodes :(%i, %i)" % (open_list[0].x, open_list[0].y))
"""
# Set Parent node
i = open_list[0].x
j = open_list[0].y
# This visualizes the grid. You may remove this and use the functions as you wish.
maze[start_node[0]][start_node[1]] = 3
maze[goal_node[0]][goal_node[1]] = 4
draw_canvas(canvas, maze)
root.update()
return E
def ana_star(maze):
global G
global E
global grid
global open_list
global x1, y1, x2, y2
global success
print ("G : ", G)
print ("E : ", E)
print ("OPEN List : ", len(open_list))
global start_node
global goal_node
# Iniatilising success flag
success = False
# Initialize the Grid
for i in range(row):
x_row = []
for j in range(col):
node_ = node(i, j)
x_row.append(node_)
grid.append(x_row)
"""
# Print the grid
for i in range(row):
for j in range(col):
print("node_info : (%i, %i) "% (grid[i][j].x, grid[i][j].y))
"""
# Set the start and goal node
x1 = start_node[0]
y1 = start_node[1]
x2 = goal_node[0]
y2 = goal_node[1]
# Initialize start node
grid[x1][y1] = node(x1, y1)
grid[x1][y1].g = 0
grid[x1][y1].h = cal_dist(grid[x1][y1].x, grid[x1][y1].y, grid[x2][y2].x, grid[x2][y2].y)
grid[x1][y1].e = (G - grid[x1][y1].g) / (grid[x1][y1].h)
grid[x1][y1].parent_x = x1
grid[x1][y1].parent_y = y1
grid[x1][y1].flag = True
grid[x1][y1]._set_color_(3)
grid[x2][y2] = node(x2, y2)
grid[x2][y2]._set_color_(4)
print ("Start Node : ")
print_node_info(x1, y1)
# start._set_color_(3)
maze[x1][y1] = 3
# goal._set_color_(4)
maze[x2][y2] = 4
# Initilize i, j as x1, y1
i = x1
j = y1
open_list.append(grid[i][j])
print("Node added in open_list (%i, %i)" % (i, j))
# print(is_valid(i, j+1))
print ("-----Start-------")
count = 0
while (len(open_list) != 0):
improve_solution()
t.sleep(0.05)
t.sleep(0.05)
print ("E-suboptimal : ", E)
print ("pruning...")
update_e()
prune()
#erase_old_path()
count = count + 1
print ("Improve Solution Count : ", count)
print ("Length Open List : ", len(open_list))
trace_path(x1, y1, x2, y2)
return
def main():
'''
Define start and goal node. You may change how to define the nodes.
'''
global start_node
global goal_node
start_node = (row - 1, 1)
goal_node = (0, col - 2)
print ("Start node : (%i, %i)" % (row - 1, 1))
print ("Goal node : (%i, %i)" % (0, col - 2))
start = t.process_time()
# run the ana_star algorithm
ana_star(maze)
end = t.process_time()
print("Total time taken : ", end - start)
# Print the grid
for i in range(row):
for j in range(col):
print ("node_info : (%s, %s) " %(str(grid[i][j].x), str(grid[i][j].y)))
print ("Parent of node (%s, %s)" %(str(grid[i][j].parent_x), str(grid[i][j].parent_y)))
print(goal_node)
root.mainloop()
if __name__ == '__main__':
main()