@@ -14,8 +14,12 @@ class SearchAlgorithm():
1414 Just import and call the method you want with the
1515 required arguments.
1616 """
17- def __init__ (self ):
18- pass
17+ def __init__ (self , autoFinish = True ):
18+ """
19+ Auto finish, finishes the search after 500 round, if the search doesn't find the path.
20+
21+ """
22+ self .autoFinish = autoFinish
1923
2024 def searchDFS (self , initialState , GoalState , spaceGraph = []):
2125 ## DONE Successfully
@@ -34,26 +38,25 @@ def searchDFS(self, initialState, GoalState, spaceGraph=[]):
3438
3539 currentState = initialState
3640 self .fringe .append (currentState )
37-
3841 else :
3942 # we don't need to calculate the childs.
4043 pass
41- counter = 0
4244
45+ counter = 0
4346 while True :
47+ if counter == 1000 and self .autoFinish :
48+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
49+ break
4450
4551 # step 1
4652 if currentState not in explored :
4753 explored .append (currentState )
48-
49- print ("\n \n " , "This is currrent state: " , "\n \n " )
54+ print ("\n \n " , "This is currrent state: " , "\n " )
5055 for row in currentState :
5156 print (row )
5257
53- if counter == 50 :
54- break
5558 if currentState == GoalState :
56- print ("The path has been found." )
59+ print (f "The path has been found at round { counter } ." )
5760 return explored
5861 else :
5962 # current state is in explored list
@@ -93,23 +96,22 @@ def searchBFS(self, initialState, GoalState, spaceGraph=[]):
9396 else :
9497 # we don't need to calculate the childs.
9598 pass
96- counter = 0
9799
100+ counter = 0
98101 while True :
102+ if counter == 1000 and self .autoFinish :
103+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
104+ break
99105
100106 # step 1
101107 if currentState not in explored :
102108 explored .append (currentState )
103-
104- print ("\n \n " , "This is currrent state: " , "\n \n " )
109+ print ("\n \n " , "This is currrent state: " , "\n " )
105110 for i in currentState :
106111 print (i )
107-
108- # if counter == 50:
109- # break
110112
111113 if currentState == GoalState :
112- print ("The path has been found." )
114+ print (f "The path has been found at round { counter } ." )
113115 return explored
114116
115117 else :
@@ -127,7 +129,7 @@ def searchBFS(self, initialState, GoalState, spaceGraph=[]):
127129 # Step 3
128130 currentState = self .fringe [0 ]
129131
130- # counter += 1
132+ counter += 1
131133
132134 def searchDLS (self , initialState , GoalState , limitation , spaceGraph = []):
133135 ## DONE Successfully
@@ -154,21 +156,21 @@ def searchDLS(self, initialState, GoalState, limitation, spaceGraph=[]):
154156 counter = 0
155157 self .limitCounter = 0
156158 while True :
159+ if counter == 1000 and self .autoFinish :
160+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
161+ break
157162
158163 if self .limitCounter == self .limitation :
159164 # step 1
160165 if currentState not in explored :
161166 explored .append (currentState )
162167
163- print ("\n \n " , "This is currrent state: " , "\n \n " )
168+ print ("\n \n " , "This is currrent state: " , "\n " )
164169 for row in currentState :
165170 print (row )
166171
167- # if counter == 50:
168- # break
169-
170172 if currentState == GoalState :
171- print ("The path has been found." )
173+ print (f "The path has been found at round { counter } ." )
172174 self .answerFLAG = True
173175 return explored
174176 else :
@@ -194,10 +196,9 @@ def searchDLS(self, initialState, GoalState, limitation, spaceGraph=[]):
194196 for row in currentState :
195197 print (row )
196198
197- if counter == 50 :
198- break
199199 if currentState == GoalState :
200- print ("The path has been found." )
200+ print (f"The path has been found at round { counter } ." )
201+ self .answerFLAG = True
201202 return explored
202203 else :
203204 # current state is in explored list
@@ -221,25 +222,29 @@ def searchDLS(self, initialState, GoalState, limitation, spaceGraph=[]):
221222
222223 print ("This is our fringe state: " )
223224 print (self .fringe )
224- # for i in self.fringe:
225- # print(f"State {i}")
226- # for j in i:
227- # print(j)
228225
229226 # Step 3
230227 currentState = self .fringe [- 1 ]
231228
232-
233- # counter += 1
229+ counter += 1
234230
235231 def searchIDS (self , initialState , GoalState , spaceGraph = []):
236232 ## DONE Successfully
237- depthCounter = 26
233+ depthCounter = 26 # this default number is the st
238234 self .answerFLAG = False
235+ counter = 0
239236 while not self .answerFLAG :
237+ if counter == 100 and self .autoFinish :
238+ print ("\n unfortunately we couldn't find answer after 5000-th round.\n " )
239+ break
240+
241+ # all of the algorithm in the one single line :)
240242 answerIs = self .searchDLS (initialState , GoalState , depthCounter )
243+
244+ depthCounter += 1
245+ counter += 1
241246 else :
242- print (f"This didn't kill you too, the path found at the depth { depthCounter } ;)" )
247+ print (f"This didn't kill you too, the path found at the depth { depthCounter - 1 } ;)" )
243248 return answerIs
244249
245250 def searchUCS (self , initialState , GoalState , spaceGraph = []):
@@ -264,8 +269,12 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
264269 else :
265270 # we don't need to calculate the childs.
266271 pass
272+
267273 counter = 0
268274 while True :
275+ if counter == 1000 and self .autoFinish :
276+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
277+ break
269278
270279 # step 1
271280 if currentState not in explored :
@@ -290,8 +299,10 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
290299 currentStateIndex = self .fringe .index (currentState )
291300 del self .fringe [currentStateIndex ]
292301 self .FatherCost = self .cost [currentStateIndex ]
302+
293303 del self .cost [currentStateIndex ]
294304 print (self .FatherCost )
305+
295306 # now we must calculate the child of the currentstate and add them to the frontier
296307 # In this code our default problem is 8 puzzle problem
297308 self .updateFringe (currentState , explored , GoalState , algorithmType = "UCS" )
@@ -327,6 +338,9 @@ def searchGreedy(self, initialState, GoalState, spaceGraph=[]):
327338 pass
328339 counter = 0
329340 while True :
341+ if counter == 1000 and self .autoFinish :
342+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
343+ break
330344
331345 # step 1
332346 if currentState not in explored :
@@ -387,6 +401,9 @@ def searchAstar(self, initialState, GoalState, spaceGraph=[]):
387401 pass
388402 counter = 0
389403 while True :
404+ if counter == 1000 and self .autoFinish :
405+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
406+ break
390407
391408 # step 1
392409 if currentState not in explored :
@@ -436,9 +453,13 @@ def searchHillClimbing(self, initialState, GoalState, spaceGraph=[]):
436453 # Step 1
437454 currentState = deepcopy (initialState )
438455 explored = [] # We don't use this in hillClimbing. it is for not getting error
456+ counter = 0
439457
440458 # Step 2 - loop
441459 while currentState != GoalState :
460+ if counter == 1000 and self .autoFinish :
461+ print ("\n unfortunately we couldn't find answere after 5000-th round.\n " )
462+ break
442463
443464 # Step 3
444465 self .fringe = []
@@ -470,6 +491,8 @@ def searchHillClimbing(self, initialState, GoalState, spaceGraph=[]):
470491 else :
471492 currentState = self .fringe [minValueIndex ]
472493
494+ counter += 1
495+
473496 else :
474497 return currentState
475498
0 commit comments