forked from ribal-aladeeb/state-space-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
318 lines (255 loc) · 11.1 KB
/
search.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
import numpy as np
from board import Board
from node import Node
from queue import PriorityQueue
import os
import time
import heuristics
import shutil
def uniform_cost(board: Board, timeout=60) -> Node:
start_time = time.time()
elapsed = start_time
root = Node(is_root=True, board=board)
open_list = PriorityQueue() # even though not python list, naming is kept for consistency with state space search theory
closed_list = {} # even though not python list, naming is kept for consistency with state space search theory
search_space = [] # Used to reconstructed the order in which nodes were traversed
created_nodes = 1
open_list.put((root.total_cost, created_nodes, root))
current_node = None
visited_nodes = 0
while not open_list.empty():
_, _, current_node = open_list.get()
visited_nodes += 1
if current_node.is_goal_state():
elapsed = round(time.time() - start_time, 2)
return {
'algo': 'UCS',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': True,
'message': 'solution found'
}
#--------This is only related to time execution--------#
if visited_nodes % 10000 == 0 and timeout > 0:
elapsed = round(time.time() - start_time, 2)
if elapsed > timeout:
return {
'algo': 'UCS',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': False,
'message': f"timeout after {timeout} seconds"
}
#--------This is only related to time execution--------#
hashed_node = tuple(current_node.board.puzzle.flatten())
if hashed_node in closed_list:
if closed_list[hashed_node].total_cost < current_node.total_cost: # we previously got to this configuration with lower cost than we do right now so ignore
continue
closed_list[hashed_node] = current_node
search_space.append(current_node)
for s in current_node.successors():
created_nodes += 1
open_list.put((s.total_cost, created_nodes, s))
return {
'algo': 'UCS',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': False,
'message': 'no more nodes in open list'
}
def greedy_best_first(board: Board, H, timeout=60) -> Node:
start_time = time.time()
elapsed = start_time
root = Node(is_root=True, board=board, heuristic_func=H)
# goal_states = root.board.generate_goal_states()
open_list = PriorityQueue() # even though not python list, naming is kept for consistency with state space search theory
closed_list = set() # even though not python list, naming is kept for consistency with state space search theory
search_space = [] # Used to reconstructed the order in which nodes were traversed
created_nodes = 1
open_list.put((root.h_n, created_nodes, root))
current_node = None
visited_nodes = 0
while not open_list.empty():
_, _, current_node = open_list.get()
visited_nodes += 1
if current_node.is_goal_state():
elapsed = round(time.time() - start_time, 2)
return {
'algo': 'GBF',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': True,
'message': 'solution found'
}
#--------This is only related to time execution--------#
if visited_nodes % 10000 == 0 and timeout > 0:
elapsed = round(time.time() - start_time, 2)
if elapsed > timeout:
return {
'algo': 'GBF',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': False,
'message': f"timeout after {timeout} seconds"
}
#--------This is only related to time execution--------#
hashed_node = tuple(current_node.board.puzzle.flatten())
if hashed_node in closed_list:
continue
closed_list.add(hashed_node)
search_space.append(current_node)
for s in current_node.successors(heuristic_func=H):
created_nodes += 1
open_list.put((s.h_n, created_nodes, s))
return {
'algo': 'GBF',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': False,
'message': 'no more nodes in open list'
}
def a_star(board: Board, H, timeout=60) -> Node:
start_time = time.time()
elapsed = start_time
root = Node(is_root=True, board=board, heuristic_func=H)
open_list = PriorityQueue() # even though not python list, naming is kept for consistency with state space search theory
closed_list = {} # even though not python list, naming is kept for consistency with state space search theory
search_space = [] # Used to reconstructed the order in which nodes were traversed
created_nodes = 1
open_list.put((root.f_n, created_nodes, root))
current_node = None
visited_nodes = 0
while not open_list.empty():
_, _, current_node = open_list.get()
visited_nodes += 1
if current_node.is_goal_state():
elapsed = round(time.time() - start_time, 2)
return {
'algo': 'A*',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': True,
'message': 'solution found'
}
#--------This is only related to time execution--------#
if visited_nodes % 10000 == 0 and timeout > 0:
elapsed = round(time.time() - start_time, 2)
if elapsed > timeout:
return {
'algo': 'A*',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': False,
'message': f"timeout after {timeout} seconds"
}
#--------This is only related to time execution--------#
hashed_node = tuple(current_node.board.puzzle.flatten())
if hashed_node in closed_list:
if closed_list[hashed_node].g_n <= current_node.g_n: # we previously got to this configuration with lower f_n than we do right now so ignore
visited_nodes -= 1 # we don't consider a node visited if you don't expand it (i.e generate it's children)
continue
closed_list[hashed_node] = current_node
search_space.append(current_node)
for s in current_node.successors(heuristic_func=H):
created_nodes += 1
open_list.put((s.f_n, created_nodes, s))
return {
'algo': 'A*',
'current_node': current_node,
'runtime': elapsed,
'visited_nodes': visited_nodes,
'created_nodes': created_nodes,
'search_space': search_space,
'success': False,
'message': 'no more nodes in open list'
}
def generate_search_string(search_space: list, algo: str) -> str:
'''
Generates the search string given closed_list (actually a dict) which is
returned by a search algorithm.
'''
search_str = ''
algo = algo.upper()
for node in search_space:
board_as_str = node.board.line_representation()
if node.is_root:
search_str += f'0 0 0 {board_as_str}\n'
continue
f = node.f_n if algo == 'A*' else 0
g = 0 if algo == 'GBF' else node.g_n
h = 0 if algo == 'UCS' else node.h_n
search_str += f'{f} {g} {h} {board_as_str}\n'
return search_str
def write_results_to_disk(solution: str, search: str, algo_name: str, puzzle_number: int, heuristic: str = None) -> bool:
'''
Writes the contents of the solution and search strings for puzzle_number using algo_name and heurisic
'''
algo_name = algo_name.lower()
heuristic_string = f'_{heuristic}_' if heuristic != None else '_'
sol_name = f'{puzzle_number}_{algo_name}{heuristic_string}solution.txt'
search_name = f'{puzzle_number}_{algo_name}{heuristic_string}search.txt'
result_dir = 'results'
with open(os.path.join(result_dir, sol_name), mode='w') as f:
f.write(solution)
with open(os.path.join(result_dir, search_name), mode='w') as f:
f.write(search)
return True
def main(chosen_heurisitics=[heuristics.manhattan_distance, heuristics.row_col_out_of_place]):
puzzles = [
np.array([
[7, 0, 1, 6],
[2, 5, 3, 4]
]),
np.array([
[0, 1, 2, 3],
[4, 5, 6, 7]
])
]
if os.path.isdir("results"):
shutil.rmtree("results")
os.makedirs("results")
for j, p in enumerate(puzzles):
start_puzzle: Board = Board(puzzle=p)
print('*'*80)
print(f'Puzzle {j+1}:\n{start_puzzle}')
for i in range(len(chosen_heurisitics)):
experiments = [
uniform_cost(start_puzzle),
greedy_best_first(start_puzzle, H=chosen_heurisitics[i]),
a_star(start_puzzle, H=chosen_heurisitics[i])
]
print(f'\nUsing heuristic \"{chosen_heurisitics[i].__name__}\":')
for result in experiments:
logged = f"\n\t{result['algo']} visited {result['visited_nodes']} nodes, created {result['created_nodes']} and "
logged += f"found with cost = {result['current_node'].total_cost} in {result['runtime']} seconds:"
logged += f"\n{result['current_node'].board}\n"
print(logged)
solution_str = result['current_node'].generate_solution_string(result['algo'])
search_str = generate_search_string(result['search_space'], result['algo'])
write_results_to_disk(solution_str, search_str, result['algo'], 0, f'h{i+1}')
if __name__ == "__main__":
main()