@@ -65,7 +65,7 @@ def searchDFS(self, initialState, GoalState, spaceGraph=[]):
6565
6666 # now we must calculate the child of the currentstate and add them to the frontier
6767 # In this code our default problem is 8 puzzle problem
68- self .updateFringe (currentState , explored )
68+ self .updateFringe (currentState , explored , GoalState )
6969
7070 # Step 3
7171 currentState = self .fringe [- 1 ]
@@ -122,7 +122,7 @@ def searchBFS(self, initialState, GoalState, spaceGraph=[]):
122122
123123 # now we must calculate the child of the currentstate and add them to the frontier
124124 # In this code our default problem is 8 puzzle problem
125- self .updateFringe (currentState , explored )
125+ self .updateFringe (currentState , explored , GoalState )
126126
127127 # Step 3
128128 currentState = self .fringe [0 ]
@@ -243,7 +243,7 @@ def searchIDS(self, initialState, GoalState, spaceGraph=[]):
243243 return answerIs
244244
245245 def searchUCS (self , initialState , GoalState , spaceGraph = []):
246- ## This algorithms problem is that we can not define the cost for the new nodes
246+ ## Temporary Done
247247 """This algorithm will return the solution path.
248248
249249 Args:
@@ -258,7 +258,7 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
258258 explored = list ()
259259 parentStates = list ()
260260
261- currentState = initialState
261+ currentState = deepcopy ( initialState )
262262 self .fringe .append (currentState )
263263
264264 else :
@@ -271,9 +271,9 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
271271 if currentState not in explored :
272272 explored .append (currentState )
273273
274- print ("\n \n " , "This is currrent state: " , "\n \n " )
275- for i in currentState :
276- print (i )
274+ # print("\n\n", "This is currrent state: ", "\n\n")
275+ # for i in currentState:
276+ # print(i)
277277
278278 # if counter == 1000:
279279 # break
@@ -291,7 +291,7 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
291291 del self .fringe [currentStateIndex ]
292292 self .FatherCost = self .cost [currentStateIndex ]
293293 del self .cost [currentStateIndex ]
294-
294+ print ( self . FatherCost )
295295 # now we must calculate the child of the currentstate and add them to the frontier
296296 # In this code our default problem is 8 puzzle problem
297297 self .updateFringe (currentState , explored , GoalState , algorithmType = "UCS" )
@@ -363,8 +363,66 @@ def searchGreedy(self, initialState, GoalState, spaceGraph=[]):
363363
364364 counter += 1
365365
366- def searchAstar (self ):
367- pass
366+ def searchAstar (self , initialState , GoalState , spaceGraph = []):
367+ ## Temporary Done
368+ """This algorithm will return the solution path.
369+
370+ Args:
371+ initialState (List): Start state.
372+ GoalState (list): End state.
373+ spaceGraph (list): For the time user enter his graph states.
374+ """
375+ if spaceGraph == []:
376+ currentState = list ()
377+ self .fringe = list ()
378+ self .cost = [0 ]
379+ explored = list ()
380+ parentStates = list ()
381+
382+ currentState = deepcopy (initialState )
383+ self .fringe .append (currentState )
384+
385+ else :
386+ # we don't need to calculate the childs.
387+ pass
388+ counter = 0
389+ while True :
390+
391+ # step 1
392+ if currentState not in explored :
393+ explored .append (currentState )
394+
395+ # print("\n\n", "This is currrent state: ", "\n\n")
396+ # for i in currentState:
397+ # print(i)
398+
399+ # if counter == 1000:
400+ # break
401+
402+ if currentState == GoalState :
403+ print (f"The path has been found at iterate { counter } " )
404+ return explored
405+
406+ else :
407+ # current state is in explored list
408+ pass
409+
410+ # step 2 - deleting the currentstate from the fringe and cost
411+ currentStateIndex = self .fringe .index (currentState )
412+ del self .fringe [currentStateIndex ]
413+ self .FatherCost = self .cost [currentStateIndex ]
414+ del self .cost [currentStateIndex ]
415+ print (self .FatherCost )
416+ # now we must calculate the child of the currentstate and add them to the frontier
417+ # In this code our default problem is 8 puzzle problem
418+ self .updateFringe (currentState , explored , GoalState , algorithmType = "Astar" )
419+
420+ # Step 3 - Choosing the state we minimum cost
421+ minimumCost = min (self .cost )
422+ minimumCostIndex = self .cost .index (minimumCost )
423+ currentState = self .fringe [minimumCostIndex ]
424+
425+ counter += 1
368426
369427 def searchHillClimbing (self , initialState , GoalState , spaceGraph = []):
370428 ## Done successfully
@@ -499,7 +557,14 @@ def updateFringe(self, currentState, parents, GoalState, algorithmType="", probl
499557 else :
500558 if algorithmType == "UCS" :
501559 self .fringe .append (newstate )
502- self .cost .append (self .CostCalculator (newstate , GoalState , type = "MissedPlaced" ))
560+ self .cost .append (self .CostCalculator (newstate , GoalState , type = "OnDepthCount" ))
561+ elif algorithmType == "Astar" :
562+ self .fringe .append (newstate )
563+ gn = self .CostCalculator (newstate , GoalState , type = "OnDepthCount" )
564+ hn = self .heuristics (newstate , GoalState , type = "MissedPlaced" )
565+ fn = gn + hn
566+ self .cost .append (hn )
567+ pass
503568
504569 elif algorithmType == "Greedy" :
505570 self .fringe .append (newstate )
@@ -529,26 +594,17 @@ def updateFringe(self, currentState, parents, GoalState, algorithmType="", probl
529594
530595 return self .fringe # A list of all the next states
531596
532- def CostCalculator (self , newState , GoalState , type = "MissedPlaced " ):
597+ def CostCalculator (self , newState , GoalState , type = "OnDepthCount " ):
533598 # This function has problem
534599 """This function calculates the cost of a state
535600 This function calculates the G(n)
536601
537602 Args:
538603 newState (list): The state is a a of three list
539- type (str, optional): Defaults to "MissedPlaced ".
604+ type (str, optional): Defaults to "OnDepthCount ".
540605 """
541- # -------------------This method is not right--------------------- #
542- # -------------------This method is not right--------------------- #
543- if type == "MissedPlaced" :
544- CostOfNewState = 0
545- for row in range (len (newState )):
546- for col in range (len (GoalState )):
547- if newState [row ][col ] != GoalState [row ][col ]:
548- CostOfNewState += 1
549- return CostOfNewState + self .FatherCost
550- # -------------------This method is not right--------------------- #
551- # -------------------This method is not right--------------------- #
606+ # -------------------This must be check more--------------------- #
607+ return 1 + self .FatherCost
552608
553609 def heuristics (self , newState , GoalState , type = "MissedPlaced" ):
554610 # Done successfully
0 commit comments