
JPQc           @   s  d  d l  Z  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 d( d	     YZ d
 e f d     YZ	 d   Z
 d e f d     YZ d   Z d   Z d   Z d 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  d l Z d e f d     YZ  d d) d      YZ! d a" d a# e$ a% d! d* d"     YZ& d#   Z' d$   Z( d S(+   iNt   FixedRandomc           B   s   e  Z d    Z RS(   c      r  C   s2   d dsd  f } t j   |  _ |  j j |  d  S(t  Ni   l        l   &n<l   a+yQl   m%l   T5 l   suW l   \X l   dtl   Ccq l   }l   IKP`l   Y l   j4il   )oq l    l   +X l   TXl   @e l   ;l   kd\l   ?Sl   "C l   01 l   sl   0) l   $M l   Cogl l   f>;l   Gw_ l    s< l   P" l   U l   in?j l   y l   .*, l   Z l   :u l   Zsul   .G l   Xl   %W l   t^b] l   ,}Tl   k< l   = l   A.77 l   i l   2M l   E% l   S l   Rjv l   6D l   v2pNl   0
 l   #J l   jE6 l   AFf l   XD l   zM l   	s l   6l   X3 l   Ce l   pCX l   <=2Z l   %`<% l   'Dl   ?gq< l   H l   G6D l   oAl   V] l   I l   )H: l   $$l   fYo l   O3a> l   @/l   o? l   El   \ l   .& l   {%) l   0_ l   8G? l    +l   l*Z/ l   3}^l   qc l    l   ^vF l   Gp l   _*q3 l   `w l   Nl   O"dl   aV5J l   L[ l   JY l   gPl   =~~l   a9=l   JPz$ l   , l   cs l   9>1 l   H l   7FX l   P^ l   <J l   IV l   HG-~ l   |&G l   > l   ?+xl   5ql   5.w l   zq l   L[S l   ? l   6gOA l   V l   )4l   ~o l   U l   wN` l   1)Hl   O l   ubxv l   ^Rl l   b&5 l   ~Zt l   0g l   < 9F l   (.G!l   =/( l   h l   !L~N l   S] l   %'fl   !' l   C\o l   8 l   F}s l   %C l   Ym1L l   C l   RSl   Z: l   =; l   FJR l   _AaI l   D$< l   bty l   !jN l   X  l   *Q
l   =Zl   C l   a&l   :| l   2'!l   k1`l   1Vl   PQ l   %u l   br l   3l   V6= l   m6 l   Nc[ l   U!`l   U7T l   ~,
 l   O l   UL l   L" l   vx# l   >(" l   kvl   n'fl   PtIl   n l   Sf|9 l   ~ l   'L l   ~d l   WQ1l   `L l   cw&K l   -j l   CIJ	 l   &Z$ l   ! l   `d
 l   )lC l   | l   MyOl   lT3Y l   OEx l   rZ
 l   :i l   jXl   5p l   S\6l   &B l   O? l   jBp l   JBl   bxE l    H l   ; l   { l   s_jl   l   8l~ l   [ l   !Q l   -N> l   C2T l   X$Hl   x:El   p6a l   w
l   > l   +/$ l     l   Ks l   ? l   }To l   5 l   c<{ l   R? l   3Rl     l   #l   	 l   my0 l   u l   2+` l   xk l    ?l   $h l   M7 l   cgl   \ugl   #Q l   =*( l   9 l   2m l   )D l   c l   }!^l   bv l   s l   Fll   Sx l   f[l   |/k l   E)-l   & l    l    P~i l   QDJ l   l$z l   n- l   2?Bl   vq l   S+ l   D$ l   >x l   #Nl   i9l   "~l   !? l   ? l   tDWl   4 l   iq l   !RU l   )s l   8L l   ^ l   X: l   ,8ml   oh* l   Ja l   6-l l   V l    l   .:, l   d l   }H- l   /lO l   tl   %(l    \l   :vbh l   )Dw l   r K l   Pj] l   > l   yc l   m& l   bG9 l   tNc l   NS3 l   r+- l   +^ l   PEX l   R3 l   l;&l   Spl   1r l   AH l   >!	 l   CTp l   jS l   oE l     l   km} l   
 l   $U l   xal   Wc# l   6* l   3FQ l   ^"l   	{ l   ES+ql   K F l   *K:l   ]< l   HCQ l   ol1l   nR l   S|4
l   H~Yl   s l   7# l   byY l   ^R8Ml   	y l   * l   2 l   OOt l   u&l   # l   
b l   1L l   ( l   vfAl   ( l   \l   >7Y l   Xl   7m` l   V; l   ;5N*l   ~Dal   dC- l    pl   Zy^l   W8 l   =cP l   El   n=^ l   , l   q5l   wc l   _{n l   Qul   l   ## l   (^l   -y6 l   4-6l   o8l   Qd l   _c>l   GP l   ]N(`l   R9: l    ] l   ~: l   >mI l   aG l   >f l   6a l   2p< l   '< l   ;Oc l   1m` l   V l   _[x8 l   4H l   !z l   /W l    bPl   5% l   Tl9l   oNb l   Vl   KD$3 l    \Nl   e7D l   f l   A& l   r. l   `L l   uf l   w0 l   Yof l   #vJ l   => l   5pj l   Tk l   ,% l   #ql   m_l   wCQl   Al   :F l   u l   Q l   t[l   Jn l   PDr l   {JP l   [| l   !"j/l   &x l   O5 Vl   5 m& l   EH l   E%Ln l   (~ l   "i, l   HTl   J{ l   ,Rl   	r l   Q)9l   5Dl   ?$@= l   +}  l   Oj l   1 l   ?w  l   fT2g l    l   2" l   /+T l   K?* l   	 l   @ l   ]H l   m l   yz I l   Oz, l   ,| l   }[l   > l   Z;~ l   I- l   =x l   qC l   y<l   M3 l   b{ l   zC l   $. l   +@ l   \6l   8zl   N$ l   vk l   %i l   z&|3l   ~ l   Lu l   [jl   5 l   7^( l   ,Z_ l   5_ l   J*G l   V|l    } l   ~!+ l   -[v l   JD l   SNal   I  l   X l   e@c3 l   o[ l    l   ~uc l   f` l   J:U l   r8l   6
tl   7u=l   aHml   +(6 l   aH l   <9i l   J= l   e~F l   HMl   iw l   "H0rl   <PMl   ZUG l   0Agl   
vel   w l   !rl   'P_l   5ni l   JM_l   j+! l   3'# l   &e l   EW?l   74 l   %  l   @b l   rkl   S9M{ l   dl   m l   PpB l   B1"l   jfXK l   Mql   Tp,[l   <} l   Ub l   &-=l   71 l   bCl   2 l   S\ l   sHVl   %& l   -Pl   8{ l   z[ l   _j	l   0*	 l   r~ l   _XP l   5H/ l   V-HDl   =f l   3f=il   Aom l   7hl   b	 l   D l   u,g l   @ l   N.= l   g\ l   ]?:l   eA l   * l   ) l   $Tcl   [vC l   c^2 l   l   v9S l   QNXI l   V8 l   [y l   h l   b l   U]V l   ;q l   = l   Lo l   <#e l   5GKl   Yu l   l"d} l   92 l   8F l   c# l   ~Q l   ?A l   6 l   =a l   ;w l   I;2l   jN l   3O4 l   rg l   Pr\ l     - l   1uI l   ST l   & l   K2 l   V* l   5s l   Y?l   ;A l   L l   '0l   \p" l   <l   'z l   Y@Nl   i"	 l   E+l   P l   F_ l   ~Zjl   u	l   z$Ul   D l   p(q  l        l   &n<l   a+yQl   m%l   T5 l   suW l   \X l   dtl   Ccq l   }l   IKP`l   Y l   j4il   )oq l    l   +X l   TXl   @e l   ;l   kd\l   ?Sl   "C l   01 l   sl   0) l   $M l   Cogl l   f>;l   Gw_ l    s< l   P" l   U l   in?j l   y l   .*, l   Z l   :u l   Zsul   .G l   Xl   %W l   t^b] l   ,}Tl   k< l   = l   A.77 l   i l   2M l   E% l   S l   Rjv l   6D l   v2pNl   0
 l   #J l   jE6 l   AFf l   XD l   zM l   	s l   6l   X3 l   Ce l   pCX l   <=2Z l   %`<% l   'Dl   ?gq< l   H l   G6D l   oAl   V] l   I l   )H: l   $$l   fYo l   O3a> l   @/l   o? l   El   \ l   .& l   {%) l   0_ l   8G? l    +l   l*Z/ l   3}^l   qc l    l   ^vF l   Gp l   _*q3 l   `w l   Nl   O"dl   aV5J l   L[ l   JY l   gPl   =~~l   a9=l   JPz$ l   , l   cs l   9>1 l   H l   7FX l   P^ l   <J l   IV l   HG-~ l   |&G l   > l   ?+xl   5ql   5.w l   zq l   L[S l   ? l   6gOA l   V l   )4l   ~o l   U l   wN` l   1)Hl   O l   ubxv l   ^Rl l   b&5 l   ~Zt l   0g l   < 9F l   (.G!l   =/( l   h l   !L~N l   S] l   %'fl   !' l   C\o l   8 l   F}s l   %C l   Ym1L l   C l   RSl   Z: l   =; l   FJR l   _AaI l   D$< l   bty l   !jN l   X  l   *Q
l   =Zl   C l   a&l   :| l   2'!l   k1`l   1Vl   PQ l   %u l   br l   3l   V6= l   m6 l   Nc[ l   U!`l   U7T l   ~,
 l   O l   UL l   L" l   vx# l   >(" l   kvl   n'fl   PtIl   n l   Sf|9 l   ~ l   'L l   ~d l   WQ1l   `L l   cw&K l   -j l   CIJ	 l   &Z$ l   ! l   `d
 l   )lC l   | l   MyOl   lT3Y l   OEx l   rZ
 l   :i l   jXl   5p l   S\6l   &B l   O? l   jBp l   JBl   bxE l    H l   ; l   { l   s_jl   l   8l~ l   [ l   !Q l   -N> l   C2T l   X$Hl   x:El   p6a l   w
l   > l   +/$ l     l   Ks l   ? l   }To l   5 l   c<{ l   R? l   3Rl     l   #l   	 l   my0 l   u l   2+` l   xk l    ?l   $h l   M7 l   cgl   \ugl   #Q l   =*( l   9 l   2m l   )D l   c l   }!^l   bv l   s l   Fll   Sx l   f[l   |/k l   E)-l   & l    l    P~i l   QDJ l   l$z l   n- l   2?Bl   vq l   S+ l   D$ l   >x l   #Nl   i9l   "~l   !? l   ? l   tDWl   4 l   iq l   !RU l   )s l   8L l   ^ l   X: l   ,8ml   oh* l   Ja l   6-l l   V l    l   .:, l   d l   }H- l   /lO l   tl   %(l    \l   :vbh l   )Dw l   r K l   Pj] l   > l   yc l   m& l   bG9 l   tNc l   NS3 l   r+- l   +^ l   PEX l   R3 l   l;&l   Spl   1r l   AH l   >!	 l   CTp l   jS l   oE l     l   km} l   
 l   $U l   xal   Wc# l   6* l   3FQ l   ^"l   	{ l   ES+ql   K F l   *K:l   ]< l   HCQ l   ol1l   nR l   S|4
l   H~Yl   s l   7# l   byY l   ^R8Ml   	y l   * l   2 l   OOt l   u&l   # l   
b l   1L l   ( l   vfAl   ( l   \l   >7Y l   Xl   7m` l   V; l   ;5N*l   ~Dal   dC- l    pl   Zy^l   W8 l   =cP l   El   n=^ l   , l   q5l   wc l   _{n l   Qul   l   ## l   (^l   -y6 l   4-6l   o8l   Qd l   _c>l   GP l   ]N(`l   R9: l    ] l   ~: l   >mI l   aG l   >f l   6a l   2p< l   '< l   ;Oc l   1m` l   V l   _[x8 l   4H l   !z l   /W l    bPl   5% l   Tl9l   oNb l   Vl   KD$3 l    \Nl   e7D l   f l   A& l   r. l   `L l   uf l   w0 l   Yof l   #vJ l   => l   5pj l   Tk l   ,% l   #ql   m_l   wCQl   Al   :F l   u l   Q l   t[l   Jn l   PDr l   {JP l   [| l   !"j/l   &x l   O5 Vl   5 m& l   EH l   E%Ln l   (~ l   "i, l   HTl   J{ l   ,Rl   	r l   Q)9l   5Dl   ?$@= l   +}  l   Oj l   1 l   ?w  l   fT2g l    l   2" l   /+T l   K?* l   	 l   @ l   ]H l   m l   yz I l   Oz, l   ,| l   }[l   > l   Z;~ l   I- l   =x l   qC l   y<l   M3 l   b{ l   zC l   $. l   +@ l   \6l   8zl   N$ l   vk l   %i l   z&|3l   ~ l   Lu l   [jl   5 l   7^( l   ,Z_ l   5_ l   J*G l   V|l    } l   ~!+ l   -[v l   JD l   SNal   I  l   X l   e@c3 l   o[ l    l   ~uc l   f` l   J:U l   r8l   6
tl   7u=l   aHml   +(6 l   aH l   <9i l   J= l   e~F l   HMl   iw l   "H0rl   <PMl   ZUG l   0Agl   
vel   w l   !rl   'P_l   5ni l   JM_l   j+! l   3'# l   &e l   EW?l   74 l   %  l   @b l   rkl   S9M{ l   dl   m l   PpB l   B1"l   jfXK l   Mql   Tp,[l   <} l   Ub l   &-=l   71 l   bCl   2 l   S\ l   sHVl   %& l   -Pl   8{ l   z[ l   _j	l   0*	 l   r~ l   _XP l   5H/ l   V-HDl   =f l   3f=il   Aom l   7hl   b	 l   D l   u,g l   @ l   N.= l   g\ l   ]?:l   eA l   * l   ) l   $Tcl   [vC l   c^2 l   l   v9S l   QNXI l   V8 l   [y l   h l   b l   U]V l   ;q l   = l   Lo l   <#e l   5GKl   Yu l   l"d} l   92 l   8F l   c# l   ~Q l   ?A l   6 l   =a l   ;w l   I;2l   jN l   3O4 l   rg l   Pr\ l     - l   1uI l   ST l   & l   K2 l   V* l   5s l   Y?l   ;A l   L l   '0l   \p" l   <l   'z l   Y@Nl   i"	 l   E+l   P l   F_ l   ~Zjl   u	l   z$Ul   D l   p(   t   Nonet   randomt   Randomt   setstate(   t   selft
   fixedState(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   __init__   s                                                                                            (   t   __name__t
   __module__R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR       s   t   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(   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR   v   s    c         C   s   |  j  j |  d S(   s   Push 'item' onto the stackN(   R   t   append(   R   t   item(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   pushy   s    c         C   s   |  j  j   S(   s0   Pop the most recently pushed item from the stack(   R   t   pop(   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR   }   s    c         C   s   t  |  j  d k S(   s"   Returns true if the stack is emptyi    (   t   lenR   (   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   isEmpty   s    (   R   R	   t   __doc__R   R   R   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR
   t   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   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    c         C   s   |  j  j   S(   s   
          Dequeue the earliest enqueued item still in the queue. This
          operation removes the item from the queue.
        (   R   R   (   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    c         C   s   t  |  j  d k S(   s"   Returns true if the queue is emptyi    (   R   R   (   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    (   R   R	   R   R   R   R   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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 |  _ d  S(   Ni    (   t   heapt   count(   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    	c         C   s8   | |  j  | f } t j |  j |  |  j  d 7_  d  S(   Ni   (   R   t   heapqt   heappushR   (   R   R   t   priorityt   entry(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    c         C   s   t  j |  j  \ } } } | S(   N(   R   t   heappopR   (   R   t   _R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    c         C   s   t  |  j  d k S(   Ni    (   R   R   (   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    (   R   R	   R   R   R   R   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      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   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      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   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      s    (   R   R	   R   R   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR      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(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   manhattanDistance   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(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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   keysR   t   key(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   incrementAll   s    c         C   sf   t  |  j    d k r d S|  j   } g  | D] } | d ^ q/ } | j t |   } | | d S(   s9   
        Returns the key with the highest value.
        i    i   N(   R   R)   R   t   itemst   indext   max(   R   t   allt   xt   valuest   maxIndex(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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(   R0   t   y(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   <lambda>$  s    t   cmpi    (   R,   t   sort(   R   t   sortedItemst   compareR0   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt
   sortedKeys  s    	c         C   s   t  |  j    S(   s9   
        Returns the sum of counts for all keys.
        (   t   sumR1   (   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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   floatR=   R)   (   R   t   totalR*   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt	   normalize.  s
     c         C   s1   t  |  } x |  D] } |  | c | :<q Wd S(   s/   
        Divides all counts by divisor
        N(   R>   (   R   t   divisorR*   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt	   divideAll:  s    c         C   s   t  t j |    S(   s/   
        Returns a copy of the counter
        (   R$   R&   t   copy(   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyRC   B  s    c         C   sq   d } |  } t  |  t  |  k r4 | | } } n  x6 | D]. } | | k rS q; n  | | | | | 7} q; W| S(   s  
        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   R5   R<   R0   R*   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   __mul__H  s    c         C   s1   x* | j    D] \ } } |  | c | 7<q Wd S(   s_  
        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   R5   R*   t   value(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   __radd__b  s    c         C   s   t    } xA |  D]9 } | | k r; |  | | | | | <q |  | | | <q Wx. | D]& } | |  k rl qT n  | | | | <qT W| S(   sY  
        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   R5   t   addendR*   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   __add__t  s    	c         C   s   t    } xA |  D]9 } | | k r; |  | | | | | <q |  | | | <q Wx2 | D]* } | |  k rl qT n  d | | | | <qT W| S(   sp  
        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   R5   RG   R*   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   __sub__  s    	(   R   R	   R   R'   R+   R3   R;   R=   R@   RB   RC   RD   RF   RH   RI   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR$      s   '			
								c          C   s_   t  j   d d }  t  j   d d } t  j   d d } d | | |  f GHt j d  d  S(   Ni   i   i   s/   *** Method not implemented: %s at line %s of %s(   t   inspectt   stackt   syst   exit(   t   fileNamet   linet   method(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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(   sW   
    normalize a vector or counter by dividing each value by the sum of all values
    i    N(   R$   t   typeR>   R=   R)   R<   (	   t   vectorOrCountert   normalizedCountert   counterR?   R*   RE   t   vectort   st   el(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR@     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    (   R<   R@   t   rangeR   R8   R   (	   t   distributionR1   t   nt   it   randt   samplest	   samplePost   distPost   cdf(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   nSample  s    %


c         C   s   t  |   t k r[ |  j   } g  | D] } | d ^ q% }  g  | D] } | d ^ qB } n  t |   d k r| t |   }  n  | d  k r t j   } n | j   } d |  d } } x( | | k r | d 7} | |  | 7} q W| | S(   Ni   i    (   RR   R$   R,   R<   R@   R   R   (   RZ   R1   R]   R,   R\   t   choiceR?   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   sample  s     
c         C   sK   |  j    } t g  | D] \ } } | ^ q g  | D] \ } } | ^ q2  S(   N(   R,   Rd   (   t   ctrR,   t   kt   v(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   sampleFromCounter  s    c         C   sC   d } x6 t  | |  D]% \ } } | |  k r | | 7} q q W| S(   st   
      Gives the probability of a value under a discrete distribution
      defined by (distributions, values).
    g        (   t   zip(   RE   RZ   R1   R?   t   probt   val(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   getProbability  s
    c         C   s   t  j    } | |  k  S(   N(   R   (   t   pt   r(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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(   RR   R&   R$   Rd   R   (   RZ   Rn   t   baseRj   t   element(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   chooseFromDistribution  s    $

 c         C   s6   |  \ } } t  | d  } t  | d  } | | f S(   sC   
    Finds the nearest grid point to a position (discretizes).
    g      ?(   t   int(   t   post   current_rowt   current_colt   grid_rowt   grid_col(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   nearestPoint  s    c         C   s   |  d k r d Sd Sd S(   s4   
    Returns 1 or -1 depending on the sign of x
    i    i   iN(    (   R0   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR4     s    c         C   s`   g  |  D] } g  ^ q } x@ |  D]8 } x/ t  t |   D] } | | j | |  q9 Wq  W| S(   s5   
    Inverts a matrix stored as a list of lists.
    (   RY   R   R   (   t   arrayR\   t   resultt   outert   inner(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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(   sP   
    Turns a matrix into a list of coordinates matching the specified value
    i    (   R   RY   R   (   t   matrixRE   t   rowst   colst   cellst   rowt   col(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\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(	   sr   
    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   getattrR1   t   strRR   t   dirR,   R   t	   Exception(	   t   namet	   namespacet   dotst
   moduleNamet   objNamet   modulet   objt   modulest   options(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   lookup*  s    0747  c           C   s   d GHt    d S(   s:   
    Pauses the output stream awaiting user feedback.
    s    <Press enter/return to continue>N(   t	   raw_input(    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   pause<  s    t   TimeoutFunctionExceptionc           B   s   e  Z d  Z RS(   s   Exception to raise on a timeout(   R   R	   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR   N  s   t   TimeoutFunctionc           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   timeoutt   function(   R   R   R   (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR   T  s    	c         C   s   t     d  S(   N(   R   (   R   t   signumt   frame(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   handle_timeoutX  s    c         O   s   t  t d  rt t j t j |  j  } t j |  j  z |  j | |   } Wd  t j t j |  Xt j d  nP t j   } |  j | |   } t j   | } | |  j k r |  j d  d   n  | S(   Nt   SIGALRMi    (	   t   hasattrt   signalR   R   t   alarmR   R   t   timeR   (   R   t   argst   keyArgst   oldR{   t	   startTimet   timeElapsed(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   __call__[  s    (   R   R	   R   R   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR   S  s   		t   WritableNullc           B   s   e  Z d    Z RS(   c         C   s   d  S(   N(    (   R   t   string(    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   writev  s    (   R   R	   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyR   u  s   c           C   s)   t  r
 d  St a  t j a t   t _ d  S(   N(   t   _MUTEDt   TrueRL   t   stdoutt   _ORIGINAL_STDOUTR   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt	   mutePrinty  s
    	c           C   s   t  s
 d  St a  t t _ d  S(   N(   R   t   FalseR   RL   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   unmutePrint  s    (    (    (    (    (    (    ()   RL   RJ   R   R   t	   cStringIOR    R
   R   R   R   R#   R&   R$   RQ   R@   Rb   R   Rd   Rh   Rl   Ro   Rr   Ry   R4   R~   R   R   R   R   R   R   R   R   R   R   t   _ORIGINAL_STDERRR   R   R   R   R   (    (    (    sY   C:\Users\Nicholas\Desktop\CS188X Berkeley\05 Projects\02 multiagent-v2\multiagent\util.pyt   <module>   sD   c	
							
	
			
			