
    h                     d    S r SSKJr  SSKrSSKJr  SSKJr  SSKrSSK	J
r
   " S S\
\   5      rg)	zPImplementation of a space that represents the cartesian product of other spaces.    )annotationsN)Iterable)Any)Spacec                     ^  \ rS rSrSr S   SU 4S jjjr\S 5       rSSU 4S j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S jrSS jrSrU =r$ )OneOf   a  An exclusive tuple (more precisely: the direct sum) of :class:`Space` instances.

Elements of this space are elements of one of the constituent spaces.

Example:
    >>> from gymnasium.spaces import OneOf, Box, Discrete
    >>> observation_space = OneOf((Discrete(2), Box(-1, 1, shape=(2,))), seed=123)
    >>> observation_space.sample()  # the first element is the space index (Discrete in this case) and the second element is the sample from Discrete
    (np.int64(0), np.int64(0))
    >>> observation_space.sample()  # this time the Box space was sampled as index=1
    (np.int64(1), array([-0.00711833, -0.7257502 ], dtype=float32))
    >>> observation_space[0]
    Discrete(2)
    >>> observation_space[1]
    Box(-1.0, 1.0, (2,), float32)
    >>> len(observation_space)
    2
c                @  > [        U[        5      (       d
   U S35       e[        U5      U l        [	        U R                  5      S:  d   S5       eU R                   H-  n[        U[
        5      (       a  M   U S[        U5       35       e   [        TU ]!  SSU5        g)ap  Constructor of :class:`OneOf` space.

The generated instance will represent the cartesian product :math:`\text{spaces}[0] \times ... \times \text{spaces}[-1]`.

Args:
    spaces (Iterable[Space]): The spaces that are involved in the cartesian product.
    seed: Optionally, you can use this argument to seed the RNGs of the ``spaces`` to ensure reproducible sampling.
z is not an iterabler   z'Empty `OneOf` spaces are not supported.z7 does not inherit from `gymnasium.Space`. Actual Type: N)	
isinstancer   tuplespaceslenr   typesuper__init__)selfr   seedspace	__class__s       P/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/oneof.pyr   OneOf.__init__"   s     &(++Kx7J-KK+Fm4;;!#N%NN#[[Eu  ^OPTUZP[}]^  ! 	tT*    c                :    [        S U R                   5       5      $ )zDChecks whether this space can be flattened to a :class:`spaces.Box`.c              3  8   #    U  H  oR                   v   M     g 7fN)is_np_flattenable.0r   s     r   	<genexpr>*OneOf.is_np_flattenable.<locals>.<genexpr>;   s     Du**s   )allr   r   s    r   r   OneOf.is_np_flattenable8   s     DDDDr   c           
     V  > Uc/  [         TU ]  S5      nU4[        S U R                   5       5      -   $ [	        U[
        5      (       a  [         TU ]  U5      nU R                  R                  [        R                  " [        R                  5      R                  [        U R                  5      S9n[         TU ]  U5        U4[        S [        U R                  U5       5       5      -   $ [	        U[        [        45      (       a  [        U5      [        U R                  5      S-   :w  a-  [        S[        U5       S[        U R                  5       35      e[         TU ]  US   5      4[        S	 [        U R                  USS 5       5       5      -   $ [!        S
[#        U5       35      e)a   Seed the PRNG of this space and all subspaces.

Depending on the type of seed, the subspaces will be seeded differently

* ``None`` - All the subspaces will use a random initial seed
* ``Int`` - The integer is used to seed the :class:`Tuple` space that is used to generate seed values for each of the subspaces. Warning, this does not guarantee unique seeds for all the subspaces.
* ``Tuple[int, ...]`` - Values used to seed the subspaces, first value seeds the OneOf and subsequent seed the subspaces. This allows the seeding of multiple composite subspaces ``[42, 54, ...]``.

Args:
    seed: An optional int or tuple of ints to seed the OneOf space and subspaces. See above for more details.

Returns:
    A tuple of ints used to seed the OneOf space and subspaces
Nc              3  B   #    U  H  oR                  S 5      v   M     g 7fr   r   r   s     r   r   OneOf.seed.<locals>.<genexpr>N   s     (S{eD)9)9{s   )sizec              3  Z   #    U  H!  u  pUR                  [        U5      5      v   M#     g 7fr   )r   intr   r   subseeds      r   r   r'   V   s)      )&@NE 

3w<((&@s   )+   z`Expects that the subspaces of seeds equals the number of subspaces + 1. Actual length of seeds: z, length of subspaces: r   c              3  H   #    U  H  u  pUR                  U5      v   M     g 7fr   r&   r+   s      r   r   r'   `   s"      4:T

7##:Ts    "z3Expected None, int, or tuple of ints, actual type: )r   r   r   r   r   r*   	np_randomintegersnpiinfoint32maxr   ziplist
ValueError	TypeErrorr   )r   r   
super_seedsubseedsr   s       r   r   
OneOf.seed=   s    <d+J=5(St{{(S#SSSc""d+J~~.."&&S-= / H GL=5 )&)$++x&@) $   udm,,4yC,q00 vwz{  xA  wB  BY  Z]  ^b  ^i  ^i  Zj  Yk  l  GLa)+e 4:=dkk4PQPR8:T4 /   Ed4j\R r   c                   U R                   R                  S[        U R                  5      [        R
                  S9nU R                  U   nUb  Ub  [        SU SU 35      eUb  [        U[        5      (       d   S[        U5       35       e[        U5      [        U R                  5      :X  d)   S[        U R                  5       S[        U5       35       eUR                  X   S9nX54$ Ub  [        U[        5      (       d   S	[        U5       35       e[        U5      [        U R                  5      :X  d)   S
[        U R                  5       S[        U5       35       eUR                  X#   S9nX54$ UR                  5       nX54$ )a  Generates a single random sample inside this space.

This method draws independent samples from the subspaces.

Args:
    mask: An optional tuple of optional masks for each of the subspace's samples,
        expects the same number of masks as spaces
    probability: An optional tuple of optional probability masks for each of the subspace's samples,
        expects the same number of probability masks as spaces

Returns:
    Tuple of the subspace's samples
r   )dtypezIOnly one of `mask` or `probability` can be provided, actual values: mask=z, probability=z/Expected type of `mask` is tuple, actual type: zExpected length of `mask` is z, actual length: )maskz6Expected type of `probability` is tuple, actual type: z$Expected length of `probability` is )probability)r/   r0   r   r   r1   int64r7   r   r   r   sample)r   r>   r?   subspace_idxsubspacesubspace_samples         r   rA   OneOf.sampleh   s   $ ~~..q#dkk2B"((.S;;|, 7[\`[aaop{o|}  e  N@dMN  t9!  ^.s4;;/?.@@QRUVZR[Q\]^  'oo43EoFO ,, $U  \G[HYGZ[\  {#s(  l5c$++6F5GGXY\]hYiXjkl  'oo+:SoTO ,, 'oo/O,,r   c                Z   [        U[        5      =(       a    [        U5      S:H  =(       a    [        US   [        R                  [
        45      =(       aV    SUS   s=:*  =(       a    [        U R                  5      :  Os  =(       a$    U R                  US      R                  US   5      $ )z?Return boolean specifying if x is a valid member of this space.   r   r-   )r   r   r   r1   r@   r*   r   contains)r   xs     r   rH   OneOf.contains   s     q%  1A!11Q4"((C11 QqT,,C,,1 AaD!**1Q40	
r   c           	         SSR                  U R                   Vs/ s H  n[        U5      PM     sn5      -   S-   $ s  snf )z,Gives a string representation of this space.zOneOf(z, ))joinr   str)r   ss     r   __repr__OneOf.__repr__   s5    $))T[[$A[SV[$ABBSHH$As   <
c                    U VVs/ s H2  u  p#[        U5      U R                  U   R                  U/5      S   /PM4     snn$ s  snnf )zCConvert a batch of samples from this space to a JSONable data type.r   )r*   r   to_jsonable)r   sample_ni	subsamples       r   rS   OneOf.to_jsonable   sN     #+
"* VT[[^//<Q?@"*
 	
 
s   9Ac                    U VVs/ s H=  u  p#[         R                  " U5      U R                  U   R                  U/5      S   4PM?     snn$ s  snnf )zCConvert a JSONable data type to a batch of samples from this space.r   )r1   r@   r   from_jsonable)r   rT   	space_idxjsonable_samples       r   rY   OneOf.from_jsonable   s]     /7

 /7*	 #I&44o5FGJ /7
 	
 
s   AAc                     U R                   U   $ )z%Get the subspace at specific `index`.r   )r   indexs     r   __getitem__OneOf.__getitem__   s    {{5!!r   c                ,    [        U R                  5      $ )zGGet the number of subspaces that are involved in the cartesian product.)r   r   r"   s    r   __len__OneOf.__len__   s    4;;r   c                b    [        U[        5      =(       a    U R                  UR                  :H  $ )z7Check whether ``other`` is equivalent to this instance.)r   r   r   )r   others     r   __eq__OneOf.__eq__   s!    %'GDKK5<<,GGr   r^   r   )r   zIterable[Space[Any]]r   z7int | typing.Sequence[int] | np.random.Generator | None)r   zint | tuple[int, ...] | Nonereturnztuple[int, ...])NN)r>   tuple[Any | None, ...] | Noner?   rj   ri   tuple[int, Any])rI   rk   ri   bool)ri   rN   )rT   z typing.Sequence[tuple[int, Any]]ri   list[list[Any]])rT   rm   ri   zlist[tuple[Any, ...]])r_   r*   ri   z
Space[Any])ri   r*   )rf   r   ri   rl   )__name__
__module____qualname____firstlineno____doc__r   propertyr   r   rA   rH   rP   rS   rY   r`   rc   rg   __static_attributes____classcell__)r   s   @r   r   r      s    , IM+$+ F+ +, E E) )Z /359/-+/- 3/- 
	/-b	
I
8
	

" H Hr   r   )rr   
__future__r   typingcollections.abcr   r   numpyr1   gymnasium.spaces.spacer   r    r   r   <module>r|      s.    V "  $   (wHE#J wHr   