-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniformed_search_algo.py
304 lines (280 loc) · 12.8 KB
/
uniformed_search_algo.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
from Puzzle_board import *
import copy
import heapq
moves = ['Down', 'Left', 'Up', 'Right']
def generate_required_board1(dimension):
gen_board = []
for i in range(dimension):
gen_board.append(list())
size = dimension * dimension
for i in range(size):
gen_board[int(i / dimension)].append(str(i))
return gen_board
def generate_required_board2(dimension):
gen_board = []
for i in range(dimension):
gen_board.append(list())
size = dimension * dimension
for i in range(size):
gen_board[int(i / dimension)].append(str((i + 1) % size))
return gen_board
def get_input_from_file(filename):
file_lines = None
try:
file = open(filename, 'r')
file_lines = file.readlines()
file.close()
except Exception as e:
print("Exception Occured:", str(e))
finally:
return file_lines
def write_output_to_file(filename, writing_str, mode='w'):
try:
file = open(filename, mode)
file.write(writing_str)
file.close()
except Exception as e:
print("Failed to write", str(e))
def is_present(board, lst):
for temp_board in lst:
if temp_board.is_boards_equal(board):
return True
return False
def generate_path_str(path, found):
os = ''
if found:
count = len(path)
for i in path:
count = count - 1
if i == 'D':
os = os + moves[0]
elif i == 'R':
os = os + moves[1]
elif i == 'U':
os = os + moves[2]
elif i == 'L':
os = os + moves[3]
if count != 0:
os = os + '-> '
else:
os = os + "No path found"
return os
def dfs_search(line):
ls = line.rstrip().split(',')
ls_visited_states = list()
ls_new_unvisited_st = list()
board = Puzzle_board(ls)
req_board1 = generate_required_board1(int(m.sqrt(len(ls))))
req_board2 = generate_required_board2(int(m.sqrt(len(ls))))
ls_new_unvisited_st.append(copy.deepcopy(board)) # maintains the list of non explored state
ls_visited_states.append(copy.deepcopy(board)) #maintains the list of all discovered states
path = ''
if board.is_boards_equal(req_board1) or board.is_boards_equal(req_board2):
return path
found = False
while len(ls_new_unvisited_st) != 0 and not found:
board = ls_new_unvisited_st.pop() # implementing the stack structure by popping out last element(LIFO)
temp_board = copy.deepcopy(board)
temp_board.move_right()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
temp_board = copy.deepcopy(board)
temp_board.move_down()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
temp_board = copy.deepcopy(board)
temp_board.move_left()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
temp_board = copy.deepcopy(board)
temp_board.move_up()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
return generate_path_str(path, found)
def IDS_search(line): #implenting dfs by limiting its depths on each iteration
ls = line.rstrip().split(',')
req_board1 = generate_required_board1(int(m.sqrt(len(ls))))
req_board2 = generate_required_board2(int(m.sqrt(len(ls))))
found = False
path = ''
iter_level = -1
while not found:
ls_visited_states = list()
ls_new_unvisited_st = list()
board = Puzzle_board(ls)
if board.is_boards_equal(req_board1) or board.is_boards_equal(req_board2):
return path
ls_new_unvisited_st.append((copy.deepcopy(board), 0))
ls_visited_states.append(copy.deepcopy(board))
iter_level = iter_level + 1
level = 0
level_flag = False
while len(ls_new_unvisited_st) != 0 and not found:
board, level = ls_new_unvisited_st.pop()
if iter_level <= level:
level_flag = True
if not level_flag:
temp_board = copy.deepcopy(board)
temp_board.move_right()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append((copy.deepcopy(temp_board), level + 1))
temp_board = copy.deepcopy(board)
temp_board.move_left()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append((copy.deepcopy(temp_board), level + 1))
temp_board = copy.deepcopy(board)
temp_board.move_up()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append((copy.deepcopy(temp_board), level + 1))
temp_board = copy.deepcopy(board)
temp_board.move_down()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append((copy.deepcopy(temp_board), level + 1))
level_flag = False
return generate_path_str(path, found)
def bfs_search(line):
ls = line.rstrip().split(',')
req_board1 = generate_required_board1(int(m.sqrt(len(ls))))
req_board2 = generate_required_board2(int(m.sqrt(len(ls))))
ls_visited_states = list()
ls_new_unvisited_st = list()
board = Puzzle_board(ls)
ls_new_unvisited_st.append(copy.deepcopy(board))
ls_visited_states.append(copy.deepcopy(board))
path = ''
if board.is_boards_equal(req_board1) or board.is_boards_equal(req_board2):
return path
found = False
while len(ls_new_unvisited_st) != 0 and not found:
board = ls_new_unvisited_st[0]
ls_new_unvisited_st.pop(0) #implenting the stack structure by popping the first Element
temp_board = copy.deepcopy(board)
temp_board.move_down()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
temp_board = copy.deepcopy(board)
temp_board.move_right()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
temp_board = copy.deepcopy(board)
temp_board.move_up()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
temp_board = copy.deepcopy(board)
temp_board.move_left()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
ls_new_unvisited_st.append(copy.deepcopy(temp_board))
return generate_path_str(path, found)
def UCS_search(line):
path = ''
found = False
ls = line.rstrip().split(',')
req_board1 = generate_required_board1(int(m.sqrt(len(ls))))
req_board2 = generate_required_board2(int(m.sqrt(len(ls))))
ls_visited_states = []
ls_new_unvisited_st = []
board = Puzzle_board(ls)
if board.is_boards_equal(req_board1) or board.is_boards_equal(req_board2):
return path
level_no = 0
heapq.heappush(ls_new_unvisited_st, (level_no, copy.deepcopy(board)))
ls_visited_states.append(copy.deepcopy(board))
while len(ls_new_unvisited_st) != 0 and not found:
level_no, board = heapq.heappop(ls_new_unvisited_st) #get the board state from queue on the basis of level no.
temp_board = copy.deepcopy(board)
temp_board.move_down()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
heapq.heappush(ls_new_unvisited_st, (level_no + 1, copy.deepcopy(temp_board)))
temp_board = copy.deepcopy(board)
temp_board.move_right()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
heapq.heappush(ls_new_unvisited_st, (level_no + 1, copy.deepcopy(temp_board)))
temp_board = copy.deepcopy(board)
temp_board.move_up()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
heapq.heappush(ls_new_unvisited_st, (level_no + 1, copy.deepcopy(temp_board)))
temp_board = copy.deepcopy(board)
temp_board.move_left()
if not is_present(temp_board.board, ls_visited_states):
if temp_board.is_boards_equal(req_board1) or temp_board.is_boards_equal(req_board2):
path = temp_board.path
found = True
break
ls_visited_states.append(copy.deepcopy(temp_board))
heapq.heappush(ls_new_unvisited_st, (level_no + 1, copy.deepcopy(temp_board)))
return generate_path_str(path, found)