Error!!! cant concatenate the tuple to non float
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 action
required 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 = state
dx, dy = Actions.dir开发者_Python百科ectionToVector(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 purposes
self._expanded += 1
if state not in self._visited:
self._visited[state] = True
self._visitedlist.append(state)
return successors
The error is:
File line 87, in depthFirstSearch
child = problem.getSuccessors(node)
File line 181, in getSuccessors
nextx, 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)]
Change this:
nextx, nexty = int(x + dx), int(y + dy)
to this:
print x, y, dx, dy, state
nextx, nexty = int(x + dx), int(y + dy)
I guarantee you will see () around something besides state. That means your value is a tuple:
int(x + dx), int(y + dy)
You cannot concatenate a float and a tuple and convert the result to integer, it just won't work:
In [57]: (5, 5) + 3.0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
c:\<ipython console> in <module>()
TypeError: can only concatenate tuple (not "float") to tuple
Looks like x
or y
(or both) is a tuple when it should be a float/int. I'd make sure that state
and node
are what you expect them to be. That's all I can say without knowing more about what problem.getStartState()
is supposed to be doing.
What this error probably means is that you are trying to concatenate (with +) is a mixture of a float and a tuple, and that isn't defined. Check to see what type of thing state
, x
, y
, dx
, and dy
are.
精彩评论