1

だから、私のコードをここに示し、それは次の行で壊れます:

 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 []

problem.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() メソッドが必要ですか、それとも代わりにリストを使用するだけですか?

すべての助けに感謝します:)

4

4 に答える 4

2

util.Stackはあなたのクラスだと思います。

オブジェクトがチェック__contains__(self, x)をサポートするようにするメソッドを提供します。a in obj

ドキュメントを参照してください:コンテナー タイプのエミュレート

于 2013-01-29T18:38:37.370 に答える
2

スタックが封じ込めテストをサポートしていない場合、実際にエラーがスローされます。テストをサポートするには、それらに__contains__メソッドを追加する必要がありinます。

inテストでスタック内のアイテムを見つける方法は他にもありますが、__contains__方法よりも効率が悪いためお勧めできません。in式のドキュメントを参照してください。

于 2013-01-29T18:38:40.373 に答える