
    h_                    6   S r SSKJr  SSKJr  SSKJrJrJrJ	r	  SSK
rSSKrSSKJrJrJr  SSKJr  SSKJr  \(       a  SS	KJr  \	" S
5      r/ SQr " S S\5      r " S S\\\\4   5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)z'Base class for vectorized environments.    )annotations)Enum)TYPE_CHECKINGAnyGenericTypeVarN)ActTypeObsTypeRenderFrame)warn)seeding)EnvSpec	ArrayType)	VectorEnvVectorWrapperVectorObservationWrapperVectorActionWrapperVectorRewardWrapperr   AutoresetModec                  $    \ rS rSrSrSrSrSrSrg)r   !   zSEnum representing the different autoreset modes, next step, same step and disabled.NextStepSameStepDisabled N)	__name__
__module____qualname____firstlineno____doc__	NEXT_STEP	SAME_STEPDISABLED__static_attributes__r       U/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/vector/vector_env.pyr   r   !   s    ]IIHr%   r   c                  x   \ rS rSr% Sr0 rS\S'   SrS\S'   SrS\S	'   S
r	S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   Sr
S\S'   SrS\S'   SSS.     S&S jjr    S'S jrS(S jrS)S jrS)S jr\S*S j5       r\R&                  S+S j5       r\S,S  j5       r\S! 5       r        S-S" jrS# rS.S$ jrS%rg)/r   )   a  Base class for vectorized environments to run multiple independent copies of the same environment in parallel.

Vector environments can provide a linear speed-up in the steps taken per second through sampling multiple
sub-environments at the same time. Gymnasium contains two generalised Vector environments: :class:`AsyncVectorEnv`
and :class:`SyncVectorEnv` along with several custom vector environment implementations.
For :func:`reset` and :func:`step` batches `observations`, `rewards`,  `terminations`, `truncations` and
`info` for each sub-environment, see the example below. For the `rewards`, `terminations`, and `truncations`,
the data is packaged into a NumPy array of shape `(num_envs,)`. For `observations` (and `actions`, the batching
process is dependent on the type of observation (and action) space, and generally optimised for neural network
input/outputs. For `info`, the data is kept as a dictionary such that a key will give the data for all sub-environment.

For creating environments, :func:`make_vec` is a vector environment equivalent to :func:`make` for easily creating
vector environments that contains several unique arguments for modifying environment qualities, number of environment,
vectorizer type, vectorizer arguments.

To avoid having to wait for all sub-environments to terminated before resetting, implementations can autoreset
sub-environments on episode end (`terminated or truncated is True`). This is crucial for correct implementing training
algorithms with vector environments. By default, Gymnasium's implementation uses `next-step` autoreset, with
:class:`AutoresetMode` enum as the options. The mode used by vector environment should be available in `metadata["autoreset_mode"]`.
Warning, some vector implementations or training algorithms will only support particular autoreset modes.
For more information, read https://farama.org/Vector-Autoreset-Mode.

Note:
    The info parameter of :meth:`reset` and :meth:`step` was originally implemented before v0.25 as a list
    of dictionary for each sub-environment. However, this was modified in v0.25+ to be a dictionary with a NumPy
    array for each key. To use the old info style, utilise the :class:`DictInfoToList` wrapper.

Examples:
    >>> import gymnasium as gym
    >>> envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync", wrappers=(gym.wrappers.TimeAwareObservation,))
    >>> envs = gym.wrappers.vector.ClipReward(envs, min_reward=0.2, max_reward=0.8)
    >>> envs
    <ClipReward, SyncVectorEnv(CartPole-v1, num_envs=3)>
    >>> envs.num_envs
    3
    >>> envs.action_space
    MultiDiscrete([2 2 2])
    >>> envs.observation_space
    Box([[-4.80000019        -inf -0.41887903        -inf  0.        ]
     [-4.80000019        -inf -0.41887903        -inf  0.        ]
     [-4.80000019        -inf -0.41887903        -inf  0.        ]], [[4.80000019e+00            inf 4.18879032e-01            inf
      5.00000000e+02]
     [4.80000019e+00            inf 4.18879032e-01            inf
      5.00000000e+02]
     [4.80000019e+00            inf 4.18879032e-01            inf
      5.00000000e+02]], (3, 5), float64)
    >>> observations, infos = envs.reset(seed=123)
    >>> observations
    array([[ 0.01823519, -0.0446179 , -0.02796401, -0.03156282,  0.        ],
           [ 0.02852531,  0.02858594,  0.0469136 ,  0.02480598,  0.        ],
           [ 0.03517495, -0.000635  , -0.01098382, -0.03203924,  0.        ]])
    >>> infos
    {}
    >>> _ = envs.action_space.seed(123)
    >>> actions = envs.action_space.sample()
    >>> observations, rewards, terminations, truncations, infos = envs.step(actions)
    >>> observations
    array([[ 0.01734283,  0.15089367, -0.02859527, -0.33293587,  1.        ],
           [ 0.02909703, -0.16717631,  0.04740972,  0.3319138 ,  1.        ],
           [ 0.03516225, -0.19559774, -0.01162461,  0.25715804,  1.        ]])
    >>> rewards
    array([0.8, 0.8, 0.8])
    >>> terminations
    array([False, False, False])
    >>> truncations
    array([False, False, False])
    >>> infos
    {}
    >>> envs.close()

The Vector Environments have the additional attributes for users to understand the implementation

- :attr:`num_envs` - The number of sub-environment in the vector environment
- :attr:`observation_space` - The batched observation space of the vector environment
- :attr:`single_observation_space` - The observation space of a single sub-environment
- :attr:`action_space` - The batched action space of the vector environment
- :attr:`single_action_space` - The action space of a single sub-environment
dict[str, Any]metadataNEnvSpec | Nonespecz
str | Nonerender_modeFboolclosed	gym.Spaceobservation_spaceaction_spacesingle_observation_spacesingle_action_spaceintnum_envsznp.random.Generator | None
_np_random
int | None_np_random_seedseedoptionsc               R    Ub$  [         R                  " U5      u  U l        U l        gg)a  Reset all parallel environments and return a batch of initial observations and info.

Args:
    seed: The environment reset seed
    options: If to return the options

Returns:
    A batch of observations and info from the vectorized environment.

Example:
    >>> import gymnasium as gym
    >>> envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync")
    >>> observations, infos = envs.reset(seed=42)
    >>> observations
    array([[ 0.0273956 , -0.00611216,  0.03585979,  0.0197368 ],
           [ 0.01522993, -0.04562247, -0.04799704,  0.03392126],
           [-0.03774345, -0.02418869, -0.00942293,  0.0469184 ]],
          dtype=float32)
    >>> infos
    {}
N)r   	np_randomr7   r9   selfr;   r<   s      r&   resetVectorEnv.reset   s)    6 4;4E4Ed4K1DOT1 r%   c                :    [        U R                  5        S35      e)aW  Take an action for each parallel environment.

Args:
    actions: Batch of actions with the :attr:`action_space` shape.

Returns:
    Batch of (observations, rewards, terminations, truncations, infos)

Note:
    As the vector environments autoreset for a terminating and truncating sub-environments, this will occur on
    the next step after `terminated or truncated is True`.

Example:
    >>> import gymnasium as gym
    >>> import numpy as np
    >>> envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync")
    >>> _ = envs.reset(seed=42)
    >>> actions = np.array([1, 0, 1], dtype=np.int32)
    >>> observations, rewards, terminations, truncations, infos = envs.step(actions)
    >>> observations
    array([[ 0.02727336,  0.18847767,  0.03625453, -0.26141977],
           [ 0.01431748, -0.24002443, -0.04731862,  0.3110827 ],
           [-0.03822722,  0.1710671 , -0.00848456, -0.2487226 ]],
          dtype=float32)
    >>> rewards
    array([1., 1., 1.])
    >>> terminations
    array([False, False, False])
    >>> terminations
    array([False, False, False])
    >>> infos
    {}
z" step function is not implemented.NotImplementedError__str__r@   actionss     r&   stepVectorEnv.step   s    H "T\\^$44V"WXXr%   c                :    [        U R                  5        S35      e)zReturns the rendered frames from the parallel environments.

Returns:
    A tuple of rendered frames from the parallel environments
z$ render function is not implemented.rD   r@   s    r&   renderVectorEnv.render   s#     "||~BC
 	
r%   c                Z    U R                   (       a  gU R                  " S0 UD6  SU l         g)a  Close all parallel environments and release resources.

It also closes all the existing image viewers, then calls :meth:`close_extras` and set
:attr:`closed` as ``True``.

Warnings:
    This function itself does not close the environments, it should be handled
    in :meth:`close_extras`. This is generic for both synchronous and asynchronous
    vectorized environments.

Note:
    This will be automatically called when garbage collected or program exited.

Args:
    **kwargs: Keyword arguments passed to :meth:`close_extras`
NTr   )r/   close_extrasr@   kwargss     r&   closeVectorEnv.close   s&    " ;;#F#r%   c                    g)zCClean up the extra resources e.g. beyond what's in this base class.Nr   rQ   s     r&   rP   VectorEnv.close_extras   s    r%   c                x    U R                   c"  [        R                  " 5       u  U l         U l        U R                   $ zReturns the environment's internal :attr:`_np_random` that if not set will initialise with a random seed.

Returns:
    Instances of `np.random.Generator`
)r7   r   r>   r9   rL   s    r&   r>   VectorEnv.np_random   s0     ??"4;4E4E4G1DOT1r%   c                    Xl         SU l        g )N)r7   r9   r@   values     r&   r>   rY      s    !r%   c                x    U R                   c"  [        R                  " 5       u  U l        U l         U R                   $ )aq  Returns the environment's internal :attr:`_np_random_seed` that if not set will first initialise with a random int as seed.

If :attr:`np_random_seed` was set directly instead of through :meth:`reset` or :meth:`set_np_random_through_seed`,
the seed will take the value -1.

Returns:
    int: the seed of the current `np_random` or -1, if the seed of the rng is unknown
)r9   r   r>   r7   rL   s    r&   np_random_seedVectorEnv.np_random_seed  s4     '4;4E4E4G1DOT1###r%   c                    U $ )zReturn the base environment.r   rL   s    r&   	unwrappedVectorEnv.unwrapped  s	     r%   c           	        UR                  5        GH  u  pEUS:X  a6  SU;   a  US   nO$[        R                  " U R                  S[        S9nXVU'   GO0[        U[        5      (       a#  U R                  UR                  U0 5      XS5      nOXA;  a  [        U5      [        [        [        4;   d(  [        [        U5      [        R                  5      (       a)  [        R                  " U R                  [        U5      S9nO[        U[        R                   5      (       a8  [        R                  " U R                  /UR"                  Q7UR$                  S9nO)[        R                  " U R                  S[        S9nOX   nXVU'   UR                  SU 3[        R                  " U R                  [        R&                  S95      nSXs'   XgsX'   USU 3'   GM     U$ )a\  Add env info to the info dictionary of the vectorized environment.

Given the `info` of a single environment add it to the `infos` dictionary
which represents all the infos of the vectorized environment.
Every `key` of `info` is paired with a boolean mask `_key` representing
whether or not the i-indexed environment has this `info`.

Args:
    vector_infos (dict): the infos of the vectorized environment
    env_info (dict): the info coming from the single environment
    env_num (int): the index of the single environment

Returns:
    infos (dict): the (updated) infos of the vectorized environment
	final_obsN)
fill_valuedtype)rg   _T)itemsnpfullr6   object
isinstancedict	_add_infogettyper5   floatr.   
issubclassnumberzerosndarrayshaperg   bool_)r@   vector_infosenv_infoenv_numkeyr]   array
array_masks           r&   ro   VectorEnv._add_info  s   $ #..*JCk!,.(5EGGDMMd&QE!&gE4((|'7'7R'@%Q *E{sE4&88JURYY= = !#d5k J#E2::66 "!]]9U[[9!
 !#$f U )-E "'g &))C5	288DMMBJ #'J :?6L|auI6U +V r%   c                J    [        U SS5      (       d  U R                  5         gg)zCloses the vector environment.r/   TN)getattrrS   rL   s    r&   __del__VectorEnv.__del__S  s    tXt,,JJL -r%   c                    U R                   c&  U R                  R                   SU R                   S3$ U R                  R                   SU R                   R                   SU R                   S3$ )zReturns a string representation of the vector environment.

Returns:
    A string containing the class name, number of environments and environment spec id
z
(num_envs=)(z, num_envs=)r,   	__class__r   r6   idrL   s    r&   __repr__VectorEnv.__repr__X  se     99nn--.jqII >>**+1TYY\\N+dmm_TUVr%   )r7   r9   r/   )r;   r8   r<   dict[str, Any] | Nonereturntuple[ObsType, dict[str, Any]]rH   r	   r   z?tuple[ObsType, ArrayType, ArrayType, ArrayType, dict[str, Any]]r   ztuple[RenderFrame, ...] | NonerR   r   r   np.random.Generatorr]   r   r   r8   )ry   r)   rz   r)   r{   r5   r   r)   )r   str)r   r   r   r   r    r*   __annotations__r,   r-   r/   r7   r9   rA   rI   rM   rS   rP   propertyr>   setterr_   rb   ro   r   r   r$   r   r%   r&   r   r   )   sB   M^  "Hn!D."K"FD  ''""M-1J*1"&OZ&
  )-	L L '	L
 
(L<$Y$Y	H$YL
.   " " $ $  =*=6D=OR=	=~
r%   r   c                  j   \ rS rSrSrS S jrSSS.     S!S jjr    S"S jrS#S jrS$S	 jr	S$S
 jr
\S 5       rS r\S%S j5       r\R                  S&S j5       r\S%S j5       r\R                  S&S j5       r\S%S j5       r\R                  S&S j5       r\S%S j5       r\R                  S 5       r\S'S j5       r\S(S j5       r\R                  S)S j5       r\S*S j5       r\S 5       r\R                  S 5       r\S+S j5       r\S#S j5       r\S 5       r\R                  S,S j5       rSrg)-r   if  a  Wraps the vectorized environment to allow a modular transformation.

This class is the base class for all wrappers for vectorized environments. The subclass
could override some methods to change the behavior of the original vectorized environment
without touching the original code.

Note:
    Don't forget to call ``super().__init__(env)`` if the subclass overrides :meth:`__init__`.
c                    Xl         [        U[        5      (       d   S[        U5       35       eSU l        SU l        SU l        SU l        SU l        g)zWInitialize the vectorized environment wrapper.

Args:
    env: The environment to wrap
z:Expected env to be a `gymnasium.vector.VectorEnv` but got N)	envrm   r   rq   _observation_space_action_space_single_observation_space_single_action_space	_metadata)r@   r   s     r&   __init__VectorWrapper.__init__q  sc     
 
 	TGS	{S	T 
 59/3;?&6:!04r%   Nr:   c               4    U R                   R                  XS9$ )z-Reset all environment using seed and options.r:   )r   rA   r?   s      r&   rA   VectorWrapper.reset  s     xx~~4~99r%   c                8    U R                   R                  U5      $ )zKStep through all environments using the actions returning the batched data.)r   rI   rG   s     r&   rI   VectorWrapper.step  s     xx}}W%%r%   c                6    U R                   R                  5       $ )z9Returns the render mode from the base vector environment.)r   rM   rL   s    r&   rM   VectorWrapper.render  s    xx  r%   c                :    U R                   R                  " S0 UD6$ )zClose all environments.r   )r   rS   rQ   s     r&   rS   VectorWrapper.close  s    xx~~'''r%   c                :    U R                   R                  " S0 UD6$ )zClose all extra resources.r   )r   rP   rQ   s     r&   rP   VectorWrapper.close_extras  s    xx$$.v..r%   c                .    U R                   R                  $ )z(Return the base non-wrapped environment.)r   rb   rL   s    r&   rb   VectorWrapper.unwrapped  s     xx!!!r%   c                P    SU R                   R                   SU R                   S3$ )z?Return the string representation of the vectorized environment.<z, >)r   r   r   rL   s    r&   r   VectorWrapper.__repr__  s&    4>>**+2dhhZq99r%   c                `    U R                   c  U R                  R                  $ U R                   $ )z5Gets the observation space of the vector environment.)r   r   r1   rL   s    r&   r1   VectorWrapper.observation_space  s,     ""*88---&&&r%   c                    Xl         g)z5Sets the observation space of the vector environment.N)r   r@   spaces     r&   r1   r     s
     #(r%   c                `    U R                   c  U R                  R                  $ U R                   $ )z0Gets the action space of the vector environment.)r   r   r2   rL   s    r&   r2   VectorWrapper.action_space  s,     %88(((!!!r%   c                    Xl         g)z0Sets the action space of the vector environment.N)r   r   s     r&   r2   r     s
     #r%   c                `    U R                   c  U R                  R                  $ U R                   $ )z<Gets the single observation space of the vector environment.)r   r   r3   rL   s    r&   r3   &VectorWrapper.single_observation_space  s,     ))188444---r%   c                    Xl         g)z<Sets the single observation space of the vector environment.N)r   r   s     r&   r3   r     s
     */&r%   c                `    U R                   c  U R                  R                  $ U R                   $ )z7Gets the single action space of the vector environment.)r   r   r4   rL   s    r&   r4   !VectorWrapper.single_action_space  s,     $$,88///(((r%   c                    Xl         g)z7Sets the single action space of the vector environment.N)r   r   s     r&   r4   r     s
     %*!r%   c                .    U R                   R                  $ )zBGets the wrapped vector environment's num of the sub-environments.)r   r6   rL   s    r&   r6   VectorWrapper.num_envs  s     xx   r%   c                .    U R                   R                  $ rX   r   r>   rL   s    r&   r>   VectorWrapper.np_random  s     xx!!!r%   c                $    XR                   l        g Nr   r\   s     r&   r>   r     s    "r%   c                .    U R                   R                  $ )zBThe seeds of the vector environment's internal :attr:`_np_random`.)r   r_   rL   s    r&   r_   VectorWrapper.np_random_seed  s     xx&&&r%   c                `    U R                   b  U R                   $ U R                  R                  $ )z'The metadata of the vector environment.)r   r   r*   rL   s    r&   r*   VectorWrapper.metadata  s(     >>%>>!xx   r%   c                    Xl         g r   )r   r\   s     r&   r*   r     s    r%   c                .    U R                   R                  $ )z2Gets the specification of the wrapped environment.)r   r,   rL   s    r&   r,   VectorWrapper.spec  s     xx}}r%   c                .    U R                   R                  $ )z4Returns the `render_mode` from the base environment.)r   r-   rL   s    r&   r-   VectorWrapper.render_mode  s     xx###r%   c                .    U R                   R                  $ )zIf the environment has closes.r   r/   rL   s    r&   r/   VectorWrapper.closed  s     xxr%   c                $    XR                   l        g r   r   r\   s     r&   r/   r     s    r%   )r   r   r   r   r   r   r   r   r;   zint | list[int] | Noner<   r   r   r   r   r   r   )r   r0   )r   r0   )r   r5   r   r   r   )r   r+   )r]   r.   )r   r   r   r   r    r   rA   rI   rM   rS   rP   r   rb   r   r1   r   r2   r3   r4   r6   r>   r_   r*   r,   r-   r/   r$   r   r%   r&   r   r   f  s   5( (,)-	: %: '	:
 
(:&&	H&!(/ " ": ' ' ( ( " " # # . . $$/ %/ ) ) *  * ! ! " " # # ' ' ! ! __    $ $   ]]   r%   r   c                  j   ^  \ rS rSrSrS
U 4S jjrSSS.     SS jjr    SS jrSS jrS	r	U =r
$ )r   i  zWraps the vectorized environment to allow a modular transformation of the observation.

Equivalent to :class:`gymnasium.ObservationWrapper` for vectorized environments.
c                   > [         TU ]  U5        SUR                  ;  a  [        SU S35        gUR                  S   [        R
                  :X  d$  UR                  S   [        R                  :X  d   egg)zcVector observation wrapper that batch transforms observations.

Args:
    env: Vector environment.
autoreset_modezVector environment (z+) is missing `autoreset_mode` metadata key.N)superr   r*   r   r   r!   r#   )r@   r   r   s     r&   r   !VectorObservationWrapper.__init__  sy     	3<</&se+VW
 -.-2I2II<< 01]5K5KKLK Jr%   Nr:   c               ^    U R                   R                  XS9u  p4U R                  U5      U4$ )z_Modifies the observation returned from the environment ``reset`` using the :meth:`observation`.r:   )r   rA   observations)r@   r;   r<   r   infoss        r&   rA   VectorObservationWrapper.reset#  s1     #hhnn$nH  .55r%   c                l    U R                   R                  U5      u  p#pEnU R                  U5      UUUU4$ )z^Modifies the observation returned from the environment ``step`` using the :meth:`observation`.)r   rI   r   r@   rH   r   rewardsterminationstruncationsr   s          r&   rI   VectorObservationWrapper.step-  sD     CG((--PWBX?|%l+
 	
r%   c                    [         e)zDefines the vector observation transformation.

Args:
    observations: A vector observation from the environment

Returns:
    the transformed observation
rE   )r@   r   s     r&   r   %VectorObservationWrapper.observations:  
     "!r%   r   r   r   r   )r   r
   r   r
   )r   r   r   r   r    r   rA   rI   r   r$   __classcell__)r   s   @r&   r   r     sW    
( (,)-	6 %6 '	6
 
(6

	H
	" 	"r%   r   c                  4    \ rS rSrSr    SS jrSS jrSrg)	r   iF  zWraps the vectorized environment to allow a modular transformation of the actions.

Equivalent of :class:`gymnasium.ActionWrapper` for vectorized environments.
c                V    U R                   R                  U R                  U5      5      $ )zHSteps through the environment using a modified action by :meth:`action`.)r   rI   rH   rG   s     r&   rI   VectorActionWrapper.stepL  s      xx}}T\\'233r%   c                    [         e)zTransform the actions before sending them to the environment.

Args:
    actions (ActType): the actions to transform

Returns:
    ActType: the transformed actions
r   rG   s     r&   rH   VectorActionWrapper.actionsR  r   r%   r   Nr   )rH   r	   r   r	   )r   r   r   r   r    rI   rH   r$   r   r%   r&   r   r   F  s!    
44	H4	"r%   r   c                  4    \ rS rSrSr    SS jrSS jrSrg)	r   i^  zWraps the vectorized environment to allow a modular transformation of the reward.

Equivalent of :class:`gymnasium.RewardWrapper` for vectorized environments.
c                h    U R                   R                  U5      u  p#pEnX R                  U5      XEU4$ )zLSteps through the environment returning a reward modified by :meth:`reward`.)r   rI   r   r   s          r&   rI   VectorRewardWrapper.stepd  s6     CG((--PWBX?|%\\'2LuTTr%   c                    [         e)zTransform the reward before returning it.

Args:
    rewards (array): the reward to transform

Returns:
    array: the transformed reward
r   )r@   r   s     r&   r   VectorRewardWrapper.rewardsk  r   r%   r   Nr   )r   r   r   r   )r   r   r   r   r    rI   r   r$   r   r%   r&   r   r   ^  s$    
UU	HU	"r%   r   ) r    
__future__r   enumr   typingr   r   r   r   numpyrj   	gymnasiumgymgymnasium.corer	   r
   r   gymnasium.loggerr   gymnasium.utilsr   gymnasium.envs.registrationr   r   __all__r   r   r   r   r   r   r   r%   r&   <module>r      s    - "  7 7   8 8 ! # 3K 	D z)34 zz	c I c L7"} 7"t"- "0"- "r%   