
    h                        % S r SSKJr  SSKJrJrJr  SSKJrJ	r	J
r
Jr  SSKrSSKJr  SSKJr  \" SSS	9r\R&                  \R(                     rS
\S'    " S S\	\   5      rg)z(Implementation of the `Space` metaclass.    )annotations)IterableMappingSequence)AnyGeneric	TypeAliasTypeVarN)seedingT_covT)	covariantr	   MaskNDArrayc                      \ rS rSrSr   S     SS jjr\SS j5       r\SS j5       r\SS j5       r	SSS jjr
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rSrg)Space   a  Superclass that is used to define observation and action spaces.

Spaces are crucially used in Gym to define the format of valid actions and observations.
They serve various purposes:

* They clearly define how to interact with environments, i.e. they specify what actions need to look like
  and what observations will look like
* They allow us to work with highly structured data (e.g. in the form of elements of :class:`Dict` spaces)
  and painlessly transform them into flat arrays that can be used in learning code
* They provide a method to sample random elements. This is especially useful for exploration and debugging.

Different spaces can be combined hierarchically via container spaces (:class:`Tuple` and :class:`Dict`) to build a
more expressive space

Warning:
    Custom observation & action spaces can inherit from the ``Space``
    class. However, most use-cases should be covered by the existing space
    classes (e.g. :class:`Box`, :class:`Discrete`, etc...), and container classes (:class:`Tuple` &
    :class:`Dict`). Note that parametrized probability distributions (through the
    :meth:`Space.sample()` method), and batching functions (in :class:`gym.vector.VectorEnv`), are
    only well-defined for instances of spaces provided in gym by default.
    Moreover, some implementations of Reinforcement Learning algorithms might
    not handle custom spaces properly. Use custom spaces with care.
Nc                   Uc  SO
[        U5      U l        Uc  SO[        R                  " U5      U l        SU l        UbB  [        U[        R                  R                  5      (       a  X0l        gU R                  U5        gg)ak  Constructor of :class:`Space`.

Args:
    shape (Optional[Sequence[int]]): If elements of the space are numpy arrays, this should specify their shape.
    dtype (Optional[Type | str]): If elements of the space are numpy arrays, this should specify their dtype.
    seed: Optionally, you can use this argument to seed the RNG that is used to sample from the space
N)	tuple_shapenpdtype
_np_random
isinstancerandom	Generatorseed)selfshaper   r   s       P/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/space.py__init__Space.__init__.   sc     $mdu"]T
$		 3 344"&		$	     c                    U R                   c  U R                  5         U R                   c  [        R                  " 5       u  U l         nU R                   $ )zLazily seed the PRNG since this is expensive and only needed if sampling from this space.

As :meth:`seed` is not guaranteed to set the `_np_random` for particular seeds. We add a
check after :meth:`seed` to set a new random number generator.
)r   r   r   	np_random)r   _s     r   r#   Space.np_randomD   sB     ??"IIK ??"!(!2!2!4DOQr!   c                    U R                   $ )z7Return the shape of the space as an immutable property.)r   r   s    r   r   Space.shapeT   s     {{r!   c                    [         e)zNChecks whether this space can be flattened to a :class:`gymnasium.spaces.Box`.NotImplementedErrorr'   s    r   is_np_flattenableSpace.is_np_flattenableY   s
     "!r!   c                    [         e)a   Randomly sample an element of this space.

Can be uniform or non-uniform sampling based on boundedness of space.

The binary mask and the probability mask can't be used at the same time.

Args:
    mask: A mask used for random sampling, expected ``dtype=np.int8`` and see sample implementation for expected shape.
    probability: A probability mask used for sampling according to the given probability distribution, expected ``dtype=np.float64`` and see sample implementation for expected shape.

Returns:
    A sampled actions from the space
r*   )r   maskprobabilitys      r   sampleSpace.sample^   s
     "!r!   c                B    [         R                  " U5      u  U l        nU$ )a  Seed the pseudorandom number generator (PRNG) of this space and, if applicable, the PRNGs of subspaces.

Args:
    seed: The seed value for the space. This is expanded for composite spaces to accept multiple values. For further details, please refer to the space's documentation.

Returns:
    The seed values used for all the PRNGs, for composite spaces this can be a tuple or dictionary of values.
)r   r#   r   )r   r   np_random_seeds      r   r   
Space.seedn   s!     +2*;*;D*A'r!   c                    [         e)zbReturn boolean specifying if x is a valid member of this space, equivalent to ``sample in space``.r*   r   xs     r   containsSpace.containsz   s    !!r!   c                $    U R                  U5      $ )z?Return boolean specifying if x is a valid member of this space.)r9   r7   s     r   __contains__Space.__contains__~   s    }}Qr!   c                    [        U5      nSU;   a  UR                  S5      US'   US	 SU;   a  US   US'   US	 U R                  R                  U5        g)zUsed when loading a pickled space.

This method was implemented explicitly to allow for loading of legacy states.

Args:
    state: The updated state value
r   r   r#   r   N)dictget__dict__update)r   states     r   __setstate__Space.__setstate__   sc     U e#ii0E(Og%"'"4E,k" 	U#r!   c                    [        U5      $ )zCConvert a batch of samples from this space to a JSONable data type.)listr   sample_ns     r   to_jsonableSpace.to_jsonable   s     H~r!   c                    U$ )zCConvert a JSONable data type to a batch of samples from this space. rH   s     r   from_jsonableSpace.from_jsonable   s	     r!   )r   r   r   )NNN)r   zSequence[int] | Noner   znpt.DTypeLike | Noner   z int | np.random.Generator | None)returnznp.random.Generator)rP   ztuple[int, ...] | None)rP   bool)NN)r/   
Any | Noner0   rR   rP   r   )N)r   z
int | NonerP   z int | list[int] | dict[str, int])r8   r   rP   rQ   )rC   z-Iterable[tuple[str, Any]] | Mapping[str, Any])rI   zSequence[T_cov]rP   	list[Any])rI   rS   rP   zlist[T_cov])__name__
__module____qualname____firstlineno____doc__r   propertyr#   r   r,   r1   r   r9   r<   rD   rJ   rN   __static_attributes__rM   r!   r   r   r      s    6 '+&*15	 #  $  /	 ,     " "" 
" $4
r!   r   )rX   
__future__r   collections.abcr   r   r   typingr   r   r	   r
   numpyr   numpy.typingnptgymnasium.utilsr   r   NDArrayint8r   __annotations__r   rM   r!   r   <module>re      sY    . " 7 7 3 3   # 	4( RWW-Y -PGEN Pr!   