stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child = problem.getSuccessors(node)if not child == 0:stack.append(child)closed.apped(node)return None
code of successor is:
def getSuccessors(self, state):"""Returns successor states, the actions they require, and a cost of 1.As noted in search.py:For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the actionrequired to get there, and 'stepCost' is the incremental cost of expanding to that successor"""successors = []for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:x,y = statedx, dy = Actions.directionToVector(action)nextx, nexty = int(x + dx), int(y + dy)if not self.walls[nextx][nexty]:nextState = (nextx, nexty)cost = self.costFn(nextState)successors.append( ( nextState, action, cost) )# Bookkeeping for display purposesself._expanded += 1 if state not in self._visited:self._visited[state] = Trueself._visitedlist.append(state)return successors
The error is:
File line 87, in depthFirstSearchchild = problem.getSuccessors(node)File line 181, in getSuccessorsnextx, nexty = int(x + dx), int(y + dy)
TypeError: can only concatenate tuple (not "float") to tuple
When we run the following commands:
print "Start:", problem.getStartState()print "Is the start a goal?", problem.isGoalState(problem.getStartState())print "Start's successors:", problem.getSuccessors(problem.getStartState())
we get:
Start: (5, 5)
Is the start a goal? False
Start's successors: [((5, 4), 'South', 1), ((4, 5), 'West', 1)]