-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysearch.py
253 lines (196 loc) · 7.58 KB
/
mysearch.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
from search import Problem
from search import breadth_first_tree_search
from search import depth_first_tree_search
from search import iterative_deepening_search
from search import astar_search
import numpy as np
import matplotlib.pyplot as plt
import time
import math
class Point(object):
def __init__(self,x,y):
self.x = x
self.y = y
# Whatever tiebraker
def __lt__(self,node):
return self.x < node.x
class MatrixProblem(Problem):
""" Problem of finding a certain spot in a matrix """
def __init__(self, initial, goal, matrix, heuristic = 1):
Problem.__init__(self, initial, goal)
self.matrix = matrix
self.heuristic = heuristic
def actions(self, state):
a = []
if self.matrix.item(state.x+1,state.y) != -1 and self.matrix.item(state.x+1,state.y) != 1:
a.append(Point(state.x+1,state.y))
if self.matrix.item(state.x-1,state.y) != -1 and self.matrix.item(state.x-1,state.y) != 1:
a.append(Point(state.x-1,state.y))
if self.matrix.item(state.x,state.y+1) != -1 and self.matrix.item(state.x,state.y+1) != 1:
a.append(Point(state.x,state.y+1))
if self.matrix.item(state.x,state.y-1) != -1 and self.matrix.item(state.x,state.y-1) != 1:
a.append(Point(state.x,state.y-1))
# Diagonals
if self.matrix.item(state.x+1,state.y+1) != -1 and self.matrix.item(state.x+1,state.y+1) != 1:
a.append(Point(state.x+1,state.y+1))
if self.matrix.item(state.x+1,state.y-1) != -1 and self.matrix.item(state.x+1,state.y-1) != 1:
a.append(Point(state.x+1,state.y-1))
if self.matrix.item(state.x-1,state.y-1) != -1 and self.matrix.item(state.x-1,state.y-1) != 1:
a.append(Point(state.x-1,state.y-1))
if self.matrix.item(state.x-1,state.y+1) != -1 and self.matrix.item(state.x-1,state.y+1) != 1:
a.append(Point(state.x-1,state.y+1))
return a
def h(self, node):
x1,y1 = matrix_to_coordinates(node.state.x,node.state.y)
x2,y2 = matrix_to_coordinates(self.goal.x,self.goal.y)
if self.heuristic == 1:
return x2-x1
else:
# Euclidian distance
return math.sqrt((x2-x1)**2+(y2-y1)**2)
def goal_test(self, state):
return state.x == goal.x and state.y == goal.y
def result(self, state, action):
state = action
self.matrix[state.x][state.y] = 1
return action
def matprint(mat, fmt="g"):
col_maxes = [max([len(("{:"+fmt+"}").format(x)) for x in col]) for col in mat.T]
for x in mat:
for i, y in enumerate(x):
print(("{:"+str(col_maxes[i])+fmt+"}").format(y), end=" ")
print("")
def matrix_to_coordinates(i,j):
x = j-10
y = 60-i
return x,y
# Matrix representation of the cartesian map
def init_matrix(initial,goal):
matrix = np.zeros((61,81),dtype=int)
matrix[:,10] = -1
matrix[:,70] = -1
matrix[20:60,30] = -1
matrix[0:40,50] = -1
matrix[0,10:70] = -1
matrix[60,10:70] = -1
matrix[initial.x,initial.y] = 1
matrix[goal.x,goal.y] = 2
return matrix
def paint_solution(node,matrix):
for point in node.solution():
matrix[point.x][point.y] = 3
# Transforms our matrix into a plot graph (This takes a long time the more we have to paint so be patient, I was lazy regarding this part)
def plot_result(matrix,initial,goal,title):
plt.axis([-10,70,0,60])
plt.grid()
for i in range(61):
for j in range(81):
if matrix.item(i,j) != 0:
x,y = matrix_to_coordinates(i,j)
if matrix.item(i,j) == -1:
plt.plot([x],[y],marker='.',color='k')
elif matrix.item(i,j) == 1:
plt.plot([x],[y],marker='x',color='c')
else:
plt.plot([x],[y],marker='x',color='r')
x,y = matrix_to_coordinates(initial.x,initial.y)
plt.plot([x],[y],marker='x',color='y')
x,y = matrix_to_coordinates(goal.x,goal.y)
plt.plot([x],[y],marker='x',color='b')
plt.title(title)
plt.show()
plt.clf()
""" Actual script """
# Initial
initial = Point(50,20)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
plot_result(matrix,initial,goal,'Mapa do ambiente inicial')
# BFS
initial = Point(50,20)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix)
start = time.time()
node = breadth_first_tree_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['BFS search time: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca em expansão (t = ', str(round(end - start,4)), ' s)']))
# DFS
initial = Point(50,20)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix)
start = time.time()
node = depth_first_tree_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['DFS time: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca em profundidade (t = ', str(round(end - start,4)), ' s)']))
# BFS close to goal
initial = Point(20,60)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix)
start = time.time()
node = breadth_first_tree_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['BFS easy search time: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca em expansão (t = ', str(round(end - start,4)), ' s)']))
# DFS close to goal
initial = Point(20,60)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix)
start = time.time()
node = depth_first_tree_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['DFS easy time: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca em profundidade (t = ', str(round(end - start,4)), ' s)']))
# A* poor heuristic
initial = Point(50,20)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix)
start = time.time()
node = astar_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['A* time bad heuristic: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca A* (t = ', str(round(end - start,4)), ' s)']))
# A* poor heuristic close to goal
initial = Point(20,60)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix)
start = time.time()
node = astar_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['A* easy time bad heuristic: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca A* (t = ', str(round(end - start,4)), ' s)']))
# A* euclidian heuristic
initial = Point(50,20)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix,2)
start = time.time()
node = astar_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['A* time: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca A* (t = ', str(round(end - start,4)), ' s)']))
# A* euclidian heuristic close to goal
initial = Point(20,60)
goal = Point(10,60)
matrix = init_matrix(initial,goal)
search_problem = MatrixProblem(initial,goal,matrix,2)
start = time.time()
node = astar_search(search_problem)
end = time.time()
paint_solution(node,matrix)
print(''.join(['A* easy time: ',str(end - start)]))
plot_result(matrix,initial,goal,''.join(['Busca A* (t = ', str(round(end - start,4)), ' s)']))