
ZQc           @   s   d  d l  m Z d  d l m Z d  d l Z d  d l  Z  d  d l m Z d e f d     YZ d   Z d e f d	     YZ d
 e f d     YZ	 d e f d     YZ
 d e f d     YZ d   Z e Z d e f d     YZ d S(   i(   t   manhattanDistance(   t
   DirectionsN(   t   Agentt   ReflexAgentc           B   s    e  Z d  Z d   Z d   Z RS(   s$  
      A reflex agent chooses an action at each choice point by examining
      its alternatives via a state evaluation function.
      The code below is provided as a guide.  You are welcome to change
      it in any way you see fit, so long as you don't touch our method
      headers.
    c   	      C   s   | j    } g  | D] } |  j | |  ^ q } t |  } g  t t |   D] } | | | k rP | ^ qP } t j |  } | | S(   sJ  
        You do not need to change this method, but you're welcome to.
        getAction chooses among the best options according to the evaluation function.
        Just like in the previous project, getAction takes a GameState and returns
        some Directions.X for some X in the set {North, South, West, East, Stop}
        (   t   getLegalActionst   evaluationFunctiont   maxt   ranget   lent   randomt   choice(	   t   selft	   gameStatet
   legalMovest   actiont   scorest	   bestScoret   indext   bestIndicest   chosenIndex(    (    s   multiAgents.pyt	   getAction   s    %5c         C   s`  | j  |  } | j   } | j   } | j   } g  | D] } | j ^ q: } | j   }	 | j   }
 | j   } g  | D] } | j ^ qz } | j   j   } | j   } d } d } d } xE | D]= } | d 7} t j	 |	 |  } | | k  r | } | } q q W| d k rt
 } n t } t
 } | t k rX| j |  j d k rXt } qXn  | j |  } | d k r| t
 k rd } n  t   } x- | D]% } t j	 |	 |  } | j |  qWd } x | D] } t | |  } qW| d k rd } n
 d | } | t k rD| t k rD| d k r3d } qD| d | 7} n  | j   | | | } | S(   s  
        Design a better evaluation function here.
        The evaluation function takes in the current and proposed successor
        GameStates (pacman.py) and returns a number, where higher numbers are better.
        The code below extracts some useful information from the state, like the
        remaining food (newFood) and Pacman position after moving (newPos).
        newScaredTimes holds the number of moves that each ghost will remain
        scared because of Pacman having eaten a power pellet.
        Print out these variables to see what you're getting, then combine them
        to create a masterful evaluation function.
        i?B i    i   i   g.Ag      ?(   t   generatePacmanSuccessort   getPacmanPositiont   getFoodt   getGhostStatest   scaredTimert   getGhostPositionst   asListt
   getNumFoodt   utilR    t   Falset   Truet   getGhostStatet   listt   appendt   mint   getScore(   R   t   currentGameStateR   t   successorGameStatet   newPost   newFoodt   newGhostStatest
   ghostStatet   newScaredTimest   pacmanPositiont   ghostStatest   ghostPositionst   scaredTimest   foodPositionst   foodRemainingt   closestGhostt   closestGhostIndext
   ghostCountt   ghostPositiont   ghostDistancet   ghostsInMazet   isGhostScaredt   ghostScaredTimet   foodDistancest   foodPositiont   foodDistancet   closestFoodt   inverseClosestFoodt   score(    (    s   multiAgents.pyR   .   s^    
					
	(   t   __name__t
   __module__t   __doc__R   R   (    (    (    s   multiAgents.pyR      s   	c         C   s
   |  j    S(   s   
      This default evaluation function just returns the score of the state.
      The score is the same one displayed in the Pacman GUI.
      This evaluation function is meant for use with adversarial search agents
      (not reflex agents).
    (   R$   (   R%   (    (    s   multiAgents.pyt   scoreEvaluationFunction   s    t   MultiAgentSearchAgentc           B   s   e  Z d  Z d d d  Z RS(   sW  
      This class provides some common elements to all of your
      multi-agent searchers.  Any methods defined here will be available
      to the MinimaxPacmanAgent, AlphaBetaPacmanAgent & ExpectimaxPacmanAgent.
      You *do not* need to make any changes here, but you can if you want to
      add functionality to all your adversarial search agents.  Please do not
      remove anything, however.
      Note: this is an abstract class: one that should not be instantiated.  It's
      only partially specified, and designed to be extended.  Agent (game.py)
      is another abstract class.
    RC   t   2c         C   s4   d |  _  t j | t    |  _ t |  |  _ d  S(   Ni    (   R   R   t   lookupt   globalsR   t   intt   depth(   R   t   evalFnRI   (    (    s   multiAgents.pyt   __init__   s    	(   R@   RA   RB   RK   (    (    (    s   multiAgents.pyRD      s   t   MinimaxAgentc           B   s   e  Z d  Z d   Z RS(   s+   
      Your minimax agent (question 2)
    c   <      C   sN  d } |  j  } d } d } d } d } d } d }	 d }
 d } | } | j   } t j } t } t } t } t } t } t j   } t   } | } | } g  } d } t j } g  } | j   t k r | j	   t k r | j
 |  } n  | r8d GHd GHd	 GHd
 G| GHd G| d GHd G| GHd G|
 GHd G| GHd GHn  | | | | | | f | | <| rd GHd G| GHd G| GHd G| GHd G| GHd G| GHd G| GHd G| GHd GHn  | d k r|
 d 7}
 n8 | d k r| d 7} | | k r| } |
 d 7}
 qn  | rd GHd G| GHd G|
 GHd GHn  | g  k r,g  Sx | D] } | j | |  } | } | j   t k s| j	   t k s|
 | k r| | k r| } n | | k r| } n  | | | | |
 | f } | j |  | r3d GHd G| GHd G| GHd G| GHd G|
 GHd G| GHd GHq3q3W| r,d GHd  GHd GHn  d } x{| j   t k r| d 7} | j   \ } } } }  }! }" |  }# |	 d 7}	 | rd! G| GHd" GHd G| GHd G|
 GHd GHd# GHd G| GHd G| GHd$ G|  GHd% G|! GHd& G|" GHd GHn  |! }
 |" } | d k r	|
 d 7}
 n8 | d k rA| d 7} | | k rA| } |
 d 7}
 qAn  | | k s|
 | k re| | k s| j   s| j	   s| g  k r\|  j |  } g  } |# | g  | | | f | |	 <| |# \ }$ }% }& }' }( }) |) j |	  |$ |% |& |' |( |) f | |# <| rd' GHd( GHd G|	 GHd) G|# GHd* G| GHd Gt j GHd G| GHd G| GHd G| GHd GHqq5d+ } | | k r{| d, 9} n  g  } |# | | | | | f | |	 <| |# \ }$ }% }& }' }( }) |) j |	  |$ |% |& |' |( |) f | |# <| r9d- GHd. GHd G|	 GHd) G|# GHd* G| GHd G| GHd G| GHd G| GHd G| GHd GHn  | }* |
 }+ | }, d+ } |* | k rp| }, | d, 9} n  g  }- | j   t k r| j	   t k r| j
 |  }- n  x |- D] }. |
 | k r| | k r| }, q| j | |.  } | }, | j   s:| j	   s:|
 | k r"| | k s:| | k s:| g  k rC| }, n |* | k rX| }, n  |	 }# |. |, | |# |
 | f } | j |  | rd/ GHd G|. GHd G|, GHd GHqqWq5W| rd0 GHd1 GHd GHn  x| | D]t }/ | |/ \ } }0 } } } } | rd2 G|/ GHd  GHd G| GHd G|0 GHd3 G| GHd G| GHd G| GHd G| GHd GHqqW| r_d4 GHd5 GHd GHn  d }1 | |1 \ }$ }% }& }' }( }) | rd6 GHd7 G|1 GHd8 G|$ GHd9 G|% GHd: G|& GHd; G|' GHd< G|( GHd= G|) GHd GHn  d }2 x|1 d k ot |)  d k st|2 d 7}2 | r	d> G|2 GHn  |1 }3 |$ }4 |% }5 |& }6 |' }7 |( }8 |) }9 |5 | k s^	t |)  d k r
| rl	d? GHn  |4 }1 | |1 \ }$ }% }& }' }( }) | r	d9 G|% GHd@ G|7 GHd; G|' GHn  |% | k r
| r	dA GHn  |7 |' k rv
|7 }' |1 d k r	|8 }( n  | r
dB G|' GHd< G|( GHq
qv
nb |% | k rv
| r.
dC GHn  |7 |' k  rv
|7 }' |1 d k rU
|8 }( n  | rs
dB G|' GHd< G|( GHqs
qv
n  |$ |% |& |' |( |) f | |1 <| r$dD G| |1 GHq$ny t |)  d k r$|) j   }: |) }9 |) }9 |4 |5 |6 |7 |8 |9 f | |3 <| r
dE GHn  |: }1 | |1 \ }$ }% }& }' }( }) n  | rd7 G|1 GHd8 G|$ GHd9 G|% GHd: G|& GHd; G|' GHd< G|( GHd= G|) GHd GHqqW| s| rdF GHdG GHdH G|$ GHdI G|% GHdJ G|& GHdK Gt |&  GHdL G|' GHd G|( GHd G|) GHd GHn  | rdM GHdN GHx( | D] }/ dO G|/ GH| |/ GHd GHqWn  |( } |' }; | s+| rJd GHdP G| GHdQ G|; GHd GHn  | S(R   s[  
          Returns the minimax action from the current gameState using self.depth
          and self.evaluationFunction.
          Here are some method calls that might be useful when implementing minimax.
          gameState.getLegalActions(agentIndex):
          Returns a list of legal actions for an agent
            agentIndex=0 means Pacman, ghosts are >= 1
          gameState.generateSuccessor(agentIndex, action):
            Returns the successor game state after an agent takes an action
          gameState.getNumAgents():
            Returns the total number of agents in the game
        i    t   MAXt   MINt   TERMINUSt   RESULTg.t    s   PROBLEM SET-UPs   --------------s   nAgents:t   nGhostsi   s   currentAgent:s   currentDepth:s
   MAX_DEPTH:s   PUSH SEARCH TREE (ROOT)s   node:s   parent:s	   operator:s   legalActions:s   score:s   rootAction:s   nodesToExpand:t
   SUCCESSORSs
   nextAgent:s
   nextDepth:s   PUSH NODE (ROOT CHILD)s   legalAction:s   depth:s   agent:s	   NODE LOOPs	   ---------s
   NODE LOOP s   -------------s   POP NODEs   nodeParent:s
   nodeDepth:s
   nodeAgent:s   PUSH SEARCH TREE (TERMINUS)s   ---------------------------s   parentPosition:t   operatorg.Ag      s   PUSH SEARCH TREE (NON_TERMINUS)s   -------------------------------s	   PUSH NODEs   SEARCH TREE LOOPs   ----------------s   key:t   legalActionst   OPERATEs   #######s   LOOP 0s   n:s   p:s   o:s   l:s   s:s   a:s   e:s   LOOP s   MOVE UPs   copys:s	   APPLY MAXs   new s:s	   APPLY MINs   UP TREE:s	   MOVE DOWNt   ROOTs   ----s   rootParent:s   rootOperator:s   rootLegalActions:s   nRootLegalActions:s
   rootScore:s   SEARCH TREEs   -----------t   keys   resultAction:s   resultScore:(   RI   t   getNumAgentsR   t   STOPR   R   t   Stackt   dictt   isWint   isLoseR   t   generateSuccessorR   t   pusht   isEmptyt   popR   R"   R   (<   R   R   t   PACMANt	   MAX_DEPTHRM   RN   RO   RP   t   rootPositiont   childPositiont   currentDeptht   currentAgentt   currentStatet   nAgentst   resultActiont   DEBUG_SETUPt   DEBUG_NODESt   DEBUG_SEARCHt
   DEBUG_DICTt   DEBUG_RESULTt	   nodeStackt
   searchTreet   parentt   currentOperatort   nodesToExpandR?   t
   rootActionRU   t   legalActiont   successorStatet   nodet   nodeLoopt
   nodeParentt	   nodeDeptht	   nodeAgentt   parentPositiont   pt   ot   lt   st   at   et	   nextAgentt	   nextDeptht   nextOperatort   nextLegalActionst   nextLegalActionRX   RT   t   nt   searchCountt   copynt   copypt   copyot   copylt   copyst   copyat   copyet   lowernt   resultScore(    (    s   multiAgents.pyR      sP   				$											
		<							

								
H													$	H																		!
																							(   R@   RA   RB   R   (    (    (    s   multiAgents.pyRL      s   t   AlphaBetaAgentc           B   s   e  Z d  Z d   Z RS(   sC   
      Your minimax agent with alpha-beta pruning (question 3)
    c         C   s   t  j   d S(   s[   
          Returns the minimax action using self.depth and self.evaluationFunction
        N(   R   t   raiseNotDefined(   R   R   (    (    s   multiAgents.pyR     s    (   R@   RA   RB   R   (    (    (    s   multiAgents.pyR     s   t   ExpectimaxAgentc           B   s   e  Z d  Z d   Z RS(   s.   
      Your expectimax agent (question 4)
    c         C   s   t  j   d S(   s   
          Returns the expectimax action using self.depth and self.evaluationFunction
          All ghosts should be modeled as choosing uniformly at random from their
          legal moves.
        N(   R   R   (   R   R   (    (    s   multiAgents.pyR     s    (   R@   RA   RB   R   (    (    (    s   multiAgents.pyR     s   c         C   s   t  j   d S(   s   
      Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
      evaluation function (question 5).
      DESCRIPTION: <write something here so we know what you did>
    N(   R   R   (   R%   (    (    s   multiAgents.pyt   betterEvaluationFunction  s    t   ContestAgentc           B   s   e  Z d  Z d   Z RS(   s+   
      Your agent for the mini-contest
    c         C   s   t  j   d S(   s  
          Returns an action.  You can use any method you want and search to any depth you want.
          Just remember that the mini-contest is timed, so you have to trade off speed and computation.
          Ghosts don't behave randomly anymore, but they aren't perfect either -- they'll usually
          just make a beeline straight towards Pacman (or away from him if they're scared!)
        N(   R   R   (   R   R   (    (    s   multiAgents.pyR     s    (   R@   RA   RB   R   (    (    (    s   multiAgents.pyR     s   (   R   R    t   gameR   R	   R   R   RC   RD   RL   R   R   R   t   betterR   (    (    (    s   multiAgents.pyt   <module>   s   |	
 	
