
    hH              	          S r SSKrSSKJr  SSKrSSKrSSKJrJ	r	  SSK
JrJrJrJrJr  SS\S\4S jjrS	\R$                  4S
 jrS	\R$                  4S jrSS	\R$                  4S jjrS	\R$                  4S jrS	\R$                  4S jrS	\R$                  4S jrS\4S jr   SS	\R$                  S\S\S\4S jjrg)aL  A set of functions for checking an environment implementation.

This file is originally from the Stable Baselines3 repository hosted on GitHub
(https://github.com/DLR-RM/stable-baselines3/)
Original Author: Antonin Raffin

It also uses some warnings/assertions from the PettingZoo repository hosted on GitHub
(https://github.com/PettingZoo-Team/PettingZoo)
Original Author: J K Terry

This was rewritten and split into "env_checker.py" and "passive_env_checker.py" for invasive and passive environment checking
Original Author: Mark Towers

These projects are covered by the MIT License.
    N)deepcopy)loggerspaces)check_action_spacecheck_observation_spaceenv_render_passive_checkerenv_reset_passive_checkerenv_step_passive_checkerexactreturnc                 2  ^ ^^ [        T 5      [        T5      La  g[        T [        5      (       aM  T R                  5       TR                  5       :H  =(       a%    [	        U UU4S jT R                  5        5       5      $ [        T [
        [        45      (       a=  [        T 5      [        T5      :H  =(       a    [	        U4S j[        T T5       5       5      $ [        T [        R                  5      (       a  T R                  TR                  :X  a  T R                  TR                  :X  aj  T R                  [        :X  a  [	        U4S j[        T T5       5       5      $ T(       a  [        R                  " T T:H  5      $ [        R                  " T TSSS9$ gT T:H  $ )a  Assert equality between data 1 and 2, i.e. observations, actions, info.

Args:
    data_1: data structure 1
    data_2: data structure 2
    exact: whether to compare array exactly or not if false compares with absolute and relative tolerance of 1e-5 (for more information check [np.allclose](https://numpy.org/doc/stable/reference/generated/numpy.allclose.html)).

Returns:
    If observation 1 and 2 are equivalent
Fc              3   J   >#    U  H  n[        TU   TU   T5      v   M     g 7fNdata_equivalence).0kdata_1data_2r   s     U/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/utils/env_checker.py	<genexpr>#data_equivalence.<locals>.<genexpr>/   s)      6
CPaVAYq	599=s    #c              3   @   >#    U  H  u  p[        XT5      v   M     g 7fr   r   )r   o_1o_2r   s      r   r   r   3   s"      2
>Q(#Su-->Q   c              3   @   >#    U  H  u  p[        XT5      v   M     g 7fr   r   )r   abr   s      r   r   r   9   s"      >Qda$Q511>Qr   gh㈵>)rtolatol)type
isinstancedictkeysalltuplelistlenzipnpndarrayshapedtypeobjectallclose)r   r   r   s   ```r   r   r   !   s@    F|4<'	FD	!	!{{}- 
# 6
CI;;=6
 3
 	
 
FUDM	*	*6{c&k) 
c 2
>A&&>Q2
 /
 	
 
FBJJ	'	'<<6<<'FLLFLL,H||v% >A&&>Q   66&F"233;;vvDtLL    envc                    [         R                  " U R                  5      nSUR                  ;   dF  SUR                  ;   Ga  UR                  S   R                  [         R
                  R                  L Ga   U R                  SS9u  p#X R                  ;   d   S5       eU R                  R                  c   S5       e[        U R                  R                  5      nU R                  5       u  pSXPR                  ;   d   S5       eU R                  SS9u  pcX`R                  ;   d   S5       e[        U R                  R                  5      nU R                  5       u  pXR                  ;   d   S5       eU R                  b  U R                  R                  S	L ax  [        X&5      (       d   S
5       e[        XX5      (       d   S5       e[        X&SS9(       d  [        R                  " S5        [        XXSS9(       d  [        R                  " S5        UR                   R"                  UR                   R"                  :X  d   S5       eU R                  SS9u  pXR                  ;   d   S5       eU R                  R                  R                   R"                  UR                   R"                  :w  d   S5       e UR                  R)                  S5      nUb2  UR*                  b$  [        R                  " SUR*                   35        ggg[,        R.                  R1                  S5      e! [$         a  n
['        SU
 35      U
eSn
A
ff = f)zCheck that the environment can be reset with a seed.

Args:
    env: The environment to check

Raises:
    AssertionError: The environment cannot be reset with a random seed,
        even though `seed` or `kwargs` appear in the signature.
seedkwargs{   r4   zVThe observation returned by `env.reset(seed=123)` is not within the observation space.NzExpects the random number generator to have been generated given a seed was passed to reset. Most likely the environment reset function does not call `super().reset(seed=seed)`.zNThe observation returned by `env.reset()` is not within the observation space.FzXUsing `env.reset(seed=123)` is non-deterministic as the observations are not equivalent.zkUsing `env.reset(seed=123)` then `env.reset()` is non-deterministic as the observations are not equivalent.Tr   zHUsing `env.reset(seed=123)` observations are not equal although similar.z[Using `env.reset(seed=123)` then `env.reset()` observations are not equal although similar.zMost likely the environment reset function does not call `super().reset(seed=seed)` as the random generates are not same when the same seeds are passed to `env.reset`.i  zVThe observation returned by `env.reset(seed=456)` is not within the observation space.zMost likely the environment reset function does not call `super().reset(seed=seed)` as the random number generators are not different when different seeds are passed to `env.reset`.zThe environment cannot be reset with a random seed, even though `seed` or `kwargs` appear in the signature. This should never happen, please report this issue. The error was: zThe default seed argument in reset should be `None`, otherwise the environment will by default always be deterministic. Actual default: zLThe `reset` method does not provide a `seed` or `**kwargs` keyword argument.)inspect	signaturereset
parameterskind	ParameterVAR_KEYWORDobservation_space	unwrapped
_np_randomr   specnondeterministicr   r   warnbit_generatorstate	TypeErrorAssertionErrorgetdefaultgymerrorError)r2   r:   obs_1infoseed_123_rng_1obs_2obs_3seed_123_rng_3obs_4obs_5e
seed_params               r   check_reset_seed_determinismrY   G   sX    !!#)),I%%%I(((  *//73D3D3P3PP;	)))-KE...hgh. ((4C CC4%cmm&>&>?N))+KE...`_`. )))-KE...hgh.%cmm&>&>?N))+KE...`_`. xx#(A(AU(J'  nmn  (  A AA  (DAKKb (DAKKu
 ,,22n6R6R6X6XXy yyX )))-KE...hgh. ((66<<!//556G GG6 ))--f5
!j&8&8&DKK##-#5#5"68 'E! iiooZ
 	
  	 VVWUXZ 	s   8H=L& &
M0L??Mc                    [         R                  " U R                  5      nSUR                  ;   dD  SUR                  ;   aE  UR                  S   R                  [         R
                  R                  L a   U R                  0 S9  g[        R                  R                  S5      e! [         a  n[        SU 35      UeSnAff = f)zCheck that the environment can be reset with options.

Args:
    env: The environment to check

Raises:
    AssertionError: The environment cannot be reset with options,
        even though `options` or `kwargs` appear in the signature.
optionsr5   )r[   zThe environment cannot be reset with options, even though `options` or `**kwargs` appear in the signature. This should never happen, please report this issue. The error was: NzPThe `reset` method does not provide an `options` or `**kwargs` keyword argument.)r9   r:   r;   r<   r=   r>   r?   rH   rI   rL   rM   rN   )r2   r:   rW   s      r   check_reset_optionsr\      s     !!#)),II(((I(((  *//73D3D3P3PP	IIbI! iioo^
 	
  	 VVWUXZ 	s   6B% %
C/B>>Cc                    U R                   b  U R                   R                  SL a  gU R                  R                  U5        U R                  R	                  5       nU R                  US9  U R                  U5      u  p4pVn[        U R                  R                  5      nU R                  US9  U R                  U5      u  ppnU R                  R                  R                  R                  UR                  R                  :X  d   S5       e[        X95      (       d   S5       e[        X9SS9(       d  [        R                  " S5        [        XJ5      (       d   S5       e[        XJSS9(       d  [        R                  " S	5        [        X[SS9(       d   S
5       eUSL a  USL d   S5       e[        UU5      (       d   S5       e[        X}SS9(       d  [        R                  " S5        gg)a  Check that the environment steps deterministically after reset.

Note: This check assumes that seeded `reset()` is deterministic (it must have passed `check_reset_seed`) and that `step()` returns valid values (passed `env_step_passive_checker`).
Note: A single step should be enough to assert that the state transition function is deterministic (at least for most environments).

Raises:
    AssertionError: The environment cannot be step deterministically after resetting with a random seed,
        or it truncates after 1 step.
NTr7   z9The `.np_random` is not properly been updated after step.zODeterministic step observations are not equivalent for the same seed and actionr8   zOStep observations are not equal although similar given the same seed and actionzJDeterministic step rewards are not equivalent for the same seed and actionzJStep rewards are not equal although similar given the same seed and actionzNDeterministic step termination are not equivalent for the same seed and actionFzBEnvironment truncates after 1 step, something has gone very wrong.zGDeterministic step info are not equivalent for the same seed and actionzGStep info are not equal although similar given the same seed and action)rC   rD   action_spacer4   sampler;   stepr   rA   rB   rF   rG   r   r   rE   )r2   r4   actionobs_0rew_0term_0trunc_0info_0
seeded_rngrO   rew_1term_1trunc_1info_1s                 r   check_step_determinismrl      s    xx 9 9T A$$$&FII4I,/HHV,<)E&6&.s}}/G/G&HJII4I,/HHV,<)E&6 	  ..44##))	*C CC	*   YXY  E5]	
   TST  E5X	
 d XWX  	5W-LKL-   Q QQ  F$7U	
 8r1   c                     [         R                  " U R                  5      nSUR                  ;   a  [        R
                  " S5        gg)zMakes sure support for deprecated `return_info` argument is dropped.

Args:
    env: The environment to check
Raises:
    UserWarning
return_infoz`return_info` is deprecated as an optional argument to `reset`. `reset`should now always return `obs, info` where `obs` is an observation, and `info` is a dictionarycontaining additional information.N)r9   r:   r;   r<   r   rE   )r2   r:   s     r   #check_reset_return_info_deprecationro      s<     !!#)),I	,,,1	
 -r1   c                 l    [        U SS5      n[        U5      (       a  [        R                  " S5        gg)zMakes sure support for deprecated function `seed` is dropped.

Args:
    env: The environment to check
Raises:
    UserWarning
r4   NzOfficial support for the `seed` function is dropped. Standard practice is to reset gymnasium environments using `env.reset(seed=<desired seed>)`)getattrcallabler   rE   )r2   seed_fns     r   check_seed_deprecationrt   
  s4     c64(Gj	
 r1   c                 >   U R                  5       n[        U[        5      (       d   S[        U5       S35       e[	        U5      S:X  d   S[	        U5       35       eUu  p#X R
                  ;   d   S5       e[        U[        5      (       d   S[        U5       35       eg)zChecks that :meth:`reset` correctly returns a tuple of the form `(obs , info)`.

Args:
    env: The environment to check
Raises:
    AssertionError depending on spec violation
zThe result returned by `env.reset()` was not a tuple of the form `(obs, info)`, where `obs` is a observation and `info` is a dictionary containing additional information. Actual type: ``   zBCalling the reset method did not return a 2-tuple, actual length: zPThe first element returned by `env.reset()` is not within the observation space.zPThe second element returned by `env.reset()` was not a dictionary, actual type: N)r;   r#   r'   r"   r)   r@   r$   )r2   resultobsrP   s       r   check_reset_return_typerz     s     YY[F  S 
C  DH  IO  DP  CQ  QR  SS  	FqZ	KCPVK=YZ IC$$$ZYZ$d  g	YZ^_cZdYefg r1   
space_typec           	         [        U [        R                  5      (       Ga  [        R                  " [        R
                  " U R                  [        R                  * 5      5      (       a  [        R                  " SU S35        [        R                  " [        R
                  " U R                  [        R                  5      5      (       a  [        R                  " SU S35        US:X  Ga  [        U R                  5      S:X  Ga  [        R                  " [        R                  " U R                  [        R                  " U R                  5      :g  [        R                  " U R                  5      [        R                  " U R                  5      :g  5      5      (       dP  [        R                  " U R                  S:  5      (       d(  [        R                  " U R                  S:  5      (       a  [        R                  " S5        gggg[        U [        R                   5      (       a  U R                   H  n[#        X!5        M     g[        U [        R$                  5      (       a#  U R'                  5        H  n[#        X!5        M     gg)	z]Check the space limit for only the Box space as a test that only runs as part of `check_env`.zA Box z< space minimum value is -infinity. This is probably too low.z< space maximum value is infinity. This is probably too high.ra      zFor Box action spaces, we recommend using a symmetric and normalized space (range=[-1, 1] or [0, 1]). See https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.html for more information.N)r#   r   Boxr+   anyequallowinfr   rE   highr)   r-   logical_and
zeros_likeabsTuplecheck_space_limitDictvalues)spacer{   subspaces      r   r   r   3  s   %$$66"((599rvvg.//KK$`a 66"((5::rvv.//KK$`a
 !5;;1$FF!IIuyy)AAFF599-

1CC  vveii"n--vvejj1n-- KKz . % "" 
E6<<	(	(Hh3 %	E6;;	'	'Hh3 ' 
(r1   rE   skip_render_checkskip_close_checkc                    Ub  [         R                  " S5        [        U [        R                  5      (       di  [        U R                  R                  5      S:X  d#  [        U R                  R                  5      S:X  a  [        S5      e[        S[        U 5       S35      eU R                  U La'  [         R                  " SU  S	U R                   S
35        U R                  R                  SS5      (       a   [        R                  R                  U 5      n O@U R                  R                  SS5      (       a  [        R                  R                  U 5      n [!        U S5      (       d  [#        S5      e[%        U R&                  5        [)        U R&                  S5        [!        U S5      (       d  [#        S5      e[+        U R,                  5        [)        U R,                  S5        [/        U 5        [1        U 5        [3        U 5        [5        U 5        [7        U 5        [9        U 5        [;        X R&                  R=                  5       5        [?        U 5        U(       d  U R@                  b  [C        U 5        U RD                  b[  U R                  S    HG  nU RD                  RG                  US9nURI                  5         [C        U5        URK                  5         MI     O[         R                  " S5        U(       dJ  U RD                  b<  U RD                  RG                  5       nURK                  5          URK                  5         ggg! [L         a#  n[         R                  " SU 35         SnAgSnAff = f)aZ  Check that an environment follows Gymnasium's API.

.. py:currentmodule:: gymnasium.Env

To ensure that an environment is implemented "correctly", ``check_env`` checks that the :attr:`observation_space` and :attr:`action_space` are correct.
Furthermore, the function will call the :meth:`reset`, :meth:`step` and :meth:`render` functions with a variety of values.

We highly recommend users call this function after an environment is constructed and within a project's continuous integration to keep an environment update with Gymnasium's API.

Args:
    env: The Gym environment that will be checked
    warn: Ignored, previously silenced particular warnings
    skip_render_check: Whether to skip the checks for the render method. False by default (useful for the CI)
    skip_close_check: Whether to skip the checks for the close method. False by default
Nz/`check_env(warn=...)` parameter is now ignored.z<class 'gym.core.Env'>z<class 'gym.core.Wrapper'>zGym is incompatible with Gymnasium, please update the environment class to `gymnasium.Env`. See https://gymnasium.farama.org/introduction/create_custom_env/ for more info.zIThe environment must inherit from the gymnasium.Env class, actual class: zQ. See https://gymnasium.farama.org/introduction/create_custom_env/ for more info.zThe environment (z+) is different from the unwrapped version (z). This could effect the environment checker as the environment most likely has a wrapper applied to it. We recommend using the raw environment for `check_env` using `env.unwrapped`.jaxFtorchr^   z}The environment must specify an action space. See https://gymnasium.farama.org/introduction/create_custom_env/ for more info.ra   r@   zThe environment must specify an observation space. See https://gymnasium.farama.org/introduction/create_custom_env/ for more info.observationrender_modes)render_modezNot able to test alternative render modes due to the environment not having a spec. Try instantiating the environment through `gymnasium.make`z_Calling `env.close()` on the closed environment should be allowed, but it raised an exception: )'r   rE   r#   rL   Envstr	__class____base__rH   r"   rA   metadatarJ   wrappers
JaxToNumpyTorchToNumpyhasattrAttributeErrorr   r^   r   r   r@   rt   ro   rz   rY   r\   r	   r
   r_   rl   r   r   rC   maker;   close	Exception)r2   rE   r   r   r   new_envrW   s          r   	check_envr   Y  s   * EFc377##&&'+CC3==))*.JJb 
 [\`ad\e[f gb b  }}Cu$OPSP]P]  _U  V	
 ||u%%ll%%c*			'5	)	)ll'', 3'' L
 	
 s''(c&&13+,, Q
 	
 C112c++]; 3',C  % c"S"2"2"9"9";< 3 ??&&s+88"||N;((--K-@*73	  < KK a  4((--/	MMO	 !5
  	KKqrsqtu 	s   3M 
M3M..M3)F)r6   )NFF)__doc__r9   copyr   numpyr+   	gymnasiumrL   r   r   #gymnasium.utils.passive_env_checkerr   r   r   r	   r
   boolr   r   rY   r\   rl   ro   rt   rz   r   r   r    r1   r   <module>r      s         $ # D # T # LV
cgg V
r
SWW 
8:
 :
z
SWW 
"
 
 g g2#4 #4P #"	e	e
e e 	er1   