ó
ˆ±8Qc           @   s}   d  Z  d d l Z d d d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d d	 „ Z e d
 „ Z	 e Z
 e Z e	 Z e Z d S(   st   
In search.py, you will implement generic search algorithms which are called
by Pacman agents (in searchAgents.py).
iÿÿÿÿNt   SearchProblemc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sÞ   
    This class outlines the structure of a search problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).
    You do not need to change anything in this class, ever.
    c         C   s   t  j ƒ  d S(   s@   
        Returns the start state for the search problem
        N(   t   utilt   raiseNotDefined(   t   self(    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   getStartState   s    c         C   s   t  j ƒ  d S(   sk   
          state: Search state
        Returns True if and only if the state is a valid goal state
        N(   R   R   (   R   t   state(    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   isGoalState   s    c         C   s   t  j ƒ  d S(   sR  
          state: Search state
        For a given state, this should return a list of triples,
        (successor, action, stepCost), where 'successor' is a
        successor to the current state, 'action' is the action
        required to get there, and 'stepCost' is the incremental
        cost of expanding to that successor
        N(   R   R   (   R   R   (    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   getSuccessors    s    	c         C   s   t  j ƒ  d S(   sº   
         actions: A list of actions to take
        This method returns the total cost of a particular sequence of actions.  The sequence must
       be composed of legal moves
        N(   R   R   (   R   t   actions(    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   getCostOfActions*   s    (   t   __name__t
   __module__t   __doc__R   R   R   R	   (    (    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyR       s
   			
c         C   s>   d d l  m } | j } | j } | | | | | | | | g S(   s›   
    Returns a sequence of moves that solves tinyMaze.  For any other
    maze, the sequence of moves will be incorrect, so only use this for tinyMaze
    iÿÿÿÿ(   t
   Directions(   t   gameR   t   SOUTHt   WEST(   t   problemR   t   st   w(    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   tinyMazeSearch2   s    		c         C   sh  t  ƒ  } t j ƒ  } t j ƒ  } t } |  j ƒ  t ƒ  d f } | j | ƒ d } xí | j ƒ  t k rA| d } | j	 ƒ  } | \ } } }	 |  j
 | ƒ r¥ t } Pn  | | k rU | j | ƒ x} |  j | ƒ D]i }
 t ƒ  } x | D] } | j | ƒ qä W|
 \ } } } | j | ƒ | | |	 | f } | j | ƒ qÎ WqU qU W| t k rWd GH| Sd G| GH| Sd S(   s  
    Search the deepest nodes in the search tree first
    Your search algorithm needs to return a list of actions that reaches
    the goal.  Make sure to implement a graph search algorithm
    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:
    print "Start:", problem.getStartState()
    print "Is the start a goal?", problem.isGoalState(problem.getStartState())
    print "Start's successors:", problem.getSuccessors(problem.getStartState())
    i    i   s   Failure: No Solution Founds	   Solution:N(   t   setR   t   Stackt   Queuet   FalseR   t   listt   pusht   isEmptyt   popR   t   Truet   addR   t   append(   R   t   closedt   fringet   nodet	   foundGoalt	   startNodet   countert   parent_positiont	   node_patht   parent_stepCostt   childt
   child_patht   elementt   child_positiont   child_actiont   child_stepCostt	   childNode(    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   depthFirstSearch;   s:    	
		c         C   sh  t  ƒ  } t j ƒ  } t j ƒ  } t } |  j ƒ  t ƒ  d f } | j | ƒ d } xí | j ƒ  t k rA| d } | j ƒ  } | \ } } }	 |  j	 | ƒ r¥ t
 } Pn  | | k rU | j | ƒ x} |  j | ƒ D]i }
 t ƒ  } x | D] } | j | ƒ qä W|
 \ } } } | j | ƒ | | |	 | f } | j | ƒ qÎ WqU qU W| t k rWd GH| Sd G| GH| Sd S(   s?   
    Search the shallowest nodes in the search tree first.
    i    i   s   Failure: No Solution Founds	   Solution:N(   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   (    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   breadthFirstSearch|   s:    	
		c         C   sb  t  ƒ  } t j ƒ  } t j ƒ  } t } |  j ƒ  t ƒ  d f } | j | d ƒ xê | j ƒ  t k r;| j	 ƒ  } | \ } } } |  j
 | ƒ r˜ t } Pn  | | k rR | j | ƒ x„ |  j | ƒ D]p }	 t ƒ  }
 x | D] } |
 j | ƒ q× W|	 \ } } } |
 j | ƒ | |
 | | f } | j | | | ƒ qÁ WqR qR W| t k rQd GH| Sd G| GH| Sd S(   s+   Search the node of least total cost first. i    s   Failure: No Solution Founds	   Solution:N(   R   R   t   PriorityQueueR   R   R   R   R   R   R   R   R   R   R   R   (   R   R    R!   R"   R#   R$   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   (    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   uniformCostSearch·   s6    			c         C   s   d S(   s›   
    A heuristic function estimates the cost from the current state to the nearest
    goal in the provided SearchProblem.  This heuristic is trivial.
    i    (    (   R   R   (    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   nullHeuristicù   s    c            s  t  } i  } i  } i  ‰ i  } t  } ˆ j ƒ  } d } ˆ  | ˆ ƒ }	 | |	 }
 | | f } | t k rs d G| GHn  t ƒ  | | <| ˆ | <|
 | | <t j ‡  ‡ ‡ f d †  ƒ } | j | ƒ d } xò| j ƒ  t  k r¹| d 7} | t k rü d G| GHn  | j ƒ  } | d } | d } | ˆ  | ˆ ƒ } | t k rCd GHn  | t k r[d G| GHn  | t k rsd G| GHn  | d k  rˆd } n  | | } | ˆ | <| t k r´d	 G| GHn  | t k rÌd
 G| GHn  ˆ j	 | ƒ t k rët } Pn  | j
 | ƒ } | t k rd G| GHn  t  } | r1| | | k  } n  | t  k sI| t k rÈ | | | <| t k rkd G| GHn  xHˆ j | ƒ D]4} | \ } } } | t k r¨d G| GHn  | t k rÀd G| GHn  ˆ | | } | d k  rãd } n  | ˆ  | ˆ ƒ } | d k  rd } n  t } | j
 | ƒ } | r9| | | k  } n  | t k rQd G| GHn  | t k rid G| GHn  | t k rjt ƒ  } x7 | | D]+ } | j | ƒ | t k r‰d G| GHq‰q‰W| t k rÐd	 G| GHn  | j | ƒ | t k rõd G| GHn  | | | <| ˆ | <| | | <| t k r/d G| | GHn  | t k rKd Gˆ | GHn  | t k rjd G| | GHqjn  | | f } | j | ƒ | t k r›d G| GHn  | t k r{d GHq{q{WqÈ qÈ W| t  k rù| t k rÚd GHn  | t k ròd G| GHn  t ƒ  S| t k rd G| GHn  | Sd S(   sF   Search the node that has the lowest combined cost and heuristic first.i    s
   startNode:c            s   ˆ |  d ˆ  |  d ˆ ƒ S(   Ni    (    (   R"   (   t	   heuristicR   t	   pathgCost(    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   <lambda>  s    i   s   count:s%   #####################################s
   expansion:s	   selected:s   parentPath:s   parentgCost:s   IsInClosedSet:s   closed:s   childPosition:s   childAction:s   doesPathAlreadyExist:s   isfCostCheaper:s   action:s
   childPath:s   path:s
   pathgCost:s
   pathfCost:s
   childNode:s!   ---------------------------------s   Failure: No Solution Founds   last attempted path:s	   Solution:N(   R   R   R   R   R   t   PriorityQueueWithFunctionR   R   R   R   t   has_keyR   R   (   R   R5   R#   R    t   patht	   pathfCostt   moreInfot   positiont   gCostt   hCostt   fCostR$   R!   R%   t
   parentNodet   parentPositiont   parentgCostt   parentfCostt
   parentPatht
   isInClosedt   isNewCostSmallerR)   t   childPositiont   childActiont
   childgCostt
   childfCostt   isfCostCheapert   doesPathAlreadyExistt	   childPatht   actionR/   (    (   R5   R6   R   s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   aStarSearch   sÚ    
 


 

   	

   
   		  	   


        (    (   R   R   R    R   R0   R1   R3   t   NoneR4   RP   t   bfst   dfst   astart   ucs(    (    (    s`   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\01 Search Project\search\search.pyt   <module>   s   #			A	;	B˜