2010-07-14 81 views
2

当我执行它时,它给我一个错误,即太多的值解压缩? 我如何使它正常工作。值错误:太多的值解压

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  

错误是:

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

为getsuccessor FUNC的代码是:

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

返回的值该功能最初:

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

如果有人需要更多的信息,请让我知道。 – Shilpa 2010-07-14 22:48:48

+1

@Shilpa:错误发生在179行,所以你应该在那里发布代码。 – sth 2010-07-14 22:52:28

+0

179行是 x,y =州 我编辑了我的问题。请参阅getSuccessor函数。 – Shilpa 2010-07-14 22:57:31

回答

4

首先,它是不太可能是w洞getSuccessors方法,因为没有返回值。

猜测,我会说getSuccessors返回元组列表:(nextState,action,cost)。你将每个节点都存储为节点,当你将一个节点传回方法时会失败,并尝试将这三个值解包为两个值。

你应该找到一个体面的调试器,并学习如何使用它。我使用Eclipse(与PyDev),它将显着帮助你解决这些类型的错误。

+0

我编辑了这个问题。现在你可以看到getsuccessor函数。 179行是x,y =州 – Shilpa 2010-07-14 22:58:09

+0

此外我不能更改此代码。我可以改变我自己的代码在顶部。 – Shilpa 2010-07-14 22:58:42

+0

是的...你是绝对正确的。但我不能想到改变我的代码。我如何在我的代码中进行更改。 – Shilpa 2010-07-14 23:31:20

0

不知道为什么有不一致的大小元组传递到getSuccessors,但您可以通过检查node = stack.pop()行之后的node长度来解决。如果它是3,那么你会想通过node[0]在行child = problem.getSuccessors(node)

+0

节点只是一个存储(5,5)的初始节点值的变量。我如何检查长度....打印len(节点)? – Shilpa 2010-07-14 23:21:52

+0

我需要返回达到目标的操作列表。那么我应该回报什么价值?我不能在while循环结束后使用return None。 – Shilpa 2010-07-15 00:26:38

相关问题