
    hiX              
          S SK r S SKJrJr  S SKrS SKrS SKJr  S SK	J
r
Jr  S SKJrJr  S\R                  S\4S jrS\\R$                  \R&                  4   S\4S	 jrS"S\R                  S
\S\SS4S jjrS#S\R.                  S\SS4S jjrS\R2                  S\R                  S\R                  SS4S jrS\R2                  SS4S jrS\R2                  S\4S jrS\S\R<                  S\SS4S jrS\\\\R@                  \!4   4   S\R2                  S\"S\\\4   SS4
S jr#S\\$\\R@                  \!4   S\R                  S\SS4S jr%S#S\R.                  S\SS4S jjr&S\R2                  S\R                  S\R                  SS4S jr'S\R2                  SS4S jr(S$S\R2                  S\SS4S jjr)S%S\R2                  S\S \SS4S! jjr*g)&    N)AnyUnion)spaces)check_for_nested_spacesis_image_space_channels_first)DummyVecEnvVecCheckNanspacereturnc                 `    [        U [        R                  [        R                  45      (       + $ )zx
Returns False if provided space is not representable as a single numpy array
(e.g. Dict and Tuple spaces return False)
)
isinstancer   DictTupler
   s    ^/home/james-whalen/.local/lib/python3.13/site-packages/stable_baselines3/common/env_checker.py_is_numpy_array_spacer      s!    
 %&++v||!<===    c                     [         R                  " U R                  [         R                  " U R                  5      5      $ )z?
Return False if a (Multi)Discrete space has a non-zero start.
)npallclosestart
zeros_liker   s    r   _starts_at_zeror      s&     ;;u{{BMM%++$>??r   
space_typekeyc                 (   [        U [        R                  [        R                  45      (       ac  [	        U 5      (       dR  U(       a  SU S3OSn[
        R                  " [        U 5      R                   SU SU SU R                   SU S3
5        g	g	g	)
a4  
:param space: Observation or action space
:param space_type: information about whether it is an observation or action space
    (for the warning message)
:param key: When the observation space comes from a Dict space, we pass the
    corresponding key to have more precise warning messages. Defaults to "".
z(key='z')  z space z with a non-zero start (start=z) is not supported by Stable-Baselines3. You can use a wrapper (see https://stable-baselines3.readthedocs.io/en/master/guide/custom_env.html) or update your z space.N)
r   r   DiscreteMultiDiscreter   warningswarntype__name__r   )r
   r   r   	maybe_keys       r   _check_non_zero_startr&      s     %&//6+?+?@AA/Z_J`J`(+fSE$	E{##$Aj\Cabgbmbman o )\2	
 KaAr   observation_spacec                    U R                   [        R                  :w  a'  [        R                  " SU SU R                    S35        [        R
                  " U R                  S:g  5      (       d(  [        R
                  " U R                  S:g  5      (       a  [        R                  " SU S35        Sn[        U 5      (       a  SnU R                  U   S	:  d  U R                  S
   S	:  a  [        R                  " S5        gg)a2  
Check that the input will be compatible with Stable-Baselines
when the observation is apparently an image.

:param observation_space: Observation space
:param key: When the observation space comes from a Dict space, we pass the
    corresponding key to have more precise warning messages. Defaults to "".
zIt seems that your observation z! is an image but its `dtype` is (z) whereas it has to be `np.uint8`. If your observation is not an image, we recommend you to flatten the observation to have only a 1D vectorr      z%It seems that your observation space z is an image but the upper and lower bounds are not in [0, 255]. Because the CNN policy normalize automatically the observation you may encounter issue if the values are not in that range.$      zThe minimal resolution for an image is 36x36 for the default `CnnPolicy`. You might need to use a custom features extractor cf. https://stable-baselines3.readthedocs.io/en/master/guide/custom_policy.htmlN)
dtyper   uint8r!   r"   anylowhighr   shape)r'   r   non_channel_idxs      r   _check_image_inputr4   -   s     "((*-cU 3$**+ ,''	
 
vv##q())RVV4E4J4Jc4Q-R-R3C5 9K K	
 O$%677/"48I8O8OPQ8RUW8W^	
 9Xr   envaction_spacec                 @   [        U[        R                  5      (       ap  SnUR                  R                  5        H3  u  pE[        U[        R                  5      (       a  Sn[	        USU5        M5     U(       a  [
        R                  " S5        [        U[        R                  5      (       aG  [        UR                  R                  5      S:  a$  [
        R                  " SUR                   S35        [        U[        R                  5      (       a  [
        R                  " S5        [	        US5        [        U[        R                  5      (       a  [
        R                  " S	5        [	        US
5        [        U5      (       d  [
        R                  " S5        gg)zcEmit warnings when the observation space or action space used is not supported by Stable-Baselines.FTobservationa8  Nested observation spaces are not supported by Stable Baselines3 (Dict spaces inside Dict space). You should flatten it to have only one level of keys.For example, `dict(space1=dict(space2=Box(), space3=Box()), spaces4=Discrete())` is not supported but `dict(space2=Box(), spaces3=Box(), spaces4=Discrete())` is.r,   zBThe MultiDiscrete observation space uses a multidimensional array z which is currently not supported by Stable-Baselines3. Please convert it to a 1D array using a wrapper: https://github.com/DLR-RM/stable-baselines3/issues/1836.zThe observation space is a Tuple, this is currently not supported by Stable Baselines3. However, you can convert it to a Dict observation space (cf. https://gymnasium.farama.org/api/spaces/composite/#dict). which is supported by SB3.zSequence observation space is not supported by Stable-Baselines3. You can pad your observation to have a fixed size instead.
Note: The checks for returned values are skipped.actionzThe action space is not based off a numpy array. Typically this means it's either a Dict or Tuple space. This type of action space is currently not supported by Stable Baselines 3. You should try to flatten the action using a wrapper.N)r   r   r   itemsr&   r!   r"   r    lennvecr2   r   Sequencer   )r5   r'   r6   nested_dictr   r
   s         r   _check_unsupported_spacesr?   S   sJ    #V[[11+2288:JC%--"!%< ;
 MMc #V%9%9::sCTCYCYC_C_?`cd?dPQbQgQgPh iG G	
 #V\\22)	
 +];#V__55@	
 ,1 ..&	
 /r   c                   ^  [        [        U 4S j/5      5      nUR                  5         [        S5       HH  n[        R
                  " T R                  R                  5       /5      nUR                  U5      u        nMJ     g)z+Check for Inf and NaN using the VecWrapper.c                     > T $ )N r5   s   r   <lambda>_check_nan.<locals>.<lambda>   s    sr   
   N)	r	   r   resetranger   arrayr6   samplestep)r5   vec_env_r9   s   `   r   
_check_nanrN      sa    +{m45GMMO2Y3++22456\\&)
1a r   c                 .    [        U R                  S5      $ )zh
Check if the env uses the convention for goal-conditioned envs (previously, the gym.GoalEnv interface)
compute_reward)hasattr	unwrappedrC   s    r   _is_goal_envrS      s    
 3=="233r   obsmethod_namec                 R   [        UR                  5      S:  dA   S[        UR                  5       S[        UR                  R                  5       5       35       eS HG  nX1R                  ;  d  M  [	        SU SU S[        UR                  R                  5       5       35      e   g)	z
Check that an environment implementing the `compute_rewards()` method
(previously known as GoalEnv in gym) contains at least three elements,
namely `observation`, `achieved_goal`, and `desired_goal`.
   zA goal conditioned env must contain at least 3 observation keys: `observation`, `achieved_goal`, and `desired_goal`. The current observation contains z keys: )achieved_goaldesired_goal!The observation returned by the `z3()` method of a goal-conditioned env requires the 'zA' key to be part of the observation dictionary. Current keys are N)r;   r   listkeysAssertionError)rT   r'   rU   r   s       r   _check_goal_env_obsr^      s      ''(A- 	,,/0A0H0H,I+J'RVWhWoWoWtWtWvRwQx	z-
 1... 3K=@stwsx y$$():)A)A)F)F)H$I#JL  1r   rewardinfoc                 r   U S   U S   pTX!R                  XEU5      :X  d   S5       e[        R                  " U5      [        R                  " U5      pT[        R                  " XD/5      n[        R                  " XU/5      n[        U[        5      (       d  [        UR                  5      S:X  a$  UR                  SS5      nUR                  SS5      n[        R                  " X3/5      nUR                  XgU5      n	U	R                  S:X  d   SU	R                   S	35       eU	S   U:X  d   S
U	S    SU 35       eg)z`
Check that reward is computed with `compute_reward`
and that the implementation is vectorized.
rX   rY   z3The reward was not computed with `compute_reward()`r      r,   )rb   z7Unexpected shape for vectorized computation of reward: z != (2,)zBVectorized computation of reward differs from single computation:  != N)rP   r   rI   r   intr;   r2   reshape)
rT   r5   r_   r`   rX   rY   batch_achieved_goalsbatch_desired_goalsbatch_infosrewardss
             r   _check_goal_env_compute_rewardrj      s@    #&o"6N8K<''T  =<=  #%((="9288L;Q<88]$BC((L#?@-%%]-@-@)AQ)F3;;AqA199!Q?((D<(K  !5KXG==D s$[\c\i\i[jjr"ss 1:~#efmnofpeqquv|u}!~~r   c                 j   [        U[        R                  5      (       d   [        U [        5      (       a   SU S35       e[        U[        R                  5      (       a?  [
        R                  " [        U 5      [
        R                  5      (       d   SU S35       eO:[        U5      (       a*  [        U [
        R                  5      (       d   SU S35       e[        U [
        R                  5      (       Ga  UR                  U R                  :X  d'   SU SU SUR                   SU R                   35       e[
        R                  " U R                  UR                  5      (       d'   SU S	U SUR                   S
U R                   35       e[        U[        R                  5      (       a  UR                  UR                   pC[
        R"                  " [
        R$                  " X:  X:  5      5      nX:  R'                  5       (       d  X:  R'                  5       (       an  SU SU S3nU[)        US   5       S3-  n[+        U6  H:  nSR-                  [/        [0        U5      5      nUSX7    SU SXG    SX    S3	-  nM<     [3        U5      eUR5                  U 5      (       d   SU SU 35       eg)zX
Check that the observation returned by the environment
correspond to the declared one.
rZ   z0()` method should be a single value, not a tuplezThe observation returned by `z()` method must be an intz ()` method must be a numpy arrayzC()` method does not match the shape of the given observation space z. Expected: z, actual shape: zU()` method does not match the data type (cannot cast) of the given observation space z, actual dtype: zD()` method does not match the bounds of the given observation space z. 
r   z invalid indices: 
,z
Expected: z <= obs[z] <= z, actual value: z 
z6()` method does not match the given observation space N)r   r   r   tupler   r   
issubdtyper#   integerr   ndarrayr2   can_castr-   Boxr0   r1   where
logical_orr/   r;   zipjoinmapstrr]   contains)	rT   r'   rU   lower_boundsupper_boundsinvalid_indicesmessageindex	index_strs	            r   
_check_obsr      s   
 '66
 
 	m.{m;kl	m 

 #V__55 }}T#Y

33{7TU`Taaz5{{3	0	1	1#rzz**y.KK=Xx,yy* #rzz"" &&#))3 	
/} =..?-@ A*0011A#))N	
3
 {{399&7&=&=>> 	
/} =..?-@ A*0011A#))N	
>
 '44):)>)>@Q@V@V, hhr}}S5GI['\]O"''))c.@-E-E-G-G7} E66G5HN  c/!"4566JKK /2E #S% 9I$\%8$9)ER^ReQf g)),C9G 3 %W--%%c** 
+K= 966G5H	J*r   c                     [        U R                  5      S:X  a  [        X5        [        U R                  5      S;  a  [        R                  " SU S35        gg)a  
Check that the observation space is correctly formatted
when dealing with a ``Box()`` space. In particular, it checks:
- that the dimensions are big enough when it is an image, and that the type matches
- that the observation has an expected shape (warn the user if not)
rW   )r,   rW   zYour observation z has an unconventional shape (neither an image, nor a 1D vector). We recommend you to flatten the observation to have only a 1D vector or use a custom policy to properly process the data.N)r;   r2   r4   r!   r"   )r'   r   s     r   _check_box_obsr     sY     ""#q(,2
""#61u %\ \	
 2r   c                 j   U R                  5       n[        U[        5      (       d   S5       e[        U5      S:X  d   S[        U5       35       eUu  pE[        U[        5      (       d
   SU 35       e[        U 5      (       a.  [        U[        R                  5      (       d   e[        XAS5        O[        U[        R                  5      (       a  [        U[        5      (       d   S5       eUR                  5       UR                  R                  5       :X  d7  [        SUR                  5        SUR                  R                  5        35      eUR                  R                  5        H   n [        XF   UR                  U   S5        M"     O[        XAS5        UR                  5       nU R                  U5      n	[        U	5      S:X  d   S[        U	5       S35       eU	u  pJpn[        U[        R                  5      (       Ga  [        U[        5      (       d   S5       e[        U 5      (       aC  [        U[        R                  5      (       d   e[        XAS5        [        X@[!        U
5      U5        UR                  5       UR                  R                  5       :X  d7  [        SUR                  5        SUR                  R                  5        35      eUR                  R                  5        H   n [        XF   UR                  U   S5        M"     O[        XAS5        [        U
[         ["        45      (       d   S5       e[        U[$        5      (       d   S5       e[        U[$        5      (       d   S5       e[        U[        5      (       d   S5       e[        U 5      (       a2  ['        U S5      (       d   eXR)                  US   US   U5      :X  d   egg! [         a!  n[        S	U S
3[        U5      -   5      UeSnAff = f! [         a!  n[        S	U S
3[        U5      -   5      UeSnAff = f)zT
Check the returned values by the env when calling `.reset()` or `.step()` methods.
z)`reset()` must return a tuple (obs, info)rb   z9`reset()` must return a tuple of size 2 (obs, info), not zMThe second element of the tuple return by `reset()` must be a dictionary not rG   z:The observation returned by `reset()` must be a dictionaryzRThe observation keys returned by `reset()` must match the observation space keys: rc   zError while checking key=z: N   z_The `step()` method must return five values: obs, reward, terminated, truncated, info. Actual: z values returned.z9The observation returned by `step()` must be a dictionaryrK   zQThe observation keys returned by `step()` must match the observation space keys: z/The reward returned by `step()` must be a floatz)The `terminated` signal must be a booleanz(The `truncated` signal must be a booleanz;The `info` returned by `step()` must be a python dictionaryrP   rX   rY   )rG   r   rm   r;   dictrS   r   r   r^   r\   r]   r   rx   rJ   rK   rj   floatrd   boolrQ   rP   )r5   r'   r6   reset_returnsrT   r`   r   er9   datar_   
terminated	truncateds                r   _check_returned_valuesr     s   
 IIKMmU++X-XX+}"t&_`cdq`r_s$tt"ICdD!!y%rswrx#yy!C+V[[9999CG<	%v{{	3	3#t$$b&bb$xxz.55::<< "xxzl$/@/G/G/L/L/N.OQ 
 %++002CZ38%6%=%=c%BGL 3 	373   "F88FDt9> 	==@YKGX	Z> 04,C#V[[11#t$$a&aa$ /====?*3U6]DIxxz.55::<< "xxzl$/@/G/G/L/L/N.OQ 
 %++002CZ38%6%=%=c%BFK 3 	362 fucl++^-^^+j$''T)TT'i&&R(RR&dD!!`#``! Cs,----++C,@#nBUW[\\\\ c " Z$'@R%H3q6%QRXYYZH " Z$'@R%H3q6%QRXYYZs0   ,O3P
P#O??P
P2P--P2c                    Sn[        U S5      (       d   SU S35       e[        U S5      (       d   SU S35       e[        U R                  [        R                  5      (       d   SU S35       e[        U R
                  [        R                  5      (       d   SU S35       e[        U 5      (       a<  [        S	5        [        U R                  [        R                  5      (       d   S
5       egg)z
Check that the observation and action spaces are defined and inherit from spaces.Space. For
envs that follow the goal-conditioned standard (previously, the gym.GoalEnv interface) we check
the observation space is gymnasium.spaces.Dict
z,cf. https://gymnasium.farama.org/api/spaces/r'   z'You must specify an observation space ()r6   z"You must specify an action space (z:The observation space must inherit from gymnasium.spaces (z5The action space must inherit from gymnasium.spaces (zWe detected your env to be a GoalEnv because `env.compute_reward()` was defined.
If it's not the case, please rename `env.compute_reward()` to something else to avoid False positives.zGoal conditioned envs (previously gym.GoalEnv) require the observation space to be gymnasium.spaces.Dict.
Note: if your env is not a GoalEnv, please rename `env.compute_reward()` to something else to avoid False positive.N)	rQ   r   r'   r   Spacer6   rS   printr   )r5   
gym_spacess     r   _check_spacesr   i  s    @J3+,,e0WXbWccd.ee,3''[+Mj\YZ)[['v||  R	CJ<qQR  c&&55|9noynzz{7||5Cu	
 #//== 	
9	
= r   r"   c                     U R                   R                  S5      nUc  U(       a  [        R                  " S5        U R                  (       a  U R                  5         U R                  5         g)a0  
Check the instantiated render mode (if any) by calling the `render()`/`close()`
method of the environment.

:param env: The environment to check
:param warn: Whether to output additional warnings
:param headless: Whether to disable render modes
    that require a graphical interface. False by default.
render_modesNzNo render modes was declared in the environment (env.metadata['render_modes'] is None or not defined), you may have trouble when calling `.render()`)metadatagetr!   r"   render_moderenderclose)r5   r"   r   s      r   _check_renderr     sN     <<##N3LMM@ 

IIKr   skip_render_checkc                    [        U [        R                  5      (       d   S5       e[        U 5        U R                  nU R
                  n U R                  SS9  U(       GaS  [        XU5        [        U[        R                  5      (       a  UR                  OSU0nUR                  5        H1  u  px[        U[        R                  5      (       d  M&  [        X5        M3     [        U[        R                  5      (       a  [        R                  " [        R                   " UR"                  5      [        R                   " UR$                  5      :g  5      (       dP  [        R                  " UR"                  S:g  5      (       d(  [        R                  " UR$                  S:g  5      (       a  [&        R(                  " S	5        [        U[        R                  5      (       a`  [        R*                  " [        R,                  " [        R.                  " UR"                  UR$                  /5      5      5      (       d   S
5       e[        U[        R                  5      (       aV  UR0                  [        R0                  " [        R2                  5      :w  a$  [&        R(                  " SUR0                   S35        [        U[        R4                  5      (       a  g[7        XU5        U(       d  [9        X5         [;        U R                  5        [=        U 5        g! [         a  n[        S5      UeSnAff = f! [>         a     gf = f)a6  
Check that an environment follows Gym API.
This is particularly useful when using a custom environment.
Please take a look at https://gymnasium.farama.org/api/env/
for more information about the API.

It also optionally check that the environment is compatible with Stable-Baselines.

:param env: The Gym environment that will be checked
:param warn: Whether to output additional warnings
    mainly related to the interaction with Stable Baselines
:param skip_render_check: Whether to skip the checks for the render method.
    True by default (useful for the CI)
zdYour environment must inherit from the gymnasium.Env class cf. https://gymnasium.farama.org/api/env/r   )seedz1The reset() method must accept a `seed` parameterNr   r*   r,   zWe recommend you to use a symmetric and normalized Box action space (range=[-1, 1]) cf. https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.htmlz@Continuous action space must have a finite lower and upper boundzYour action space has dtype z5, we recommend using np.float32 to avoid cast errors.) r   gymEnvr   r'   r6   rG   	TypeErrorr?   r   r   r:   rr   r   r   r/   absr0   r1   r!   r"   allisfiniterI   r-   float32r=   r   r   r   rN   NotImplementedError)	r5   r"   r   r'   r6   r   
obs_spacesr   r
   s	            r   	check_envr     s    SWW  nmn 
 # --##LT		q	 !#,G1;<Mv{{1[1[&--bdfwax
$**,JC%,,u* -
 lFJJ//FF266,**+rvvl6G6G/HHIIvvl&&",--vvl''1,--MM\
 lFJJ//66BHHl&6&68I8I%JKL  RQR  lFJJ//L4F4F"((SUS]S]J^4^MM.|/A/A.BBwx
 #V__55 3<@ c  5 56 	3c  TKLRSSTd  s*   L 4 L3 
L0L++L03
M ?M )r8   r   )r   )F)TT)+r!   typingr   r   	gymnasiumr   numpyr   r   &stable_baselines3.common.preprocessingr   r    stable_baselines3.common.vec_envr   r	   r   r   r   r   r    r   rx   r&   rr   r4   r   r?   rN   rS   r   r   r^   rp   rd   r   rj   rm   r   r   r   r   r   r   rB   r   r   <module>r      sx        i E> >$ >@5&2F2F!FG @D @
 
3 
UX 
bf 
$#
&** #
3 #
 #
L4
377 4
v|| 4
[a[g[g 4
lp 4
n*CGG * *4cgg 4$ 4T fkk PS X\ (	c5S))	*	  sCx.	
 
66E%rzz367 6FLL 6gj 6os 6r
fjj 
s 
D 
(O] O]FLL O]X^XdXd O]im O]d
sww 
4 
:sww d t 2O377 O$ O$ ORV Or   