forked from ArNayyeri/SearchProject_SortIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.py
290 lines (267 loc) · 10.9 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
import State
from Heuristic import Heuristic
from Solution import Solution
from Problem import Problem
from datetime import datetime
class Search:
Gn = {}
states_hash = {}
Fn = {}
ids_limit = 1
@staticmethod
def bfs(prb: Problem) -> Solution: # this method get a first state of Problem and do bfs for find solution if no
# solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
queue.append(state)
while len(queue) > 0:
state = queue.pop(0)
neighbors = prb.successor(state)
for c in neighbors:
if prb.is_goal(c):
return Solution(c, prb, start_time)
queue.append(c)
return None
@staticmethod
def ucs(prb: Problem) -> Solution: # this method get a first state of Problem and do ucs for find solution if no
# solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
Search.Gn[state.__hash__()] = 0
queue.append(state)
while len(queue) > 0:
state = queue.pop(0)
neighbors = prb.successor(state)
for i in neighbors:
Search.gn(state, i, 1)
Search.add_hash(i)
neighbors = Search.sort_neighbors(neighbors)
for c in neighbors:
print(c.__hash__(), Search.Gn[c.__hash__()])
if prb.is_goal(c):
return Solution(c, prb, start_time)
queue.append(c)
return None
@staticmethod
def ids(prb: Problem) -> Solution: # this method get a first state of Problem and do ids for find solution if no
# solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
Search.Gn[state.__hash__()] = 0
queue.append(state)
while len(queue) > 0:
state = queue.pop()
if prb.is_goal(state):
return Solution(state, prb, start_time)
neighbors = prb.successor(state)
for c in neighbors:
Search.gn(state, c, 1)
print(Search.Gn[c.__hash__()], Search.ids_limit)
if c.__hash__() not in Search.states_hash.keys() and Search.Gn[c.__hash__()] <= Search.ids_limit:
queue.append(c)
Search.add_hash(c)
Search.ids_limit += 1
Search.Gn = {}
Search.states_hash = {}
return Search.ids(prb)
@staticmethod
def dfs(prb: Problem) -> Solution: # this method get a first state of Problem and do dfs for find solution if no
# solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
queue.append(state)
while len(queue) > 0:
state = queue.pop()
# print(prb.heuristic(state))
if prb.is_goal(state):
return Solution(state, prb, start_time)
neighbors = prb.successor(state)
for c in neighbors:
if c.__hash__() not in Search.states_hash.keys():
queue.append(c)
Search.add_hash(c)
return None
@staticmethod
def dls(prb: Problem) -> Solution: # this method get a first state of Problem and do dls for find solution if no
# solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
Search.Gn[state.__hash__()] = 0
limited_depth = 199
queue.append(state)
while len(queue) > 0:
state = queue.pop()
if prb.is_goal(state):
# printing all states cost to final state and the cost of final state and returning it
print(Search.Gn, state.__hash__(), Search.Gn[state.__hash__()])
return Solution(state, prb, start_time)
neighbors = prb.successor(state)
for c in neighbors:
Search.gn(state, c, 1)
if c.__hash__() not in Search.states_hash.keys() and Search.Gn[c.__hash__()] <= limited_depth:
queue.append(c)
Search.add_hash(c)
return None
@staticmethod
def astar(prb: Problem) -> Solution: # this method get a first state of Problem and do astar for find solution if
# no solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
Search.Gn[state.__hash__()] = 0
hn = Heuristic(state)
queue.append(state)
while len(queue) > 0:
state = queue.pop(0)
neighbors = prb.successor(state)
for i in neighbors:
Search.gn(state, i, 1)
Search.fn(i, hn)
Search.add_hash(i)
neighbors = Search.sort_neighbors_fn(neighbors)
for c in neighbors:
print(c.__hash__(), Search.Fn[c.__hash__()])
if prb.is_goal(c):
return Solution(c, prb, start_time)
queue.append(c)
queue = Search.sort_neighbors_fn(queue)
return None
@staticmethod
def ida(prb: Problem) -> Solution: # this method get a first state of Problem and do ida for find solution if
# no solution is find return None else return the solution
start_time = datetime.now()
queue = []
unexpanded = {}
visited_hash = {}
state = prb.initState
print(prb.initState.__hash__())
Search.Gn[state.__hash__()] = 0
hn = Heuristic(state)
Search.fn(state, hn)
cut_off = Search.Fn[state.__hash__()]
queue.append(state)
while len(queue) > 0:
state = queue.pop(0)
neighbors = prb.successor(state)
for i in neighbors:
Search.gn(state, i, 1)
Search.fn(i, hn)
Search.add_hash(i)
neighbors = Search.sort_neighbors_fn(neighbors)
for c in neighbors:
if Search.Fn[c.__hash__()] <= cut_off:
print(c.__hash__(), cut_off)
if prb.is_goal(c):
return Solution(c, prb, start_time)
visited_hash[c.__hash__()] = c
queue.append(c)
queue = Search.sort_neighbors_fn(queue)
elif not c.__hash__() in visited_hash.keys():
unexpanded[c.__hash__()] = Search.Fn[c.__hash__()]
if len(queue) == 0:
print(prb.initState.__hash__())
queue.append(prb.initState)
cut_off = min(unexpanded.values())
Search.Fn = {}
Search.Gn = {}
unexpanded = {}
Search.Gn[prb.initState.__hash__()] = 0
print(cut_off)
return None
parent = {}
second_best = {}
@staticmethod
def rbfs(prb: Problem) -> Solution: # this method get a first state of Problem and do rbfs for find solution if no
# solution is find return None else return the solution
start_time = datetime.now()
queue = []
state = prb.initState
Search.Gn[state.__hash__()] = 0
hn = Heuristic(state)
queue.append(state)
while len(queue) > 0:
state = queue.pop(0)
neighbors = prb.successor(state)
for i in neighbors:
Search.gn(state, i, 1)
Search.fn(i, hn)
Search.add_hash(i)
neighbors = Search.sort_neighbors_fn(neighbors)
Search.parent[neighbors[1].__hash__()] = neighbors[0]
Search.second_best[neighbors[1].__hash__()] = Search.Fn[neighbors[1].__hash__()]
for c in neighbors:
# for i in c.pipes:
# print(i.stack)
# print('------')
if Search.Fn[c.__hash__()] <= min(Search.second_best.values()):
if prb.is_goal(c):
return Solution(c, prb, start_time)
queue.append(c)
queue = Search.sort_neighbors_fn(queue)
else:
min_second_best = min(Search.second_best.values())
min_second_key = ''
for key in Search.second_best.keys():
if min_second_best == Search.second_best[key]:
min_second_key = key
Search.Fn[Search.parent[min_second_key].__hash__()] = Search.Fn[c.__hash__()]
queue.append(Search.states_hash[min_second_key])
del Search.second_best[min_second_key]
break
return None
@staticmethod
# receives the parent and child node and increments the child value based on the parent, and it's value
# note that for the first node you should set the value yourself
def gn(parent, child, child_cost):
Gn = Search.Gn
Gn[child.__hash__()] = Gn[parent.__hash__()] + child_cost
Search.Gn = Gn
return Gn[child.__hash__()]
@staticmethod
def add_hash(state): # adds a state to a dict base on hash as key & object as value
states_hash = Search.states_hash
if state.__hash__() in states_hash.keys():
return False
states_hash[state.__hash__()] = state
Search.states_hash = states_hash
return True
# sorts the neighbors based on gn and returns the neighbors
@staticmethod
def sort_neighbors(neighbors): # sorts the neighbors based on their gn and returns objects of states
gn_neighbors = {}
for s in neighbors:
gn_neighbors[s.__hash__()] = Search.Gn[s.__hash__()]
neighbors = []
while len(gn_neighbors.values()) > 0:
min_value = min(gn_neighbors.values())
for s in gn_neighbors:
if gn_neighbors[s] == min_value:
neighbors.append(Search.states_hash[s])
del gn_neighbors[s]
break
return neighbors
# sorts the neighbors based on fn and returns the neighbors
@staticmethod
def sort_neighbors_fn(neighbors):
fn_neighbors = {}
for s in neighbors:
fn_neighbors[s.__hash__()] = Search.Fn[s.__hash__()]
neighbors = []
while len(fn_neighbors.values()) > 0:
min_value = min(fn_neighbors.values())
for s in fn_neighbors:
if fn_neighbors[s] == min_value:
neighbors.append(Search.states_hash[s])
del fn_neighbors[s]
break
return neighbors
@staticmethod
def fn(state, hn):
gn = Search.Gn[state.__hash__()]
Search.Fn[state.__hash__()] = gn + hn.heuristic(state)
pass