tags:

views:

74

answers:

3
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)]
A: 

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.

eruciform
state is (5,5)- the initial statedx and dy are the numbers.
Shilpa
i'd print out state and dx and dy right in the code and double-check that. you're printing values that are not quite directly related to the line that's broken. better to go straight to the source.
eruciform
+1  A: 

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.

Rob Lourens
problem.getStartState() returns me " (5,5)...this is initial state.I have given you the results of the functions. See the last lines of my question.
Shilpa
Ah. Didn't notice it since it wasn't formatted when i first saw the question. Well do what Wayne said to see what x and y really are at that point.
Rob Lourens
i did that, he is right. The tuple and float cant be concatenated. And I cant change the code in successor function. The only thing I am allowed to do is to change my own code. what changes in my code and make it work? My code is given at the top of the question.
Shilpa
+1  A: 

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
Wayne Werner
Yes...u r awesome....I am getting the same result. 5 5 0.0 -1.0 (5, 5) 5 5 -1.0 0.0 (5, 5)But I cant change the code in getSuccessor function. I'm not allowed to do that. Only I can do is to change my code written at the top of teh question. So what should I do now?
Shilpa
That makes no sense. `state` is a tuple, but you're getting an error from this operation: `nextx, nexty = int(5 + 0.0), int(5 - 1.0)` and that plainly doesn't generate an error. In the directory where you have these(this?) file do you have any `.pyc` files?
Wayne Werner
yes I do have those files.
Shilpa
The file in which the " successor func" is defined, has a .pyc file also but I dint look through it. What should I use from that file??
Shilpa
I think, instead of making a state a "tuple"..can I use it as matrix that can store the values.
Shilpa
delete the `.pyc` files only and see if that helps, because there's no way that code should be giving you an error, but if something was changed in the file and it was `import`ed, then it's possible that the `.pyc` file hasn't been updated.
Wayne Werner
should I delete all the .pyc filese or just that specific file?
Shilpa
I deleted all the .pyc files from the direc that are realted to the projectBut it is still showing the same error.
Shilpa
for x, y, dx, and dy, do `print x, type(x)` - do any of them show tuple?
Wayne Werner
Should i put this line of code before Nextx and nexty in successor fnct
Shilpa
5 <type 'int'> 5 <type 'int'>This is what I get after putting that line of code.
Shilpa
what about for dx and dy?
Wayne Werner