TypeError: argument of type 'instance' is not iterable

user1305599 picture user1305599 · Jan 29, 2013 · Viewed 16.1k times · Source

So heres my code and it breaks at the line:

 if (suc not in sFrontier) or (suc not in sExplored):

giving the error: TypeError: argument of type 'instance' is not iterable

 """
    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 returns a list of successor states, the actions they require, and a cost of 1.

so after

lSuccessors = problem.getSuccessors(leaf)

lSuccessors prints

  [((5,4), 'South', 1), ((4,5), 'West', 1)]

and after

  for suc in lSuccessors:

suc prints

  ((5,4), 'South', 1)

Why does it break? Is it because sFrontier and sExplored are stacks and it cannot look in a stack?

Would I need a contain() method or just use a list instead?

All help appreciated :)

Answer

Kos picture Kos · Jan 29, 2013

I assume util.Stack is your class.

Provide a __contains__(self, x) method to make the objects support a in obj checks.

See the docs: Emulating container types