ó
Öî/Qc           @   sµ  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d „  Z d „  Z d „  Z	 d „  Z
 d e f d „  ƒ  YZ d	 „  Z d
 e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d  d l m Z d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d  d l Z d  d l Z d  d l m Z d e j f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   SearchProblemc         C   sE   t  |  ƒ t  g  ƒ k r7 d j t j d j |  ƒ ƒ ƒ St |  ƒ Sd  S(   Ns   
t    (   t   typet   joint   textwrapt   wrapt   str(   t   solution(    (    s   searchTestClasses.pyt   wrap_solution   s    c         C   s7   x0 | j  |  ƒ D] \ } } } | | k r | Sq Wd  S(   N(   t   getSuccessorst   None(   t   statet   actiont   problemt
   successor1t   action1t   cost1(    (    s   searchTestClasses.pyt   followAction   s     c         C   sI   | j  ƒ  } | g } x- |  D]% } t | | | ƒ } | j | ƒ q W| S(   N(   t   getStartStateR   t   append(   t   pathR   R   t   statesR   (    (    s   searchTestClasses.pyt
   followPath#   s    	c         C   s<   |  j  ƒ  } x  | D] } t | | |  ƒ } q W|  j | ƒ S(   N(   R   R   t   isGoalState(   R   R   R   R   (    (    s   searchTestClasses.pyt   checkSolution+   s    t   GraphSearchc           B   sG   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   sI  g  |  _  | j d ƒ } t j d | d ƒ } | d  k r[ d GHd | GHt d | ƒ ‚ n  | j d ƒ j ƒ  |  _ t j d | d ƒ } | d  k r¶ d GHd | GHt d	 | ƒ ‚ n  | j d ƒ j ƒ  } t	 t
 j | ƒ |  _ i  |  _ t ƒ  } g  |  _ x| d
 D]} t | j ƒ  ƒ d k rB| j ƒ  \ } } }	 d }
 nQ t | j ƒ  ƒ d k ru| j ƒ  \ } } }	 }
 n d GHd | GHt d | ƒ ‚ t |
 ƒ }
 |  j j | | |	 |
 f ƒ | j | ƒ | j |	 ƒ | |  j k rôg  |  j | <n  |  j | j |	 | |
 f ƒ qWx- | D]% } | |  j k rg  |  j | <qqWd  S(   Ns   
s   start_state:(.*)i    s   Broken graph:s   """%s"""sK   GraphSearch graph specification start_state not found or incorrect on line:i   s   goal_states:(.*)sK   GraphSearch graph specification goal_states not found or incorrect on line:i   i   i   s8   Invalid line in GraphSearch graph specification on line:(   t   expanded_statest   splitt   ret   matchR
   t	   Exceptiont   groupt   stript   start_statet   mapR   t   goalst
   successorst   sett   orderedSuccessorTuplest   lent   floatR   t   add(   t   selft
   graph_textt   linest   rt   lR#   t
   all_statest   startR   t
   next_statet   costt   s(    (    s   searchTestClasses.pyt   __init__5   sJ    								!c         C   s   |  j  S(   N(   R!   (   R*   (    (    s   searchTestClasses.pyR   ^   s    c         C   s   | |  j  k S(   N(   R#   (   R*   R   (    (    s   searchTestClasses.pyR   b   s    c         C   s!   |  j  j | ƒ t |  j | ƒ S(   N(   R   R   t   listR$   (   R*   R   (    (    s   searchTestClasses.pyR	   f   s    c   
      C   s‘   d } |  j  } x{ | D]s } |  j | } t } x< | D]4 \ } } }	 | | k r6 | } | |	 7} t } q6 q6 W| s d GHt j d ƒ q q W| S(   Ni    s   invalid action sequencei   (   R!   R$   t   Falset   Truet   syst   exit(
   R*   t   actionst
   total_costR   t   aR$   R   R1   R   R2   (    (    s   searchTestClasses.pyt   getCostOfActionsk   s    	
c         C   s   |  j  S(   N(   R   (   R*   (    (    s   searchTestClasses.pyt   getExpandedStates|   s    c         C   sQ   |  j  GHg  |  j D] } d | ^ q } d |  j d j |  j ƒ d j | ƒ f S(   Ns   %s %s %s %ss"   start_state: %s
goal_states: %s
%sR   s   
(   R$   R&   R!   R   R#   (   R*   t   tt   edges(    (    s   searchTestClasses.pyt   __str__   s     (	   t   __name__t
   __module__R4   R   R   R	   R=   R>   RA   (    (    (    s   searchTestClasses.pyR   2   s   	)					c            s‘   i  ‰  xr |  j  d ƒ D]a } | j  ƒ  } t | ƒ d k r[ d GHd t GHt d t ƒ ‚ n  | \ } } t | ƒ ˆ  | <q Wd  ‡  f d † } | S(   Ns   
i   s   Broken heuristic:s   """%s"""s+   GraphSearch heuristic specification broken:c            sR   |  ˆ  k r ˆ  |  St  j d d ƒ } d GH| j  ˆ  ƒ t d t |  ƒ ƒ ‚ d  S(   Nt   indenti   s
   Heuristic:s+   Graph heuristic called with invalid state: (   t   pprintt   PrettyPrinterR   R   (   R   R   t   pp(   t	   heuristic(    s   searchTestClasses.pyt   graphHeuristic”   s    (   R   R'   R+   R   R.   R(   R
   (   t   heuristicTextt   linet   tokensR   t   hRI   (    (   RH   s   searchTestClasses.pyt   parseHeuristic‰   s    		t   GraphSearchTestc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  t |  ƒ j | | ƒ | d |  _ | d |  _ | d |  _ | j d d ƒ j ƒ  d k |  _ d | k rƒ t	 | d ƒ |  _
 n	 d  |  _
 d  S(   Nt   grapht	   algorithmt   diagramt   exactExpansionOrderR7   t   trueRH   (   t   superRO   R4   R+   t   algRR   t   gett   lowerRS   RN   RH   R
   (   R*   t   questiont   testDict(    (    s   searchTestClasses.pyR4   ¢   s    !c         C   sœ   t  | |  j ƒ } t |  j ƒ } |  j d  k rE | | |  j ƒ } n | | ƒ } t | ƒ t g  ƒ k r‰ d  d  d |  j t | ƒ f f S| | j ƒ  d  f S(   Ns4   The result of %s must be a list. (Instead, it is %s)(   t   getattrRV   R   R+   RH   R
   R   R>   (   R*   t   searchRV   R   R   (    (    s   searchTestClasses.pyt
   getSolInfo¯   s     c         C   sæ  | d } | d } t  j | d ƒ t  j | d ƒ g } t  j | d ƒ t  j | d ƒ g } |  j | ƒ \ } }	 }
 |
 d  k r­ | j d |  j ƒ | j d |
 ƒ t S| | k r	|  j sÏ |	 | k r	| j d	 |  j ƒ | j d
 | ƒ | j d |	 ƒ t S| j d |  j ƒ | j d ƒ x. |  j	 j d ƒ D] } | j d | f ƒ q=W| j d | ƒ | j d |	 ƒ | j d ƒ | j d | d ƒ | j d | d ƒ | j d | d ƒ | j d | d ƒ t Sd  S(   NR\   t   searchAgentsR   t   rev_solutionR   t   rev_expanded_statess   FAIL: %ss   	%ss   PASS: %ss   	solution:		%ss   	expanded_states:	%ss   	graph:s   
s   	    %ss   	student solution:		%ss   	student expanded_states:	%st    s   	correct solution:		%si    s   	correct expanded_states:	%ss   	correct rev_solution:		%si   s    	correct rev_expanded_states:	%s(
   R   R   R]   R
   t
   addMessageR   R6   RS   R7   RR   (   R*   t   gradest
   moduleDictt   solutionDictR\   R^   t   gold_solutiont   gold_expanded_statesR   R   t   errorRK   (    (    s   searchTestClasses.pyt   execute¿   s4    

&&"c   	      C   sO  | d } | d } t  | d ƒ } | j d |  j ƒ | j d ƒ | j d ƒ |  j | ƒ \ } } } | d  k rˆ t d | ƒ ‚ n  | j d d	 j | ƒ ƒ | j d
 d	 j | ƒ ƒ | j | _ |  j | ƒ \ } } } | d  k r t d | ƒ ‚ n  | j d d	 j | ƒ ƒ | j d d	 j | ƒ ƒ | j | _ | j ƒ  t	 S(   NR\   R^   t   ws$   # This is the solution file for %s.
s:   # This solution is designed to support both right-to-left
s%   # and left-to-right implementations.
s   Error in solution code: %ss   solution: "%s"
R   s   expanded_states: "%s"
s   rev_solution: "%s"
s   rev_expanded_states: "%s"
(
   t   opent   writeR   R]   R
   R   R   t   REVERSE_PUSHt   closeR7   (	   R*   Rd   t   filePathR\   R^   t   handleR   R   Rh   (    (    s   searchTestClasses.pyt   writeSolutionÞ   s(    

  
(   RB   RC   R4   R]   Ri   Rq   (    (    (    s   searchTestClasses.pyRO       s   			t   PacmanSearchTestc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s¤   t  t |  ƒ j | | ƒ | d |  _ | d |  _ | d |  _ t | j d d ƒ ƒ |  _ t	 | j d d ƒ ƒ |  _
 | j d d	 ƒ |  _ | j d
 d  ƒ |  _ d  S(   Nt   layoutRQ   t
   layoutNamet   leewayFactort   1t   costFnR
   t   searchProblemClasst   PositionSearchProblemRH   (   RU   Rr   R4   t   layout_textRV   Rt   R(   RW   Ru   t   evalRw   t   searchProblemClassNameR
   t   heuristicName(   R*   RY   RZ   (    (    s   searchTestClasses.pyR4   ý   s    c         C   s¨  t  | |  j ƒ } t j g  |  j j d ƒ D] } | j ƒ  ^ q+ ƒ } t j ƒ  } | j	 | d ƒ t  | |  j
 ƒ } i  } |  j d  k r™ |  j | d <n  | | |  }	 |  j d  k rÉ t  | |  j ƒ n d  }
 |
 d  k rí | |	 |
 ƒ } n | |	 ƒ } t | ƒ t g  ƒ k r1d  d  d |  j t | ƒ f f Sd d l m } | j j ƒ  } g  | D] } | | k ^ qWj t ƒ d k r’d  d  d |  j f S|	 j } | | d  f S(   Ns   
i    Rw   s4   The result of %s must be a list. (Instead, it is %s)iÿÿÿÿ(   t
   Directionss;   Output of %s must be a list of actions from game.Directions(   R[   RV   Rs   t   LayoutRz   R   R    t   pacmant	   GameStatet
   initializeR|   Rw   R
   R}   R   t   gameR~   t   LEFTt   keyst   countR6   t	   _expanded(   R*   R\   R^   RV   R.   t   layR!   t   problemClasst   problemOptionsR   RH   R   R~   t   dirst   elt   expanded(    (    s   searchTestClasses.pyR]   
  s*    4' .	c         C   sY  | d } | d } t  j | d ƒ t  j | d ƒ g } t t | d ƒ t | d ƒ ƒ } |  j | | ƒ \ } }	 }
 |
 d  k r­ | j d |  j ƒ | j d |
 ƒ t S| | k r…| j d |  j ƒ | j d	 ƒ | j d
 t	 | ƒ ƒ | j d t
 | ƒ ƒ | j d ƒ | j d t	 | d ƒ ƒ | j d t	 | d ƒ ƒ | j d t
 | d ƒ ƒ | j d t
 | d ƒ ƒ t S|	 |  j | k r|	 | d k r| j d |  j ƒ | j d ƒ | j d |	 ƒ | j d ƒ | j d | |  j f ƒ t S| j d |  j ƒ | j d |  j ƒ | j d t	 | ƒ ƒ | j d |	 ƒ t S(   NR\   R^   R   R_   t   expanded_nodest   rev_expanded_nodess   FAIL: %ss   %ss   Solution not correct.s   	student solution length: %ss   	student solution:
%sRa   s   	correct solution length: %si    s'   	correct (reversed) solution length: %si   s   	correct solution:
%ss    	correct (reversed) solution:
%ss6   Too many node expanded; are you expanding nodes twice?s   	student nodes expanded: %ss-   	correct nodes expanded: %s (leewayFactor %s)s   PASS: %ss   	pacman layout:		%ss   	solution length: %ss   	nodes expanded:		%s(   R   R   t   maxt   intR]   R
   Rb   R   R6   R'   R   Ru   Rt   R7   (   R*   Rc   Rd   Re   R\   R^   Rf   t   gold_expandedR   R   Rh   (    (    s   searchTestClasses.pyRi   '  s@    

&##c   	      C   sQ  | d } | d } t  | d ƒ } | j d |  j ƒ | j d ƒ | j d ƒ | j d |  j ƒ |  j | | ƒ \ } } } | d  k rŸ t d | ƒ ‚ n  | j d	 t | ƒ ƒ | j d
 | ƒ | j | _ |  j | | ƒ \ } } } | d  k rt d | ƒ ‚ n  | j d t | ƒ ƒ | j d | ƒ | j | _ | j	 ƒ  t
 S(   NR\   R^   Rj   s$   # This is the solution file for %s.
s:   # This solution is designed to support both right-to-left
s%   # and left-to-right implementations.
sM   # Number of nodes expanded must be with a factor of %s of the numbers below.
s   Error in solution code: %ss   solution: """
%s
"""
s   expanded_nodes: "%s"
s   rev_solution: """
%s
"""
s   rev_expanded_nodes: "%s"
(   Rk   Rl   R   Ru   R]   R
   R   R   Rm   Rn   R7   (	   R*   Rd   Ro   R\   R^   Rp   R   R   Rh   (    (    s   searchTestClasses.pyRq   P  s*    

  
(   RB   RC   R4   R]   Ri   Rq   (    (    (    s   searchTestClasses.pyRr   û   s   			)(   t   Actionsc   	      C   sr   |  g } |  } x\ | D]T } | \ } } t  j | ƒ \ } } t | | ƒ t | | ƒ f } | j | ƒ q W| S(   s1   Returns the list of states visited along the path(   R“   t   directionToVectorR‘   R   (	   R0   R   t   vist   currR<   t   xt   yt   dxt   dy(    (    s   searchTestClasses.pyt   getStatesFromPathn  s    	 t   CornerProblemTestc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s7   t  t |  ƒ j | | ƒ | d |  _ | d |  _ d  S(   NRs   Rt   (   RU   Rœ   R4   t
   layoutTextRt   (   R*   RY   RZ   (    (    s   searchTestClasses.pyR4   {  s    c         C   s  t  j g  |  j j d ƒ D] } | j ƒ  ^ q ƒ } t j ƒ  } | j | d ƒ | j | ƒ } | j	 | ƒ } t j ƒ  } | j | d ƒ t
 | j ƒ  | ƒ } | j ƒ  j d | j ƒ  j d }	 }
 g  d d |	 f |
 d f |
 |	 f f D] } | | k rë | ^ që } | | f S(   Ns   
i    i   i   (   i   i   (   Rs   R   R   R   R    R€   R   R‚   t   CornersProblemt   bfsR›   t   getPacmanPositiont   getWallst   heightt   width(   R*   R\   R^   R.   Rˆ   t	   gameStateR   R   t   visitedt   topt   rightt   pt   missedCorners(    (    s   searchTestClasses.pyR   €  s    4'Cc   	      C   sm  | d } | d } t  | d ƒ } |  j | | ƒ \ } } t | ƒ t g  ƒ k rƒ | j d |  j ƒ | j d t | ƒ ƒ t St | ƒ d k r¾ | j d |  j ƒ | j d | ƒ t St | ƒ | k r*| j d |  j ƒ | j d ƒ | j d	 t | ƒ ƒ | j d
 ƒ | j d | ƒ t S| j d |  j ƒ | j d |  j ƒ | j d t | ƒ ƒ t S(   NR\   R^   t   solution_lengths   FAIL: %ss.   The result must be a list. (Instead, it is %s)i    s   Corners missed: %ss   Optimal solution not found.s   	student solution length:
%sRa   s   	correct solution length:
%ss   PASS: %ss   	pacman layout:		%ss   	solution length:		%s(	   R‘   R   R   Rb   R   R6   R'   Rt   R7   (	   R*   Rc   Rd   Re   R\   R^   t   gold_lengthR   R©   (    (    s   searchTestClasses.pyRi     s.    

c   	      C   s“   | d } | d } t  | d ƒ } | j d |  j ƒ d G|  j GH|  j GH|  j | | ƒ \ } } t | ƒ } d GH| j d | ƒ | j ƒ  d  S(   NR\   R^   Rj   s$   # This is the solution file for %s.
s   Solving problems   Problem solveds   solution_length: "%s"
(   Rk   Rl   R   Rt   R   R   R'   Rn   (	   R*   Rd   Ro   R\   R^   Rp   R   t   _t   length(    (    s   searchTestClasses.pyRq   ¬  s    

(   RB   RC   R4   R   Ri   Rq   (    (    (    s   searchTestClasses.pyRœ   y  s   			t   HeuristicTestc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   sQ   t  t |  ƒ j | | ƒ | d |  _ | d |  _ | d |  _ | d |  _ d  S(   NRs   Rt   Rx   RH   (   RU   R®   R4   R   Rt   R|   R}   (   R*   RY   RZ   (    (    s   searchTestClasses.pyR4   Ñ  s
    c   	      C   s™   t  j g  |  j j d ƒ D] } | j ƒ  ^ q ƒ } t j ƒ  } | j | d ƒ t | |  j	 ƒ } | | ƒ } | j
 ƒ  } t | |  j ƒ } | | | f S(   Ns   
i    (   Rs   R   R   R   R    R€   R   R‚   R[   R|   R   R}   (	   R*   R^   R.   Rˆ   R¤   R‰   R   R   RH   (    (    s   searchTestClasses.pyt   setupProblemØ  s    4c   
      C   sì   | | | ƒ } | d k r> | d k r1 t  d f St d f Sn  | d k  rT t d f S| d k sj t d f S| | k s€ t d f Sx_ | j | ƒ D]N \ } } } | | | ƒ }	 |	 d k  rÄ t d f S| |	 | k r t d f Sq Wt  d f S(   Ni    Ra   s"   Heuristic failed H(goal) == 0 tests   Heuristic failed H >= 0 tests$   Heuristic failed non-triviality tests#   Heuristic failed admissibility tests!   Heuristic failed consistency test(   R7   R6   R	   (
   R*   RH   R   R   t   solutionCostt   h0t   succR   t   stepCostt   h1(    (    s   searchTestClasses.pyt   checkHeuristicã  s$    



 
 c         C   s¥   | d } | d } t  | d ƒ } |  j | ƒ \ } } }	 |  j |	 | | | ƒ \ }
 } |
 s‰ | j d |  j ƒ | j d | ƒ t S| j d |  j ƒ t Sd  S(   NR\   R^   t   solution_costs   FAIL: %ss   %ss   PASS: %s(   R‘   R¯   Rµ   Rb   R   R6   R7   (   R*   Rc   Rd   Re   R\   R^   R°   R   R   RH   t   passedt   message(    (    s   searchTestClasses.pyRi   ú  s    

c         C   s¯   | d } | d } t  | d ƒ } | j d |  j ƒ d G|  j G|  j GH|  j GH|  j | ƒ \ } } } | j | | ƒ }	 | j |	 ƒ }
 d GH| j d |
 ƒ | j	 ƒ  t
 S(   NR\   R^   Rj   s$   # This is the solution file for %s.
s   Solving problems   Problem solveds   solution_cost: "%s"
(   Rk   Rl   R   Rt   R}   R   R¯   t   astarR=   Rn   R7   (   R*   Rd   Ro   R\   R^   Rp   R   R¬   RH   R   R2   (    (    s   searchTestClasses.pyRq   
  s    


(   RB   RC   R4   R¯   Rµ   Ri   Rq   (    (    (    s   searchTestClasses.pyR®   Ï  s
   				t   HeuristicGradec           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  t |  ƒ j | | ƒ | d |  _ | d |  _ | d |  _ | d |  _ t | d ƒ |  _ g  | d j	 ƒ  D] } t | ƒ ^ qq |  _
 d  S(   NRs   Rt   Rx   RH   t
   basePointst   gradingThresholds(   RU   Rº   R4   R   Rt   R|   R}   R‘   R»   R   t
   thresholds(   R*   RY   RZ   R?   (    (    s   searchTestClasses.pyR4   #  s    c   	      C   s™   t  j g  |  j j d ƒ D] } | j ƒ  ^ q ƒ } t j ƒ  } | j | d ƒ t | |  j	 ƒ } | | ƒ } | j
 ƒ  } t | |  j ƒ } | | | f S(   Ns   
i    (   Rs   R   R   R   R    R€   R   R‚   R[   R|   R   R}   (	   R*   R^   R.   Rˆ   R¤   R‰   R   R   RH   (    (    s   searchTestClasses.pyR¯   ,  s    4c         C   sE  | d } | d } |  j  | ƒ \ } } } | j | | ƒ }	 | j }
 t | |	 ƒ sŒ | j d |  j ƒ | j d ƒ | j d |
 ƒ t S| j |  j ƒ d } x* |  j	 D] } |
 | k  r¬ | d 7} q¬ q¬ W| j | ƒ | t
 |  j	 ƒ k r| j d |  j ƒ n | j d |  j ƒ | j d	 |
 ƒ | j d
 |  j	 ƒ t S(   NR\   R^   s   FAIL: %ss!   	Returned path is not a solution.s   	path returned by astar: %si    i   s   PASS: %ss   	expanded nodes: %ss   	thresholds: %s(   R¯   R¹   R‡   R   Rb   R   R6   t	   addPointsR»   R½   R'   R7   (   R*   Rc   Rd   Re   R\   R^   R   R¬   RH   R   R   t   pointst	   threshold(    (    s   searchTestClasses.pyRi   8  s,    

	c         C   s>   t  | d ƒ } | j d |  j ƒ | j d ƒ | j ƒ  t S(   NRj   s$   # This is the solution file for %s.
s   # File intentionally blank.
(   Rk   Rl   R   Rn   R7   (   R*   Rd   Ro   Rp   (    (    s   searchTestClasses.pyRq   W  s
    
(   RB   RC   R4   R¯   Ri   Rq   (    (    (    s   searchTestClasses.pyRº   !  s   				t   ClosestDotTestc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s7   t  t |  ƒ j | | ƒ | d |  _ | d |  _ d  S(   NRs   Rt   (   RU   RÁ   R4   R   Rt   (   R*   RY   RZ   (    (    s   searchTestClasses.pyR4   q  s    c         C   si   t  j g  |  j j d ƒ D] } | j ƒ  ^ q ƒ } t j ƒ  } | j | d ƒ | j ƒ  j	 | ƒ } | S(   Ns   
i    (
   Rs   R   R   R   R    R€   R   R‚   t   ClosestDotSearchAgentt   findPathToClosestDot(   R*   R^   R.   Rˆ   R¤   R   (    (    s   searchTestClasses.pyR   v  s
    4c         C   s)  | d } | d } t  | d ƒ } |  j | ƒ } t | ƒ t g  ƒ k rz | j d |  j ƒ | j d t | ƒ ƒ t St | ƒ | k ræ | j d |  j ƒ | j d ƒ | j d t | ƒ ƒ | j d ƒ | j d	 | ƒ t S| j d
 |  j ƒ | j d |  j ƒ | j d t | ƒ ƒ t S(   NR\   R^   Rª   s   FAIL: %ss/   	The result must be a list. (Instead, it is %s)s   Closest dot not found.s   	student solution length:
%sRa   s   	correct solution length:
%ss   PASS: %ss   	pacman layout:		%ss   	solution length:		%s(	   R‘   R   R   Rb   R   R6   R'   Rt   R7   (   R*   Rc   Rd   Re   R\   R^   R«   R   (    (    s   searchTestClasses.pyRi   }  s&    

c         C   s„   | d } | d } t  | d ƒ } | j d |  j ƒ d G|  j GH|  j GHt |  j | ƒ ƒ } d GH| j d | ƒ | j ƒ  t S(   NR\   R^   Rj   s$   # This is the solution file for %s.
s   Solving problems   Problem solveds   solution_length: "%s"
(	   Rk   Rl   R   Rt   R   R'   R   Rn   R7   (   R*   Rd   Ro   R\   R^   Rp   R­   (    (    s   searchTestClasses.pyRq   •  s    


(   RB   RC   R4   R   Ri   Rq   (    (    (    s   searchTestClasses.pyRÁ   o  s   			t   CornerHeuristicSanityc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s*   t  t |  ƒ j | | ƒ | d |  _ d  S(   NRs   (   RU   RÄ   R4   Rz   (   R*   RY   RZ   (    (    s   searchTestClasses.pyR4   «  s    c         C   sU  | d } | d } t  j ƒ  } t j g  |  j j d ƒ D] } | j ƒ  ^ q9 ƒ } | j | d ƒ | j | ƒ }	 |	 j	 ƒ  }
 | j
 |
 |	 ƒ } |	 j |
 ƒ } xE | D]= } | j
 | d |	 ƒ } | | d k r§ | j d ƒ t Sq§ W| j
 |
 |	 ƒ } t | d ƒ } | d k r'| j d ƒ t S| | k rD| j d	 ƒ t S| d
 j ƒ  } t | |	 ƒ } g  } x' | D] } | j | j
 | |	 ƒ ƒ qpWxƒ t d t | ƒ d ƒ D]h } | | } | | d } | | d k rì| j d ƒ t S| d k  s| d k  r­| j d ƒ t Sq­W| t | ƒ d d k rD| j d ƒ t S| j d ƒ t S(   NR\   R^   s   
i    i   s   FAIL: inconsistent heuristicR2   s$   FAIL: must use non-trivial heuristics   FAIL: Inadmissible heuristicR   s   FAIL: non-positive heuristics    FAIL: heuristic non-zero at goals8   PASS: heuristic value less than true cost at start state(   R€   R   Rs   R   Rz   R   R    R‚   Rž   R   t   cornersHeuristicR	   Rb   R6   R(   R   R   t   rangeR'   R7   (   R*   Rc   Rd   Re   R\   R^   t
   game_stateR.   Rˆ   R   R!   R±   t   succsR²   R´   t   heuristic_costt	   true_costR   R   t
   heuristicsR   t   i(    (    s   searchTestClasses.pyRi   ¯  sR    

4 
c         C   sú   | d } | d } t  | d ƒ } | j d ƒ | j d ƒ | j d ƒ t j g  |  j j d ƒ D] } | j ƒ  ^ qc ƒ } t j ƒ  } | j	 | d ƒ | j
 | ƒ }	 | j |	 | j ƒ }
 | j d	 t |
 ƒ ƒ | j d
 t |
 ƒ ƒ | j ƒ  t S(   NR\   R^   Rj   s7   # In order for a heuristic to be admissible, the value
s7   # of the heuristic must be less at each state than the
s;   # true cost of the optimal path from that state to a goal.
s   
i    s   cost: "%d"
s   path: """
%s
"""
(   Rk   Rl   Rs   R   Rz   R   R    R€   R   R‚   Rž   R¹   RÅ   R'   R   Rn   R7   (   R*   Rd   Ro   R\   R^   Rp   R.   Rˆ   R!   R   R   (    (    s   searchTestClasses.pyRq   à  s    

4
(   RB   RC   R4   Ri   Rq   (    (    (    s   searchTestClasses.pyRÄ   ©  s   		1t   CornerHeuristicPacmanc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s*   t  t |  ƒ j | | ƒ | d |  _ d  S(   NRs   (   RU   RÍ   R4   Rz   (   R*   RY   RZ   (    (    s   searchTestClasses.pyR4   ø  s    c         C   s±  | d } | d } d } t  | d ƒ } t t | d j ƒ  ƒ } t j ƒ  }	 t j g  |  j j d ƒ D] }
 |
 j	 ƒ  ^ qh ƒ } |	 j
 | d ƒ | j |	 ƒ } | j ƒ  } | j | | ƒ | k r× | j d ƒ t S| j | | j ƒ } d G| GHd	 Gt | ƒ GH| j | ƒ } | | k r0| j d
 ƒ t S| j } d } x' | D] } | | k  rF| d 7} qFqFW| j | ƒ | t | ƒ k rœ| j d | ƒ n | j d | ƒ t S(   NR\   R^   i    R2   R½   s   
s   FAIL: Inadmissible heuristics   path:s   path length:s   FAIL: Inconsistent heuristici   s1   PASS: Heuristic resulted in expansion of %d nodess1   FAIL: Heuristic resulted in expansion of %d nodes(   R(   R"   R‘   R   R€   R   Rs   R   Rz   R    R‚   Rž   R   RÅ   Rb   R6   R¹   R'   R=   R‡   R¾   R7   (   R*   Rc   Rd   Re   R\   R^   t   totalRÊ   R½   RÇ   R.   Rˆ   R   R!   R   R2   R   R¿   RÀ   (    (    s   searchTestClasses.pyRi   ü  s<    

4		c         C   s  | d } | d } t  | d ƒ } | j d ƒ | j d ƒ | j d ƒ t j g  |  j j d ƒ D] } | j ƒ  ^ qc ƒ } t j ƒ  } | j	 | d ƒ | j
 | ƒ }	 | j |	 | j ƒ }
 | j d	 t |
 ƒ ƒ | j d
 t |
 ƒ ƒ | j d ƒ | j ƒ  t S(   NR\   R^   Rj   s>   # This solution file specifies the length of the optimal path
s>   # as well as the thresholds on number of nodes expanded to be
s   # used in scoring.
s   
i    s   cost: "%d"
s   path: """
%s
"""
s   thresholds: "2000 1600 1200"
(   Rk   Rl   Rs   R   Rz   R   R    R€   R   R‚   Rž   R¹   RÅ   R'   R   Rn   R7   (   R*   Rd   Ro   R\   R^   Rp   R.   Rˆ   R!   R   R   (    (    s   searchTestClasses.pyRq     s     

4
(   RB   RC   R4   Ri   Rq   (    (    (    s   searchTestClasses.pyRÍ   ö  s   		!(   t   TimeoutFunctiont
   ExtraGradec           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sv   t  t |  ƒ j | | ƒ | d |  _ | d |  _ t | d ƒ |  _ g  | d j ƒ  D] } t | ƒ ^ qW |  _ d  S(   NRt   t	   agentNamet   maxTimeR½   (	   RU   RÐ   R4   Rt   RÑ   R‘   RÒ   R   R½   (   R*   RY   RZ   R?   (    (    s   searchTestClasses.pyR4   <  s
    c         C   s  t  j  ƒ  } y‚t t j |  j ƒ } y3d |  j |  j f } | t j | j ƒ  ƒ   } t  j  ƒ  | } | d j	 }	 d }
 x0 |  j
 D]% } t |	 ƒ | k r… |
 d 7}
 q… q… W|
 t |  j
 ƒ k rÚ | j d |  j ƒ n | j d |  j ƒ | j d | ƒ | j d t |	 ƒ ƒ | j d |  j d	 ƒ | j d
 |
 |
 f ƒ | j |
 ƒ t SWn3 t k
 rŒ| j d |  j ƒ | j d ƒ t SXWnr t k
 rÚ} | j d |  j ƒ | j d t | ƒ t j ƒ  f ƒ n) | j d |  j ƒ | j d ƒ t SXd  S(   Ns   -l %s -p %s -qi    i   s   PASS: %ss   FAIL: %ss   	Extra credit run-time: %1.2fs   	Extra credit total moves %ds   	Thresholds: %sR½   s!   	Passed %s thresholds: %s points.s   	Extra credit code is too slows(   	Extra credit threw an exception: %s.
%ss&   	Extra credit threw a string exception(   t   timeRÏ   R€   t   runGamesRÒ   Rt   RÑ   t   readCommandR   t   moveHistoryR½   R'   Rb   R   RZ   R¾   R7   t   TimeoutFunctionExceptionR6   R   R   t	   tracebackt
   format_exc(   R*   Rc   Rd   Re   t	   starttimet
   timed_funct   commandt   gamest
   extra_timet   movesR·   R?   t   inst(    (    s   searchTestClasses.pyRi   C  s@     	&c         C   s>   t  | d ƒ } | j d |  j ƒ | j d ƒ | j ƒ  t S(   NRj   s$   # This is the solution file for %s.
s   # File intentionally blank.
(   Rk   Rl   R   Rn   R7   (   R*   Rd   Ro   Rp   (    (    s   searchTestClasses.pyRq   h  s
    
(   RB   RC   R4   Ri   Rq   (    (    (    s   searchTestClasses.pyRÐ   :  s   		%(   R   t   testClassesR   Rs   R€   R\   R    R   R   R   R   R   RN   t   TestCaseRO   Rr   Rƒ   R“   R›   Rœ   R®   Rº   RÁ   RÄ   RÍ   RÓ   RØ   t   utilRÏ   RÐ   (    (    (    s   searchTestClasses.pyt   <module>   s2   					W	[r	VRN:M?