所以这是我的代码,它在该行中断:
if (suc not in sFrontier) or (suc not in sExplored):
给出错误:TypeError:“实例”类型的参数不可迭代
"""
The pseudocode I'm following
initialize the frontier using the initial state of the problem
initialize the explored set to be empty
loop do
if the frontier is empty then return failure
choose a leaf node and remove it from the frontier
if the node contains a goal state then return the corresponding solution
add the node to the explored set
expand the chosen node, adding the resulting nodes to the frontier
only if not in the frontier or explored set
"""
sFrontier = util.Stack()
sFrontier.push(problem.getStartState())
sExplored = util.Stack()
lSuccessors = []
while not sFrontier.isEmpty():
leaf = sFrontier.pop()
if problem.isGoalState(leaf):
solution = []
while not sExplored.isEmpty():
solution[:0] = (sExplored.pop())[2]
return solution
sExplored.push(leaf)
lSuccessors = problem.getSuccessors(leaf)
for suc in lSuccessors:
if (suc not in sFrontier) or (suc not in sExplored):
sFrontier.push(suc)
return []
question.getSuccessors 返回一个后继状态列表、它们需要的操作以及成本 1。
所以之后
lSuccessors = problem.getSuccessors(leaf)
l 继任者印刷品
[((5,4), 'South', 1), ((4,5), 'West', 1)]
之后
for suc in lSuccessors:
成功打印
((5,4), 'South', 1)
为什么会断?是因为 sFrontier 和 sExplored 是堆栈,它不能在堆栈中查找吗?
我需要一个 contains() 方法还是只使用一个列表?
所有帮助表示赞赏:)