Guest User

Untitled

a guest
Dec 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.43 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 game import Directions
  19.    startNode = (problem.getStartState(),'None',1)
  20.    path =  recDFS(problem, Tree(problem, None, startNode), [[],False])
  21.    direction = []
  22.    for p in path[0]:
  23.  
  24.         direction.append( p[1] )
  25.    return direction
  26.    #return ['North','North','West','East','West','East','West','West','West','West','North','North','West']
  27.    #util.raiseNotDefined()
  28.  
  29.  
  30.    
  31. def recDFS(problem, tree, path):
  32.     #print "\n\n"
  33.     #print path
  34.     if path[1]:
  35.         return path
  36.     if tree == None:
  37.         return path
  38.     for child in tree.children:
  39.         if child in path[0]:
  40.             tree.children.remove(child)
  41.              
  42.            
  43.     if (problem.isGoalState(tree.node[0])):
  44.         path[1] = True
  45.         return path
  46. #   if tree.node[1] != 'None':
  47.         #path.append(tree.node[1])
  48.     if (len(tree.children)==0):
  49.         path[0].pop()
  50.         if tree.parent != None:
  51.             tree.parent.children.remove(tree.node)
  52.         return recDFS(problem, tree.parent, path)
  53.        
  54.        
  55.  
  56.     #(parent,node,[children])
  57.  
  58.  
  59.    
  60.     for child in tree.children:
  61.         path[0].append(child)
  62.  
  63.         recDFS(problem,Tree(problem,tree,child),path)
  64.  
  65.        
  66.     return path
  67.    
  68.    
  69.    
  70. class Tree:
  71.     def __init__(self, problem, parent, node):
  72.        
  73.         self.problem = problem
  74.         self.node = node
  75.         self.parent = parent
  76.         self.children = self.getChildren(node)
  77.  
  78.         #print node, " ", self.children
  79.    
  80.     def getChildren(self, node):
  81.         children = self.problem.getSuccessors(node[0])
  82.         for child in children:
  83.             if self.opposite(node[1],child[1]):
  84.                 children.remove(child)
  85.                
  86.         return children
  87.  
  88.     def opposite(self, d1,d2):
  89.        
  90.         if (d1=='North' and d2=='South'):
  91.             return True
  92.         if (d1=='West' and d2=='East'):
  93.             return True
  94.         if (d1=='South' and d2=='North'):
  95.             return True
  96.         if (d1=='East' and d2=='West'):
  97.             return True
  98.         return False
Add Comment
Please, Sign In to add comment