From db5810d5d14f086a49b5b626bd06448afc5e8473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pelletier?= Date: Mon, 4 Feb 2019 00:27:13 -0500 Subject: [PATCH] question 5 fonctionne --- search/search.py | 2 ++ search/searchAgents.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/search/search.py b/search/search.py index a65998a..d2b9aae 100644 --- a/search/search.py +++ b/search/search.py @@ -188,6 +188,7 @@ def uniformCostSearch(problem): maFileEtat.push(succNoeudCourant,succCostChemin) # Ajouter le nouveau chemin dans la file avec son cout maFileChemin.push(listeDirections+[succDirection],succCostChemin) + # si noeud existant, mettre a jour le cout si meilleur elif (succNoeudCourant in [i[0] for i in maFileEtat.heap]): maFileEtat.update(succNoeudCourant,succCostChemin) listeDirections = maFileChemin.pop() @@ -240,6 +241,7 @@ def aStarSearch(problem, heuristic=nullHeuristic): maFileEtat.push(succNoeudCourant,succCostChemin) # Ajouter le nouveau chemin dans la file avec son cout maFileChemin.push(listeDirections+[succDirection],succCostChemin) + # si noeud existant, mettre a jour le cout si meilleur elif (succNoeudCourant in [i[0] for i in maFileEtat.heap]): maFileEtat.update(succNoeudCourant,succCostChemin) listeDirections = maFileChemin.pop() diff --git a/search/searchAgents.py b/search/searchAgents.py index 1d757b1..068b623 100644 --- a/search/searchAgents.py +++ b/search/searchAgents.py @@ -288,6 +288,8 @@ class CornersProblem(search.SearchProblem): # Please add any code here which you would like to use # in initializing the problem "*** YOUR CODE HERE ***" + self.cornerSet = {(1,1), (1,top), (right, 1), (right, top)} + self.defaultCost = 1 def getStartState(self): """ @@ -295,14 +297,24 @@ class CornersProblem(search.SearchProblem): space) """ "*** YOUR CODE HERE ***" - util.raiseNotDefined() + return (self.startingPosition,self.cornerSet) def isGoalState(self, state): """ Returns whether this search state is a goal state of the problem. """ "*** YOUR CODE HERE ***" - util.raiseNotDefined() + currentPosition, currentCornerSet = state + print "Validation du but a ", currentPosition, "Coins restants: ", currentCornerSet + # On atteint le but s'il reste un seul coin a atteindre, et que c'est l'etat actuel + if len(currentCornerSet)==1: + if currentPosition in currentCornerSet: + print "But atteint" + return True + else: + return False + else: + return False def getSuccessors(self, state): """ @@ -325,7 +337,20 @@ class CornersProblem(search.SearchProblem): # hitsWall = self.walls[nextx][nexty] "*** YOUR CODE HERE ***" - + currentPosition, currentCornerSet = state + x,y = currentPosition + dx, dy = Actions.directionToVector(action) + nextx, nexty = int(x + dx), int(y + dy) + nextPosition = (nextx, nexty) + hitsWall = self.walls[nextx][nexty] + if not hitsWall: + # vu que le set est une structure mutable, j'en fais une copie a chaque fois, + # sinon elle reste modifiee dans la boucle + currentCornerSet2 = currentCornerSet.copy() + # Si la position est un coin, alors enlever de l'ensemble + currentCornerSet2.discard(currentPosition) + nextCornerState = (nextPosition,currentCornerSet2) + successors.append((nextCornerState, action, self.defaultCost)) self._expanded += 1 # DO NOT CHANGE return successors