Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

python - 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.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 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)]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...