-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.py
147 lines (120 loc) · 2.58 KB
/
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
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
# depth-limited dfs
def dls_solver(maze, limit):
par={};
seen = {};
depth = {};
stack = [];
stack.append(maze.start);
depth[maze.start] = 0;
while(len(stack) != 0):
node = stack[len(stack)-1];
flag = 0;
for i in maze.get_neighbors(node) :
if(((i not in seen) or (seen[i] != True) ) and depth[node] + 1 <= limit ):
seen[i] = True;
par[i] = node;
depth[i] = depth[node] + 1;
stack.append(i);
flag = 1;
if(flag == 0):
stack.pop();
path = [];
tmp = maze.goal;
if( tmp not in par):
return [];
path.append(tmp);
while tmp != maze.start:
tmp = par[tmp];
path.append(tmp);
path.append(tmp);
path.reverse();
return path;
def iterative_dfs_solver(maze):
for l in range(0,maze.nrows*maze.ncols + 1):
if(len(dls_solver(maze,l) ) != 0):
return dls_solver(maze,l);
def dfs_solver(maze):
par={};
seen = {};
stack = [];
stack.append(maze.start);
while(len(stack) != 0):
node = stack[len(stack)-1];
flag = 0;
for i in maze.get_neighbors(node) :
if((i not in seen) or (seen[i] != True) ):
seen[i] = True;
par[i] = node;
stack.append(i);
flag = 1;
if(flag == 0):
stack.pop();
path = [];
tmp = maze.goal;
path.append(tmp);
while tmp != maze.start:
tmp = par[tmp];
path.append(tmp);
path.append(tmp);
path.reverse();
return path;
def bfs_solver(maze):
par={};
seen={};
queue = [];
queue.append(maze.start);
while(len(queue) != 0):
node = queue[0];
for i in maze.get_neighbors(node) :
if((i not in seen) or (seen[i] != True) ):
seen[i] = True;
par[i] = node;
queue.append(i);
queue.pop(0);
path = [];
tmp = maze.goal;
path.append(tmp);
while tmp != maze.start:
tmp = par[tmp];
path.append(tmp);
path.append(tmp);
path.reverse();
return path;
def astar_heuristic(maze, cell):
x = cell[0] - maze.goal[0];
y = cell[1] - maze.goal[1];
return x**2 + y**2;
def astar_solver(maze):
g = {};
seen = {};
par = {};
stack = [];
g[maze.start] = 0;
stack.append(maze.start);
while(len(stack) != 0):
node = stack[len(stack)-1];
min_dist = 10000000;
tmp_node = (-1,-1);
flag = 0;
for i in maze.get_neighbors(node) :
f = g[node] + 1 + astar_heuristic(maze, i);
if((i not in seen) or (seen[i] != True) and f < min_dist):
min_dist = f;
tmp_node = i;
flag = 1;
if(flag == 0):
stack.pop();
else:
seen[tmp_node] = True;
par[tmp_node] = node;
g[tmp_node] = g[node] + 1;
stack.append(tmp_node);
path = [];
tmp = maze.goal;
path.append(tmp);
while tmp != maze.start:
tmp = par[tmp];
path.append(tmp);
path.append(tmp);
path.reverse();
return path;