当我执行它时,它给了我一个错误,即解压缩的值太多了? 如何让它正常工作。
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 :(得分:4)
首先,它不太可能是整个getSuccessors
方法,因为没有返回值。
猜测,我会说getSuccessors
返回一个元组列表:(nextState,action,cost)。您将每个节点存储为节点,当您将一个节点传回方法时会失败,并尝试将这三个值解压缩为两个。
你应该找到一个像样的调试器,并学习如何使用它。我使用Eclipse(使用PyDev),它会显着帮助您解决这些类型的错误。
答案 1 :(得分:0)
不确定为什么将大小不一致的元组传递给getSuccessors
,但您可以通过检查node
行之后node = stack.pop()
的长度来修复它。如果是3,那么您需要在node[0]
行中传递child = problem.getSuccessors(node)
。