Guest User

Untitled

a guest
Nov 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. def depthFirstSearch(problem):
  2.     """
  3.    Search the deepest nodes in the search tree first
  4.    [2nd Edition: p 75, 3rd Edition: p 87]
  5.  
  6.    Your search algorithm needs to return a list of actions that reaches
  7.    the goal.  Make sure to implement a graph search algorithm
  8.    [2nd Edition: Fig. 3.18, 3rd Edition: Fig 3.7].
  9.  
  10.    To get started, you might want to try some of these simple commands to
  11.    understand the search problem that is being passed in:
  12.  
  13.    print "Start:", problem.getStartState()
  14.    print "Is the start a goal?", problem.isGoalState(problem.getStartState())
  15.    print "Start's successors:", problem.getSuccessors(problem.getStartState())
  16.    """
  17.     "*** YOUR CODE HERE ***"
  18.     from util import Stack
  19.     lastSibling = 0
  20.     frontierStack = Stack()
  21.     startState = problem.getStartState()
  22.     exploredSet = set()
  23.     frontierSet = set()
  24.     solutionList = []
  25.     exploredSet.add(startState)
  26.     successorList = problem.getSuccessors(startState)
  27.     if len(successorList) > 1:
  28.         lastSibling = 0
  29.     else:
  30.         lastSibling = lastSibling + 1
  31.     for successor in successorList:
  32.         frontierStack.push(successor)
  33.         frontierSet.add(successor[0])
  34.     while not frontierStack.isEmpty():
  35.         leafNode = frontierStack.pop() #leafNode of form(state, direc, cost)
  36.         solutionList.append(leafNode[1])
  37.         #print "FrontierStack:", frontierStack.list
  38.         frontierSet.remove(leafNode[0])
  39.         #print "frontierSet:", frontierSet
  40.         if problem.isGoalState(leafNode[0]):
  41.             return solutionList
  42.         exploredSet.add(leafNode[0])
  43.         successorList = problem.getSuccessors(leafNode[0])
  44.         #print "Successor List Before:", successorList
  45.         #print "Explored Set:", exploredSet
  46.         #print "Frontier Set:", frontierSet
  47.         for successor in successorList[:]:
  48.             if (successor[0] in exploredSet) or (successor[0] in frontierSet):
  49.                 successorList.remove(successor)
  50.                 #print "HOW OFTEN DO I HAPPEN"
  51.         if (len(successorList) > 1):
  52.             print "CHECK POINT UNO"
  53.             lastSibling = 0
  54.         else:
  55.             lastSibling = lastSibling + 1
  56.             print "CHECK POINT DOS"
  57.         print lastSibling
  58.         #print "Solution List:", solutionList
  59.         print "Current leaf:", leafNode
  60.         print "Successor List:", successorList
  61.         #print "Length Successor List:", len(successorList)
  62.         if (len(successorList) is 0):
  63.             #print "Now that we're here, lastSibling is:", lastSibling
  64.             while lastSibling > 0:
  65.                 #print "WHOOOSH!"
  66.                 solutionList.pop()
  67.                 lastSibling = lastSibling - 1
  68.             #print "Dead end, so pop one. Solution List:", solutionList
  69.         for successor in successorList:
  70.             if(successor[0] not in exploredSet) and (successor[0] not in frontierSet):
  71.                 frontierStack.push(successor)
  72.                 frontierSet.add(successor[0])
  73.         #print "FrontierStack After:", frontierStack.list
  74.     return "Failure"
Add Comment
Please, Sign In to add comment