tags:

views:

53

answers:

2

When I execute it, it is giving me an eror i.e too many values to unpack? How do i make it to work properly.

  stack = util.Stack()
  closed = []
  child = []
  index = 0
  currNode = problem.getStartState()
  node = currNode
  stack.push(node)
  while not stack.isEmpty():
     node = stack.pop()
     if problem.isGoalState(node):
        print "true"
        closed.append(node)
     else:
         child = problem.getSuccessors(node)
         for nodes in child:
            stack.push(nodes)
         closed.append(node)
  return None      

Error is:

 File  line 90, in depthFirstSearch
    child = problem.getSuccessors(node)
  File  line 179, in getSuccessors
    x,y = state
**ValueError: too many values to unpack**

The code for the getsuccessor func is:

def getSuccessors(self, state):
    """
    Returns successor states, the actions they require, and a cost of 1.

     """

    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) )

The values returned for this function initially:

problem.getStartState() - (5, 5)
problem.isGoalState(problem.getStartState())- False
 problem.getSuccessors(problem.getStartState()) - [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
+1  A: 

First, it's unlikely that's the whole getSuccessors method, since there's no return value.

To guess, I'd say getSuccessors returns a list of tuples: (nextState, action, cost). You're storing each of those as nodes, which will fail when you pass one back into the method, and it tries to unpack the three values as two.

You owe it to yourself to find a decent debugger, and learn how to use it. I use Eclipse (with PyDev), and it will significantly help you with these sorts of bugs.

Chris B.
I have edited the question. Now you can see the getsuccessor function. 179 line is x,y = state
Shilpa
moreover I cant change this code. I can change my own code which is at the top.
Shilpa
yes...u r absolutly right. But i cant think of changing my code. how do i make changes in my code.
Shilpa
try `for nodes, _, _ in child` rather than `for nodes in child`
Chris B.
I need to return the list of actions that reaches the goal. What value should I return then? I cant use return None after the while loop gets over.
Shilpa
I used ur method and when i execute it, it just gets stopped withour giving any result. That means the loop is completed and I need to return something at the end which is list of actions in the successor funct. What should I write then? return ?
Shilpa
You should probably go talk to whoever gave you this assignment, and tell them you really a refresher course on functions.
Chris B.
A: 

Not sure why there is an inconsistent sized tuples being passed to getSuccessors, but you could probably fix it by checking the length of node after the node = stack.pop() line. If it's 3, then you'll want to pass node[0] in the line child = problem.getSuccessors(node).

node is just a variable that stores the values of initail node which is (5,5). How do i check the length....print len(node)?
Shilpa
I need to return the list of actions that reaches the goal. What value should I return then? I cant use return None after the while loop gets over.
Shilpa