
    h                         S SK r S SKJrJr  S SKJr  S SKJr  S SKr	S SK
Jr   " S S\5      r " S S	\5      r " S
 S\5      r " S S\5      rg)    N)ABCabstractmethod)Iterable)Optional)	DTypeLikec                   f   ^  \ rS rSrSrSU 4S jjrSS jr\S\R                  4S j5       r
SrU =r$ )	ActionNoise
   z
The action noise base class
returnc                 "   > [         TU ]  5         g N)super__init__)self	__class__s    X/home/james-whalen/.local/lib/python3.13/site-packages/stable_baselines3/common/noise.pyr   ActionNoise.__init__   s        c                     g)z)
Call end of episode reset for the noise
N r   s    r   resetActionNoise.reset   s     	r   c                     [        5       er   )NotImplementedErrorr   s    r   __call__ActionNoise.__call__   s    !##r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   npndarrayr   __static_attributes____classcell__r   s   @r   r	   r	   
   s1     $"** $ $r   r	   c            	          ^  \ rS rSrSr\R                  4S\R                  S\R                  S\SS4U 4S jjjr	S\R                  4S	 jr
S\4S
 jrSrU =r$ )NormalActionNoise   z
A Gaussian action noise.

:param mean: Mean value of the noise
:param sigma: Scale of the noise (std here)
:param dtype: Type of the output noise
meansigmadtyper   Nc                 F   > Xl         X l        X0l        [        TU ]  5         g r   )_mu_sigma_dtyper   r   )r   r,   r-   r.   r   s       r   r   NormalActionNoise.__init__&   s    r   c                     [         R                  R                  U R                  U R                  5      R                  U R                  5      $ r   )r$   randomnormalr0   r1   astyper2   r   s    r   r   NormalActionNoise.__call__,   s0    yy$++6==dkkJJr   c                 <    SU R                    SU R                   S3$ )NzNormalActionNoise(mu=, sigma=)r0   r1   r   s    r   __repr__NormalActionNoise.__repr__/   s    &txxjQGGr   )r2   r0   r1   )r   r    r!   r"   r#   r$   float32r%   r   r   r   strr=   r&   r'   r(   s   @r   r*   r*      sh     PRzz RZZ 

 9 ^b  K"** KH# H Hr   r*   c                      ^  \ rS rSrSrSSS\R                  4S\R                  S\R                  S\S	\S
\	\R                     S\
SS4U 4S jjjrS\R                  4S jrSS jrS\4S jrSrU =r$ )OrnsteinUhlenbeckActionNoise3   a  
An Ornstein Uhlenbeck action noise, this is designed to approximate Brownian motion with friction.

Based on http://math.stackexchange.com/questions/1287634/implementing-ornstein-uhlenbeck-in-matlab

:param mean: Mean of the noise
:param sigma: Scale of the noise
:param theta: Rate of mean reversion
:param dt: Timestep for the noise
:param initial_noise: Initial value for the noise output, (if None: 0)
:param dtype: Type of the output noise
g333333?g{Gz?Nr,   r-   thetadtinitial_noiser.   r   c                    > X0l         Xl        X l        X@l        X`l        XPl        [        R                  " U R                  5      U l        U R                  5         [        TU ]-  5         g r   )_thetar0   r1   _dtr2   rF   r$   
zeros_like
noise_prevr   r   r   )r   r,   r-   rD   rE   rF   r.   r   s          r   r   %OrnsteinUhlenbeckActionNoise.__init__A   sL     *--1

r   c                    U R                   U R                  U R                  U R                   -
  -  U R                  -  -   U R                  [
        R                  " U R                  5      -  [
        R                  R                  U R                  R                  S9-  -   nXl         UR                  U R                  5      $ )N)size)rK   rH   r0   rI   r1   r$   sqrtr5   r6   shaper7   r2   r   noises     r   r   %OrnsteinUhlenbeckActionNoise.__call__T   s    OOkkTXX78488CDkkBGGDHH--		0@0@dhhnn0@0UUV 	
  ||DKK((r   c                     U R                   b  U R                   U l        g[        R                  " U R                  5      U l        g)z=
reset the Ornstein Uhlenbeck noise, to the initial position
N)rF   r$   rJ   r0   rK   r   s    r   r   "OrnsteinUhlenbeckActionNoise.reset]   s5     150B0B0N$,,TVTaTabfbjbjTkr   c                 <    SU R                    SU R                   S3$ )Nz OrnsteinUhlenbeckActionNoise(mu=r:   r;   r<   r   s    r   r=   %OrnsteinUhlenbeckActionNoise.__repr__c   s     1$((8DKK=PQRRr   )rI   r2   r0   r1   rH   rF   rK   r   )r   r    r!   r"   r#   r$   r?   r%   floatr   r   r   r   r   r@   r=   r&   r'   r(   s   @r   rB   rB   3   s    " .2::jj zz 	
   

+  
 &)"** )lS# S Sr   rB   c                      \ rS rSrSrS\S\SS4S jrSS\\	\      SS4S	 jjr
S\4S
 jrS\R                  4S jr\S\4S j5       r\R$                  S\SS4S j5       r\S\\   4S j5       r\R$                  S\\   SS4S j5       rSrg)VectorizedActionNoiseg   z
A Vectorized action noise for parallel environments.

:param base_noise: Noise generator to use
:param n_envs: Number of parallel environments

base_noisen_envsr   Nc                 0    [        U5      U l        U R                  S:  d   e Xl        [        U5       Vs/ s H#  n[        R                  " U R
                  5      PM%     snU l	        g ! [        [        4 a  n[	        SU S35      UeS nAff = fs  snf )Nr   zExpected n_envs=z& to be positive integer greater than 0)
intr]   	TypeErrorAssertionError
ValueErrorr\   rangecopydeepcopynoises)r   r\   r]   e_s        r   r   VectorizedActionNoise.__init__o   s    	gf+DK;;?"? %?DV}M}!t}}T__5}M	 >* 	g/x7]^_eff	g Ns   "A+ *B+B;BBindicesc                     Uc  [        [        U R                  5      5      nU H   nU R                  U   R                  5         M"     g)z
Reset all the noise processes, or those listed in indices.

:param indices: The indices to reset. Default: None.
    If the parameter is None, then all processes are reset to their initial position.
N)rc   lenrf   r   )r   rj   indexs      r   r   VectorizedActionNoise.resety   s;     ?C,-GEKK$$& r   c                 P    SU R                   < S[        U R                  5       S3$ )NzVecNoise(BaseNoise=z
), n_envs=r;   )r\   rl   rf   r   s    r   r=   VectorizedActionNoise.__repr__   s'    $T__$7z#dkkBRASSTUUr   c                 z    [         R                  " U R                   Vs/ s H	  o" 5       PM     sn5      nU$ s  snf )z=
Generate and stack the action noise from each noise object.
)r$   stackrf   rQ   s     r   r   VectorizedActionNoise.__call__   s1     t{{;{e%'{;< <s   8c                     U R                   $ r   )_base_noiser   s    r   r\    VectorizedActionNoise.base_noise   s    r   c                     Uc  [        S[        5      e[        U[        5      (       d  [        S[        5      eXl        g )Nz>Expected base_noise to be an instance of ActionNoise, not Nonez9Expected base_noise to be an instance of type ActionNoise)rb   r	   
isinstancer`   ru   )r   r\   s     r   r\   rv      s9    ]_jkk*k22WYdee%r   c                     U R                   $ r   )_noisesr   s    r   rf   VectorizedActionNoise.noises   s    ||r   rf   c           
         [        U5      n[        U5      U R                  :X  d!   SU R                   S[        U5       S35       e[        U5       VVs/ s H-  u  p#[	        U[        U R                  5      5      (       a  M+  UPM/     nnn[        U5      (       a#  [        SU S3[        U R                  5      5      eXl        U H  nUR                  5         M     g s  snnf )NzExpected a list of z ActionNoises, found .zNoise instances at indices z# don't match the type of base_noise)
listrl   r]   	enumeraterx   typer\   rb   rz   r   )r   rf   irR   different_typess        r   rf   r{      s    f6{dkk)q-@Mbcfgmcnboop+qq)-6v->o->jQVX\]a]l]lXmFn1->o-o->>abdhimixixdy  EKKM  ps   *CC)ru   rz   r\   r]   rf   r   )r   r    r!   r"   r#   r	   r_   r   r   r   r   r@   r=   r$   r%   r   propertyr\   setterr~   rf   r&   r   r   r   rZ   rZ   g   s    N; N N N'Xhsm4 ' 'V# V"**   K     &[ &T & & [)   ]]T+. 4  r   rZ   )rd   abcr   r   collections.abcr   typingr   numpyr$   numpy.typingr   r	   r*   rB   rZ   r   r   r   <module>r      sN     # $   "$# $&H H,1S; 1ShGK Gr   