-
Notifications
You must be signed in to change notification settings - Fork 0
/
a2.py
240 lines (212 loc) · 8.5 KB
/
a2.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
import numpy as np
test = 4
if test == 0:
# Fully explored count 65
# Moves to Goal 14
start = np.array([0, 0])
goal = np.array([5, 9])
grid = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], # Row 0
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 1
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 2
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 3
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 4
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 5
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 6
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 7
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) # Row 9
# Columns 0 1 2 3 4 5 6 7 8 9
elif test == 1:
# Fully explored count 7
# Moves to Goal 4
start = np.array([9, 9])
goal = np.array([5, 9])
grid = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], # Row 0
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0], # Row 1
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], # Row 2
[0, 1, 1, 0, 1, 1, 1, 1, 1, 0], # Row 3
[0, 1, 0, 1, 0, 0, 0, 0, 1, 1], # Row 4
[0, 1, 0, 1, 0, 0, 1, 0, 0, 0], # Row 5
[0, 1, 1, 0, 1, 1, 1, 0, 0, 0], # Row 6
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0], # Row 7
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0], # Row 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) # Row 9
# Columns 0 1 2 3 4 5 6 7 8 9
elif test == 2:
# Fully explored count 59
# Moves to Goal 9
start = np.array([2, 4])
goal = np.array([9, 6])
grid = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], # Row 0
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 1
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 2
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 3
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 4
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 5
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 6
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 7
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # Row 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) # Row 9
# Columns 0 1 2 3 4 5 6 7 8 9
elif test == 3:
# Fully explored count 62
# Moves to Goal 22
start = np.array([0, 0])
goal = np.array([5, 9])
grid = np.array([[0, 1, 0, 0, 0, 0, 0, 1, 0, 0], # Row 0
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 1
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 2
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 3
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 4
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], # Row 5
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 6
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 7
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0], # Row 8
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]) # Row 9
# Columns 0 1 2 3 4 5 6 7 8 9
else:
# Fully explored count 67
# Moves to Goal 33
start = np.array([0, 0])
goal = np.array([7, 2])
grid = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], # Row 0
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0], # Row 1
[0, 1, 1, 0, 0, 0, 1, 0, 0, 0], # Row 2
[0, 1, 1, 0, 0, 0, 1, 0, 0, 0], # Row 3
[0, 1, 1, 0, 0, 1, 0, 0, 0, 0], # Row 4
[0, 1, 1, 0, 1, 0, 0, 1, 1, 0], # Row 5
[0, 1, 1, 1, 0, 0, 0, 1, 0, 0], # Row 6
[0, 1, 0, 0, 0, 1, 1, 1, 0, 1], # Row 7
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0], # Row 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) # Row 9
# Columns 0 1 2 3 4 5 6 7 8 9
# Copies of grid to be used for visualizing results.
path = np.zeros([len(grid), len(grid)], dtype=int)
best_path = np.zeros([len(grid), len(grid)], dtype=int)
h_grid = np.zeros([len(grid), len(grid)], dtype=float)
class AStarSearch:
def __init__(self, start, goal, grid, path, h_grid):
self.pos = start
self.pos_str = str(start)
self.pos_depth = 0
self.goal_str = str(goal)
self.explored = {}
self.not_explored = {}
self.not_explored[str(start)] = 0
self.grid = grid
self.path = path
self.h_grid = h_grid
# START - Student Section
def get_possible_moves(self):
potential_moves = self.generate_potential_moves(self.pos)
for move in potential_moves:
# Check if potential move is valid.
if not self.valid_move(move):
continue
# Check if move has already been explored.
if (str(move) in self.explored) or (str(move) in self.not_explored):
continue
# Visualize the Heuristic Grid
self.h_grid[move[0], move[1]] = self.heuristic(move) + self.pos_depth + 1
self.not_explored[str(move)] = self.h_grid[move[0], move[1]]
# Since all next possible moves have been determined,
# consider current location explored.
self.explored[self.pos_str] = self.pos_depth
self.path[self.pos[0], self.pos[1]] = self.pos_depth
self.not_explored.pop(self.pos_str, None)
return True
def goal_found(self):
if self.goal_str in self.not_explored:
# Add goal to path.
self.pos = self.string_to_array(self.goal_str)
self.pos_depth = self.not_explored.pop(self.goal_str)
self.path[self.pos[0], self.pos[1]] = self.pos_depth
return True
return False
def explore_next_move(self):
# Determine next move to explore.
sorted_not_explored = sorted(
self.not_explored,
key=self.not_explored.get,
reverse=False)
# Determine the pos and depth of next move.
self.pos_str = sorted_not_explored[0]
self.pos = self.string_to_array(self.pos_str)
self.pos_depth = self.not_explored.pop(self.pos_str) - self.heuristic(self.pos)
# Write depth of next move onto path.
self.path[self.pos[0], self.pos[1]] = round(self.pos_depth, 1)
return True
def heuristic(self, move):
distance = move - goal
distance_squared = distance * distance
answer = np.sqrt(sum(distance_squared))
return round(answer, 1)
# END - Student Section
# Helper Functions
def generate_potential_moves(self, pos):
u = np.array([-1, 0])
d = np.array([1, 0])
l = np.array([0, -1])
r = np.array([0, 1])
potential_moves = [pos + u, pos + d, pos + l, pos + r]
# Students, uncomment the line below, what happens?
#potential_moves += [pos + u+r, pos + u+l, pos + d+r, pos + d+l]
return potential_moves
def valid_move(self, move):
# Check if out of boundary.
if (move[0] < 0) or (move[0] > 9):
return False
if (move[1] < 0) or (move[1] > 9):
return False
# Check if wall or obstacle exists.
if self.grid[move[0], move[1]] == 1:
return False
return True
def string_to_array(self, string):
array = [int(string[1]), int(string[3])]
return np.array(array)
# Init
astar = AStarSearch(start, goal, grid, path, h_grid)
while True:
# Determine next possible moves.
astar.get_possible_moves()
if astar.goal_found():
break
astar.explore_next_move()
print('')
print('Heuristic Grid')
print('--------------')
print(h_grid)
print('')
print('')
print('Explored Path')
print('-------------')
path[start[0], start[1]] = 9999
print(path)
print('')
print('Fully explored count ' + str(np.count_nonzero(path)))
def find_best_path(pos):
best_path[pos[0], pos[1]] = 1
h_pos = path[pos[0], pos[1]]
if h_pos == 1:
return 1
potential_moves = astar.generate_potential_moves(pos)
best_move = [0, 0]
best_h = h_pos
for move in potential_moves:
if not astar.valid_move(move):
continue
h_move = path[move[0], move[1]]
if h_move <= best_h and h_move != 0:
best_h = h_move
best_move = move
return find_best_path(best_move) + 1
goal_count = find_best_path(goal)
best_path[start[0], start[1]] = 9999
print('')
print('Best Path To Goal')
print('-----------------')
print(best_path)
print('')
print('Moves to Goal: ' + str(goal_count))
print('')