
8~;Lc           @   s[  d  d l  Z  d  d l Z d  d l Z d  d l Z d d d     YZ d d  d     YZ d d! d     YZ d e f d	     YZ d
   Z d e	 f d     YZ
 d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e d  Z d   Z d   Z d  d l Z d e f d     YZ d d" d     YZ d S(#   iNt   Stackc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s;   A container with a last-in-first-out (LIFO) queuing policy.c         C   s   g  |  _  d  S(   N(   t   list(   t   self(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   __init__   s    c         C   s   |  j  j |  d S(   s   Push 'item' onto the stackN(   R   t   append(   R   t   item(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   push   s    c         C   s   |  j  j   S(   s0   Pop the most recently pushed item from the stack(   R   t   pop(   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR      s    c         C   s   t  |  j  d k S(   s"   Returns true if the stack is emptyi    (   t   lenR   (   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   isEmpty   s    (   t   __name__t
   __module__t   __doc__R   R   R   R	   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR       s
   			t   Queuec           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s<   A container with a first-in-first-out (FIFO) queuing policy.c         C   s   g  |  _  d  S(   N(   R   (   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   %   s    c         C   s   |  j  j d |  d S(   s!   Enqueue the 'item' into the queuei    N(   R   t   insert(   R   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   (   s    c         C   s   |  j  j   S(   sx   
      Dequeue the earliest enqueued item still in the queue. This
      operation removes the item from the queue.
    (   R   R   (   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   ,   s    c         C   s   t  |  j  d k S(   s"   Returns true if the queue is emptyi    (   R   R   (   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR	   3   s    (   R
   R   R   R   R   R   R	   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   #   s
   			t   PriorityQueuec           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s  
    Implements a priority queue data structure. Each inserted item
    has a priority associated with it and the client is usually interested
    in quick retrieval of the lowest-priority item in the queue. This
    data structure allows O(1) access to the lowest-priority item.
    
    Note that this PriorityQueue does not allow you to change the priority
    of an item.  However, you may insert the same item multiple times with
    different priorities.
  c         C   s   g  |  _  d  S(   N(   t   heap(   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   B   s    c         C   s#   | | f } t  j |  j |  d  S(   N(   t   heapqt   heappushR   (   R   R   t   priorityt   pair(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   E   s    c         C   s   t  j |  j  \ } } | S(   N(   R   t   heappopR   (   R   R   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   I   s    c         C   s   t  |  j  d k S(   Ni    (   R   R   (   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR	   M   s    (   R
   R   R   R   R   R   R	   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   7   s
   
			t   PriorityQueueWithFunctionc           B   s    e  Z d  Z d   Z d   Z RS(   s   
  Implements a priority queue with the same push/pop signature of the
  Queue and the Stack classes. This is designed for drop-in replacement for
  those two classes. The caller has to provide a priority function, which
  extracts each item's priority.
  c         C   s   | |  _  t j |   d S(   s#   priorityFunction (item) -> priorityN(   t   priorityFunctionR   R   (   R   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   W   s    	c         C   s    t  j |  | |  j |   d S(   sB   Adds an item to the queue with priority from the priority functionN(   R   R   R   (   R   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   \   s    (   R
   R   R   R   R   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   P   s   	c         C   s,   t  |  d | d  t  |  d | d  S(   s9   Returns the Manhattan distance between points xy1 and xy2i    i   (   t   abs(   t   xy1t   xy2(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   manhattanDistancea   s    t   Counterc           B   sz   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z RS(   s  
  A counter keeps track of counts for a set of keys.
  
  The counter class is an extension of the standard python
  dictionary type.  It is specialized to have number values  
  (integers or floats), and includes a handful of additional
  functions to ease the task of counting data.  In particular, 
  all keys are defaulted to have value 0.  Using a dictionary:
  
  a = {}
  print a['test']
  
  would give an error, while the Counter class analogue:
    
  >>> a = Counter()
  >>> print a['test']
  0

  returns the default 0 value. Note that to reference a key 
  that you know is contained in the counter, 
  you can still use the dictionary syntax:
    
  >>> a = Counter()
  >>> a['test'] = 2
  >>> print a['test']
  2
  
  This is very useful for counting things without initializing their counts,
  see for example:
  
  >>> a['blah'] += 1
  >>> print a['blah']
  1
  
  The counter also includes additional functionality useful in implementing
  the classifiers for this assignment.  Two counters can be added,
  subtracted or multiplied together.  See below for details.  They can
  also be normalized and their total count and arg max can be extracted.
  c         C   s    |  j  | d  t j |  |  S(   Ni    (   t
   setdefaultt   dictt   __getitem__(   R   t   idx(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR      s    c         C   s%   x | D] } |  | c | 7<q Wd S(   s   
    Increments all elements of keys by the same count.
    
    >>> a = Counter()
    >>> a.incrementAll(['one','two', 'three'], 1)
    >>> a['one']
    1
    >>> a['two']
    1
    N(    (   R   t   keyst   countt   key(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   incrementAll   s    c         C   sf   t  |  j    d k r d S|  j   } g  | D] } | d ^ q/ } | j t |   } | | d S(   s1   
    Returns the key with the highest value.
    i    i   N(   R   R!   t   Nonet   itemst   indext   max(   R   t   allt   xt   valuest   maxIndex(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   argMax   s     c         C   s@   |  j    } d   } | j d |  g  | D] } | d ^ q, S(   s  
    Returns a list of keys sorted by their values.  Keys
    with the highest values will appear first.
    
    >>> a = Counter()
    >>> a['first'] = -2
    >>> a['second'] = 4
    >>> a['third'] = 1
    >>> a.sortedKeys()
    ['second', 'third', 'first']
    c         S   s   t  | d |  d  S(   Ni   (   t   sign(   R*   t   y(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   <lambda>   s    t   cmpi    (   R&   t   sort(   R   t   sortedItemst   compareR*   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt
   sortedKeys   s    	c         C   s   t  |  j    S(   s1   
    Returns the sum of counts for all keys.
    (   t   sumR+   (   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt
   totalCount   s    c         C   sO   t  |  j    } | d k r" d Sx& |  j   D] } |  | | |  | <q/ Wd S(   s   
    Edits the counter such that the total count of all
    keys sums to 1.  The ratio of counts for all keys
    will remain the same. Note that normalizing an empty 
    Counter will result in an error.
    i    N(   t   floatR7   R!   (   R   t   totalR#   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt	   normalize   s
     c         C   s1   t  |  } x |  D] } |  | c | :<q Wd S(   s'   
    Divides all counts by divisor
    N(   R8   (   R   t   divisorR#   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt	   divideAll   s    c         C   s   t  t j |    S(   s'   
    Returns a copy of the counter
    (   R   R   t   copy(   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR=      s    c         C   sq   d } |  } t  |  t  |  k r4 | | } } n  x6 | D]. } | | k rS q; n  | | | | | 7} q; W| S(   sR  
    Multiplying two counters gives the dot product of their vectors where
    each unique label is a vector element.
    
    >>> a = Counter()
    >>> b = Counter()
    >>> a['first'] = -2
    >>> a['second'] = 4
    >>> b['first'] = 3
    >>> b['second'] = 5
    >>> a['third'] = 1.5
    >>> a['fourth'] = 2.5
    >>> a * b
    14
    i    (   R   (   R   R/   R6   R*   R#   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   __mul__   s    c         C   s1   x* | j    D] \ } } |  | c | 7<q Wd S(   s3  
    Adding another counter to a counter increments the current counter
    by the values stored in the second counter.
    
    >>> a = Counter()
    >>> b = Counter()
    >>> a['first'] = -2
    >>> a['second'] = 4
    >>> b['first'] = 3
    >>> b['third'] = 1
    >>> a += b
    >>> a['first']
    1
    N(   R&   (   R   R/   R#   t   value(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   __radd__   s    c         C   s   t    } xA |  D]9 } | | k r; |  | | | | | <q |  | | | <q Wx. | D]& } | |  k rl qT n  | | | | <qT W| S(   s1  
    Adding two counters gives a counter with the union of all keys and
    counts of the second added to counts of the first.
    
    >>> a = Counter()
    >>> b = Counter()
    >>> a['first'] = -2
    >>> a['second'] = 4
    >>> b['first'] = 3
    >>> b['third'] = 1
    >>> (a + b)['first']
    1
    (   R   (   R   R/   t   addendR#   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   __add__  s    	c         C   s   t    } xA |  D]9 } | | k r; |  | | | | | <q |  | | | <q Wx2 | D]* } | |  k rl qT n  d | | | | <qT W| S(   sH  
    Subtracting a counter from another gives a counter with the union of all keys and
    counts of the second subtracted from counts of the first.
    
    >>> a = Counter()
    >>> b = Counter()
    >>> a['first'] = -2
    >>> a['second'] = 4
    >>> b['first'] = 3
    >>> b['third'] = 1
    >>> (a - b)['first']
    -5
    i(   R   (   R   R/   RA   R#   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   __sub__&  s    	(   R
   R   R   R   R$   R-   R5   R7   R:   R<   R=   R>   R@   RB   RC   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR   k   s   '			
								c           C   s(   d t  j   d d GHt j d  d  S(   Ns   Method not implemented: %si   i   (   t   inspectt   stackt   syst   exit(    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   raiseNotDefined@  s    c   	      C   s   t    } t |   t |  k r| |  } t | j    } | d k rI | Sx, | j   D] } | | } | | | | <qV W| S|  } t t |   } | d k r | Sg  | D] } | | ^ q Sd S(   sS   
  normalize a vector or counter by dividing each value by the sum of all values
  i    N(   R   t   typeR8   R7   R!   R6   (	   t   vectorOrCountert   normalizedCountert   counterR9   R#   R?   t   vectort   st   el(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR:   D  s    	 
 c   	      C   s   t  |   d k r! t |   }  n  g  t |  D] } t j   ^ q. } | j   g  } d d |  d } } } xV | | k  r | | | k  r | d 7} | j | |  qq | d 7} | |  | 7} qq W| S(   Ni   i    (   R6   R:   t   ranget   randomR2   R   (	   t   distributionR+   t   nt   it   randt   samplest	   samplePost   distPost   cdf(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   nSampleW  s    %


c         C   s   t  |   t k r[ |  j   } g  | D] } | d ^ q% }  g  | D] } | d ^ qB } n  t |   d k r| t |   }  n  t j   } d |  d } } x( | | k r | d 7} | |  | 7} q W| | S(   Ni   i    (   RI   R   R&   R6   R:   RQ   (   RR   R+   R&   RT   t   choiceR9   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   sampleg  s     
c         C   sK   |  j    } t g  | D] \ } } | ^ q g  | D] \ } } | ^ q2  S(   N(   R&   R\   (   t   ctrR&   t   kt   v(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   sampleFromCounteru  s    c         C   sC   d } x6 t  | |  D]% \ } } | |  k r | | 7} q q W| S(   sn   
    Gives the probability of a value under a discrete distribution
    defined by (distributions, values).
  g        (   t   zip(   R?   RR   R+   R9   t   probt   val(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   getProbabilityy  s
    c         C   s   t  j    } | |  k  S(   N(   RQ   (   t   pt   r(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   flipCoin  s    c         C   su   t  |   t k s$ t  |   t k r. t |   St j   } d } x. |  D]& \ } } | | 7} | | k rG | SqG Wd S(   sA   Takes either a counter or a list of (prob, key) pairs and samplesg        N(   RI   R   R   R\   RQ   (   RR   Rf   t   baseRb   t   element(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   chooseFromDistribution  s    $

 c         C   s6   |  \ } } t  | d  } t  | d  } | | f S(   s?   
  Finds the nearest grid point to a position (discretizes).
  g      ?(   t   int(   t   post   current_rowt   current_colt   grid_rowt   grid_col(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   nearestPoint  s    c         C   s   |  d k r d Sd Sd S(   s0   
  Returns 1 or -1 depending on the sign of x
  i    i   iN(    (   R*   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR.     s    c         C   s`   g  |  D] } g  ^ q } x@ |  D]8 } x/ t  t |   D] } | | j | |  q9 Wq  W| S(   s1   
  Inverts a matrix stored as a list of lists.
  (   RP   R   R   (   t   arrayRT   t   resultt   outert   inner(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   arrayInvert  s
    c         C   s   t  |   t  |  d  } } g  } xU t |  D]G } x> t |  D]0 } |  | | | k rC | j | | f  qC qC Wq0 W| S(   sL   
  Turns a matrix into a list of coordinates matching the specified value
  i    (   R   RP   R   (   t   matrixR?   t   rowst   colst   cellst   rowt   col(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   matrixAsList  s    c   	      C   sO  |  j  d  } | d k rd d j |  j d  d   |  j d  d } } t |  } t | |  Sg  | j   D]$ } t t |   d k rq | ^ qq } g  | D]' } |  t |  k r t | |   ^ q } | g  | j	   D]  } | d |  k r | d ^ q 7} t
 |  d k r | d St
 |  d k r>t d  n  t d |   d S(	   sl   
  Get a method or class from any imported module from its name.
  Usage: lookup(functionName, globals())
  t   .i    is   <type 'module'>i   s   Name conflict for %ss!   %s not found as a method or classN(   R"   t   joint   splitt
   __import__t   getattrR+   t   strRI   t   dirR&   R   t	   Exception(	   t   namet	   namespacet   dotst
   moduleNamet   objNamet   modulet   objt   modulest   options(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   lookup  s    0747  c           C   s   d GHt    d S(   s6   
  Pauses the output stream awaiting user feedback.
  s    <Press enter/return to continue>N(   t	   raw_input(    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   pause  s    t   TimeoutFunctionExceptionc           B   s   e  Z d  Z RS(   s   Exception to raise on a timeout(   R
   R   R   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR     s   t   TimeoutFunctionc           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s   | |  _  | |  _ d S(   s(   timeout must be at least 1 second. WHY??N(   t   timeoutt   function(   R   R   R   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR     s    	c         C   s   t     d  S(   N(   R   (   R   t   signumt   frame(    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   handle_timeout  s    c         G   s   d t  t  k r |  j |   St j t j |  j  } t j |  j  z |  j |   } Wd  t j t j |  Xt j d  | S(   Nt   SIGALRMi    (   R   t   signalR   R   R   t   alarmR   (   R   t   argst   oldRs   (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   __call__  s    (   R
   R   R   R   R   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyR     s   		(    (    (    (    (   RF   RD   R   RQ   R    R   R   R   R   R   R   RH   R:   RZ   R%   R\   R`   Rd   Rg   Rj   Rq   R.   Rv   t   TrueR}   R   R   R   R   R   R   (    (    (    sa   C:\Users\user\Desktop\Studies\02 BerkeleyX cs188x\05 Projects\00 Unofficial\02 multiagent\util.pyt   <module>	   s2   	
							
	
			
			