
    h.                       S r SSKJr  SSKJr  SSKJr  SSKrSSK	J
r
  SSKJr  SSKJrJrJrJrJrJrJrJrJrJrJr  SS	KJr  / S
Qr " S S5      rS r\SS j5       r\R?                  \5      SS j5       r \R?                  \5      SS j5       r!\R?                  \5      SS j5       r"\R?                  \5      SS j5       r#\R?                  \5      SS j5       r$\R?                  \5      S S j5       r%\R?                  \5      S!S j5       r&\R?                  \5      S"S j5       r'\R?                  \5      S#S j5       r(\R?                  \5      S$S j5       r)        S%S jr*g)&z#Utility functions for the wrappers.    )annotations)Callable)singledispatchN)Space)CustomSpaceError)BoxDictDiscreteGraphGraphInstanceMultiBinaryMultiDiscreteOneOfSequenceTextTuple)T_cov)RunningMeanStd"update_mean_var_count_from_momentscreate_zero_arrayc                  H    \ rS rSrSrSS\R                  4S jrS rS r	Sr
g	)
r      .Tracks the mean, variance and count of values.g-C6? c                t    [         R                  " X#S9U l        [         R                  " X#S9U l        Xl        g)r   dtypeN)npzerosmeanonesvarcount)selfepsilonshaper   s       R/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/wrappers/utils.py__init__RunningMeanStd.__init__#   s'    HHU0	775.
    c                    [         R                  " USS9n[         R                  " USS9nUR                  S   nU R	                  X#U5        g)z8Updates the mean, var and count from a batch of samples.r   axisN)r   r    r"   r&   update_from_moments)r$   x
batch_mean	batch_varbatch_counts        r'   updateRunningMeanStd.update)   s?    WWQQ'
FF11%	ggaj  Dr*   c                    [        U R                  U R                  U R                  XU5      u  U l        U l        U l        g)z4Updates from batch mean, variance and count moments.N)r   r    r"   r#   )r$   r0   r1   r2   s       r'   r.   "RunningMeanStd.update_from_moments0   s0    *LIItxxZK+
'	48TZr*   )r#   r    r"   N)__name__
__module____qualname____firstlineno____doc__r   float64r(   r3   r.   __static_attributes__r   r*   r'   r   r      s"    8  $2RZZ E
r*   r   c                    X0-
  nX%-   nXU-  U-  -   nX-  n	XE-  n
X-   [         R                  " U5      U-  U-  U-  -   nX-  nUnXU4$ )zUUpdates the mean, var and count using the previous mean, var, count and batch values.)r   square)r    r"   r#   r0   r1   r2   delta	tot_countnew_meanm_am_bM2new_var	new_counts                 r'   r   r   7   sp     E#Ik)I55H
+C

!C	RYYu%-;iG	GBnGIi''r*   c                    [        U [        5      (       a$  [        S[        U 5       S[        U 5       S35      e[	        S[        U 5       SU  35      e)a  Creates a zero-based array of a space, this is similar to ``create_empty_array`` except all arrays are valid samples from the space.

As some ``Box`` cases have ``high`` or ``low`` that don't contain zero then the ``create_empty_array`` would in case
create arrays which is not contained in the space.

Args:
    space: The space to create a zero array for

Returns:
    Valid sample from the space that is as close to zero as possible
zSpace of type `zE` doesn't have an registered `create_zero_array` function. Register `z(` for `create_zero_array` to support it.zSThe space provided to `create_zero_array` is not a gymnasium Space instance, type: , )
isinstancer   r   type	TypeErrorspaces    r'   r   r   H   so     %d5k]*optuzp{o|  }e  f
 	
 abfglbmannpqvpwx
 	
r*   c                   [         R                  " U R                  U R                  S9n[         R                  " U R
                  S:  U R
                  U5      n[         R                  " U R                  S:  U R                  U5      nU$ )Nr   r   )r   r   r&   r   wherelowhigh)rN   
zero_arrays     r'   _create_box_zero_arrayrT   _   s\    %++U[[9J%))a-J?J%**q.%**jAJr*   c                    U R                   $ N)startrM   s    r'   _create_discrete_zero_arrayrX   g   s    ;;r*   c                V    [         R                  " U R                  SU R                  S9$ )NT)copyr   )r   arrayrW   r   rM   s    r'    _create_multidiscrete_zero_arrayr\   l   s    88EKKd%++>>r*   c                T    [         R                  " U R                  U R                  S9$ )Nr   )r   r   r&   r   rM   s    r'   _create_array_zero_arrayr^   q   s    88EKKu{{33r*   c                :    [        S U R                   5       5      $ )Nc              3  8   #    U  H  n[        U5      v   M     g 7frV   )r   ).0subspaces     r'   	<genexpr>+_create_tuple_zero_array.<locals>.<genexpr>x   s     J\"8,,\s   )tuplespacesrM   s    r'   _create_tuple_zero_arrayrg   v   s    JU\\JJJr*   c                    U R                   R                  5        VVs0 s H  u  pU[        U5      _M     snn$ s  snnf rV   )rf   itemsr   )rN   keyrb   s      r'   _create_dict_zero_arrayrk   {   s6    BG,,BTBTBVWBVC"8,,BVWWWs   :c                b    U R                   (       a  [        U R                  5      $ [        5       $ rV   )stackr   stacked_feature_spacere   rM   s    r'   _create_sequence_zero_arrayro      s!    {{ !<!<==wr*   c                `   ^  SR                  U 4S j[        T R                  5       5       5      $ )N c              3  B   >#    U  H  nTR                   S    v   M     g7f)r   N)
characters)ra   _rN   s     r'   rc   *_create_text_zero_array.<locals>.<genexpr>   s     H0G15##A&0Gs   )joinrange
min_lengthrM   s   `r'   _create_text_zero_arrayry      s#    77He6F6F0GHHHr*   c                ,   [         R                  " [        U R                  5      SS9nU R                  c  [        US S S9$ [         R                  " [        U R                  5      SS9n[         R                  " S[         R                  S9n[        XUS9$ )Nr   r,   )nodesedges
edge_links)      r   )r   expand_dimsr   
node_space
edge_spacer   r   int64)rN   r{   r|   r}   s       r'   _create_graph_zero_arrayr      sv    NN,U-=-=>QGE5FF01A1ABKXXfBHH5
5*MMr*   c                6    S[        U R                  S   5      4$ )Nr   )r   rf   rM   s    r'   _create_one_of_zero_arrayr      s    Q000r*   c                  ^^ [        U [        5      (       d   e[        U[        R                  5      (       d  [        R                  " [        U5      [        R                  5      (       d5  [        R                  " [        U5      [        R                  5      (       d   e[        R                  " U R                  U5      nUR                  U R                  :X  d0   UR                   SU R                   SU SU R                   35       e[        U[        R                  5      (       d  [        R                  " [        U5      [        R                  5      (       d5  [        R                  " [        U5      [        R                  5      (       d   e[        R                  " U R                  U5      nUR                  U R                  :X  d   e[        R                  " XR                  :H  [        R                  " U5      [        R                  " U R                  5      -     5      (       d   e[        R                  " X R                  :H  [        R                  " U5      [        R                  " U R                  5      -     5      (       d   e[        R                  " X:*  5      (       d   e[        R                  " U R                  U R                  :*  5      (       d   e [        R                  n[        R"                  " U5      n[        R"                  " U5      nXE-  n[        R$                  " U R                  U   US9[        R$                  " U R                  U   US9-
  n[        R&                  " XR(                  S9mX&   X   -
  U-  TU'   [        R*                  " XR(                  S9mX%   U R                  U   -
  TU'   TU   U R                  U   * -  X   -   TU'   [        UUU R                  U R(                  S9nSUU4S jjn	SUU4S jjn
XU
4$ ! [         a    [        R                   n GNHf = f)a  Rescale and shift the given box space to match the given bounds.

For unbounded components in the original space, the corresponding target bounds must also be infinite and vice versa.

Args:
    box: The box space to rescale
    new_min: The new minimum bound
    new_max: The new maximum bound

Returns:
    A tuple containing the rescaled box space, the forward transformation function (original -> rescaled) and the
    backward transformation function (rescaled -> original).
rI   r   )rQ   rR   r&   r   c                   > TU -  T-   $ rV   r   obsgradient	intercepts    r'   forwardrescale_box.<locals>.forward   s    #~	))r*   c                   > U T-
  T-  $ rV   r   r   s    r'   backwardrescale_box.<locals>.backward   s    i8++r*   )r   
np.ndarrayreturnr   )rJ   r   r   ndarray
issubdtyperK   integerfloatingfullr&   rQ   allisinfrR   float128AttributeErrorr<   isfiniter[   	ones_liker   
zeros_like)boxnew_minnew_maxhigh_low_diff_dtype
min_finite
max_finiteboth_finitehigh_low_diffnew_boxr   r   r   r   s              @@r'   rescale_boxr      sK   $ c3grzz**}}T']BJJ772==M2;;<
 <
 	
 
 ''#))W-"=
--399+Ry377)<=" grzz**}}T']BJJ772==M2;;<
 <
 	
 
 ''#))W-==CII%%%667gg%rxx'8288CGG;L'LMNNNN667hh&(9BHHSXX<N(NOPPPP66'$%%%%66#''SXX%&&&&) kk W%JW%J)KHH%8
%-@ABM ||G995Hw33H[ gYY7I#/#((:2FFIj
 333g6II j iiii	G* *, , X%%I  ) jj)s   &P! !P?>P?)rN   zSpace[T_cov]r   r   )rN   r   )rN   r
   )rN   r   )rN   r   )rN   r   )rN   r	   )rN   r   )rN   r   )rN   r   )rN   r   )r   r   r   %np.floating | np.integer | np.ndarrayr   r   r   zRtuple[Box, Callable[[np.ndarray], np.ndarray], Callable[[np.ndarray], np.ndarray]])+r;   
__future__r   collections.abcr   	functoolsr   numpyr   	gymnasiumr   gymnasium.errorr   gymnasium.spacesr   r	   r
   r   r   r   r   r   r   r   r   gymnasium.spaces.spacer   __all__r   r   r   registerrT   rX   r\   r^   rg   rk   ro   ry   r   r   r   r   r*   r'   <module>r      s   ) " $ $   ,    ) X
 
0(" 
 
, C  ! H% & M*? +? K(4 )4 E"K #K D!X "X H% & D!I "I E"N #N E"1 #1P&	P&2P& 3P& X	P&r*   