-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze_Solver.py
80 lines (57 loc) · 2.61 KB
/
Maze_Solver.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
import matplotlib.pyplot as plt
import Maze
import Hybrid_A_star as A_star
import timeit
import numpy as np
import pdb
def get_astar_path(sx,sy,ex,ey,mazeX,mazeY,sparsity):
obs,arr=Maze.creat_map([sx,sy],[ex,ey],mazeX,mazeY,sparsity)
plt.plot(ex,ey,'xm')
plt.plot(sx,sy,'xm')
path_planner=A_star.A_Star(obs,[sx,sy,0],[ex, ey,0])
# Plot Mesh
plt.pcolormesh(arr)
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
start_time=timeit.default_timer()
path=path_planner.A_Star()
# If Path found plot the path
if path!=None:
nppath=np.asarray(path)
end_time=timeit.default_timer()
print("timing",end_time-start_time)
plt.plot(nppath[:,0],nppath[:,1],'r-')
plt.savefig('./frames/astar_final')
plt.show()
else:
print("path not found")
plt.show()
return path
if __name__ == '__main__':
maze_sizeX=input("Enter Maze X size. Odd values allowed: ")
maze_sizeY=input("Enter Maze Y size. Odd values allowed: ")
sparsity_level=input("Sparsity level can be selected as even numbers. Ex 2,4,6...: ")
maze_size=np.array([int(maze_sizeX),int(maze_sizeY)])
sparsity_level=int(sparsity_level)
assert maze_size.shape==(2,),"Incorrect Shape"
assert maze_size[0]%2 ==1, "Incorrect size value"
assert maze_size[1]%2 ==1, "Incorrect size value"
min_size=min(maze_size[0],maze_size[1])
assert sparsity_level<(min_size-1), "sparsity value too high "
assert sparsity_level%2==0, "odd number sparsity not supported"
assert sparsity_level>0," sparsity level 0 or negative"
startX=input("Enter start X. Even values allowed: ")
startY=input("Enter start Y. Even values allowed: ")
start_node=np.array([int(startX),int(startY)])
assert start_node.shape==(2,),"Incorrect Shape"
assert start_node[0]%2 ==0, "Incorrect size value"
assert start_node[1]%2 ==0, "Incorrect size value"
endX=input("Enter end X. Even values allowed: ")
endY=input("Enter end Y. Even values allowed: ")
end_node=np.array([int(endX),int(endY)])
assert end_node.shape==(2,),"Incorrect Shape"
assert end_node[0]%2 ==0, "Incorrect size value"
assert end_node[1]%2 ==0, "Incorrect size value"
get_astar_path(sx=start_node[0],sy=start_node[1],ex=end_node[0],ey=end_node[1],mazeX=maze_size[0],mazeY=maze_size[1],sparsity=sparsity_level)