Guest User

Search algo

a guest
Oct 9th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1.  
  2. def performSearch(problem, fringe):
  3.     print "Start:", problem.getStartState()
  4.     """
  5.    (state, direction, path, cost)
  6.    """
  7.     fringe.push((problem.getStartState(),None,[], 0))
  8.     visited = []
  9.     while fringe.isEmpty() == False:
  10.         node = fringe.pop()
  11.         path = node[2]
  12.         if problem.isGoalState(node[0]):
  13.             path.append(node[1])
  14.             print "Found solution:", path
  15.             return path
  16.         elif node[0] not in visited:
  17.             visited = visited + [node[0]]
  18.             if node[1] is not None:
  19.                 path = path + [node[1]]
  20.             for cNode in problem.getSuccessors(node[0]):
  21.                 if cNode[0] not in visited:
  22.                     fringe.push((cNode[0], cNode[1], path, cNode[2]))
  23.                    
  24.     print "Failed to find solution, fringe is emtpy"
  25.     return []
Advertisement
Add Comment
Please, Sign In to add comment