
    h'              	          S r SSKJr  SSKrSSKJrJr  SSKrSSKJ	r	  SSK
rSSKJr   " S S\\\\S	4   \4      5      rg)
zBImplementation of a space that represents finite-length sequences.    )annotationsN)AnyUnion)NDArray)Spacec                     ^  \ rS rSrSr  S     SU 4S jjjrSSU 4S jjjr\S 5       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rU =r$ )Sequence   a  This space represent sets of finite-length sequences.

This space represents the set of tuples of the form :math:`(a_0, \dots, a_n)` where the :math:`a_i` belong
to some space that is specified during initialization and the integer :math:`n` is not fixed

Example:
    >>> from gymnasium.spaces import Sequence, Box
    >>> observation_space = Sequence(Box(0, 1), seed=0)
    >>> observation_space.sample()
    (array([0.6822636], dtype=float32), array([0.18933342], dtype=float32), array([0.19049619], dtype=float32))
    >>> observation_space.sample()
    (array([0.83506], dtype=float32), array([0.9053838], dtype=float32), array([0.5836242], dtype=float32), array([0.63214064], dtype=float32))

Example with stacked observations
    >>> observation_space = Sequence(Box(0, 1), stack=True, seed=0)
    >>> observation_space.sample()
    array([[0.6822636 ],
           [0.18933342],
           [0.19049619]], dtype=float32)
c                $  > [        U[        5      (       d   S[        U5       35       eXl        X0l        U R                  (       a9  [
        R                  R                  R                  U R                  S5      U l	        [        TU ]-  SSU5        g)a3  Constructor of the :class:`Sequence` space.

Args:
    space: Elements in the sequences this space represent must belong to this space.
    seed: Optionally, you can use this argument to seed the RNG that is used to sample from the space.
    stack: If ``True`` then the resulting samples would be stacked.
zFExpects the feature space to be instance of a gym Space, actual type:    N)
isinstancer   typefeature_spacestackgymvectorutilsbatch_spacestacked_feature_spacesuper__init__)selfspaceseedr   	__class__s       S/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/sequence.pyr   Sequence.__init__%   s     5
 
 	bSTXY^T_S`a	b 
 #
::03

0@0@0L0L""A1D&
 	tT*    c                  > Uc*  [         TU ]  S5      U R                  R                  S5      4$ [        U[        5      (       a  [         TU ]  U5      n[	        U R
                  R                  [        R                  " [        R                  5      R                  5      5      n[         TU ]  U5        X R                  R                  U5      4$ [        U[        [        45      (       aV  [        U5      S:w  a  [        S[        U5       35      e[         TU ]  US   5      U R                  R                  US   5      4$ [        S[!        U5       35      e)a=  Seed the PRNG of the Sequence space and the feature space.

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:`Sequence` space that is used to generate a seed value for the feature space.
* ``Tuple of ints`` - A tuple for the :class:`Sequence` and feature space.

Args:
    seed: An optional int or tuple of ints to seed the PRNG. See above for more details

Returns:
    A tuple of the seeding values for the Sequence and feature space
N   zYExpects the seed to have two elements for the Sequence and feature space, actual length: r   r   z0Expected None, int, tuple of ints, actual type: )r   r   r   r   int	np_randomintegersnpiinfoint32maxtuplelistlen
ValueError	TypeErrorr   )r   r   
super_seedfeature_seedr   s       r   r   Sequence.seed?   s!    <7<%t'9'9'>'>t'DDDc""d+Jt~~66rxx7I7M7MNOLGL1166|DDDudm,,4yA~ opstxpyoz{  7<Q($*<*<*A*A$q'*JJJB4:,O r   c                    g)zDChecks whether this space can be flattened to a :class:`spaces.Box`.F r   s    r   is_np_flattenableSequence.is_np_flattenablea   s     r   c                  ^ ^^ Tb  Tb  [        ST ST 35      eTb5  T R                  TS   S5      n[        UU 4S j[        U5       5       5      nOqTb5  T R                  TS   S5      n[        UU 4S j[        U5       5       5      nO9T R                  R                  S5      n[        U 4S	 j[        U5       5       5      nT R                  (       aq  [        R                  R                  R                  T R                  [        U5      5      n[        R                  R                  R                  T R                  XE5      $ U$ )
a  Generates a single random sample from this space.

Args:
    mask: An optional mask for (optionally) the length of the sequence and (optionally) the values in the sequence.
        If you specify ``mask``, it is expected to be a tuple of the form ``(length_mask, sample_mask)`` where ``length_mask`` is

        * ``None`` - The length will be randomly drawn from a geometric distribution
        * ``int`` - Fixed length
        * ``np.ndarray`` of integers - Length of the sampled sequence is randomly drawn from this array.

        The second element of the tuple ``sample_mask`` specifies how the feature space will be sampled.
        Depending on if mask or probability is used will affect what argument is used.
    probability: See mask description above, the only difference is on the ``sample_mask`` for the feature space being probability rather than mask.

Returns:
    A tuple of random length with random samples of elements from the :attr:`feature_space`.
zIOnly one of `mask` or `probability` can be provided, actual values: mask=z, probability=r   maskc              3  \   >#    U  H!  nTR                   R                  TS    S9v   M#     g7f)r   )r6   Nr   sample).0_r6   r   s     r   	<genexpr>"Sequence.sample.<locals>.<genexpr>   s-      #AUA""))tAw)7AU   ),probabilityc              3  \   >#    U  H!  nTR                   R                  TS    S9v   M#     g7f)r   )r?   Nr8   )r:   r;   r?   r   s     r   r<   r=      s0      #-A ""))k!n)E-r>         ?c              3  X   >#    U  H  nTR                   R                  5       v   M!     g 7fNr8   )r:   r;   r   s     r   r<   r=      s'      #5I""))++5Is   '*)r+   generate_sample_lengthr(   ranger"   	geometricr   r   r   r   create_empty_arrayr   r*   concatenate)r   r6   r?   sample_lengthsampled_valuesouts   ```   r   r9   Sequence.samplef   s7   @  7[\`[aaop{o|}   77QHM" #AF}AU# N $ 77AVM" #}-# N
 !NN44T:M" #5:=5I# N ::**""55""C$7C ::##//0B0BNXXr   c                   UGbB  [         R                  " [        U5      [         R                  5      (       a  SU::  d   SU SU 35       eU$ [	        U[         R
                  5      (       a  [        UR                  5      S:X  d   SU SUR                   35       e[         R                  " SU:*  5      (       d   SU SU 35       e[         R                  " UR                  [         R                  5      (       d   S	U S
UR                   35       eU R                  R                  U5      $ [        SU S[        U5       35      eU R                  R                  S5      $ )zAGenerate the sample length for a given length mask and mask type.r   zExpects the length mask of `z5` to be greater than or equal to zero, actual value: r   z)Expects the shape of the length mask of `z%` to be 1-dimensional, actual shape: z*Expects all values in the length_mask of `z6` to be greater than or equal to zero, actual values: z"Expects the length mask array of `z,` to have dtype of np.integer, actual type: z$Expects the type of length_mask of `z1` to be an integer or a np.ndarray, actual type: rA   )r$   
issubdtyper   integerr   ndarrayr*   shapealldtyper"   choicer,   rF   )r   length_mask	mask_types      r   rD   Sequence.generate_sample_length   s    "}}T+.

;;$@1)<qr}q~@$ #"K44))*a/C>ykInoz  pA  pA  oB  CC/vv$  O?	{  KA  BM  AN  OO  }}%%rzz  C7	{Bnoz  pA  pA  oB  CC  ~~,,[99:9+Evw{  }H  xI  wJ  K 
 >>++D11r   c                  ^  T R                   (       aG  [        U 4S j[        R                  R                  R                  T R                  U5       5       5      $ [        U[        5      =(       a    [        U 4S jU 5       5      $ )z?Return boolean specifying if x is a valid member of this space.c              3  B   >#    U  H  nUTR                   ;   v   M     g 7frC   )r   r:   itemr   s     r   r<   $Sequence.contains.<locals>.<genexpr>   s#      SD ***Ss   c              3  Z   >#    U  H   nTR                   R                  U5      v   M"     g 7frC   )r   containsrZ   s     r   r<   r\      s(      0>?d""++D11as   (+)	r   rR   r   r   r   iterater   r   r(   )r   xs   ` r   r^   Sequence.contains   sn     :: JJ,,44T5O5OQRS  
 a' C 0>?0 - r   c                <    SU R                    SU R                   S3$ )z,Gives a string representation of this space.z	Sequence(z, stack=))r   r   r2   s    r   __repr__Sequence.__repr__   s"    4--.htzzl!DDr   c                    U R                   (       a  U R                  R                  U5      $ U Vs/ s H  o R                  R                  U5      PM     sn$ s  snf )zCConvert a batch of samples from this space to a JSONable data type.)r   r   to_jsonabler   r   sample_nr9   s      r   rg   Sequence.to_jsonable   sI     ::--99(CCIQRv&&226:RRRs   $Ac                    U R                   (       a  U R                  R                  U5      $ U Vs/ s H'  n[        U R                  R                  U5      5      PM)     sn$ s  snf )zCConvert a JSONable data type to a batch of samples from this space.)r   r   from_jsonabler(   r   rh   s      r   rl   Sequence.from_jsonable   s[    ::--;;HEE OWNVFd((66v>?h  s   .A"c                    [        U[        5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )z7Check whether ``other`` is equivalent to this instance.)r   r	   r   r   )r   others     r   __eq__Sequence.__eq__   sA     uh' *""e&9&99*

ekk)	
r   )r   r   r   )NF)r   z
Space[Any]r   z int | np.random.Generator | Noner   boolrC   )r   zint | tuple[int, int] | Nonereturnztuple[int, int])NN)r6   3None | tuple[None | int | NDArray[np.integer], Any]r?   rt   rs   ztuple[Any] | Any)rU   z'None | np.integer | NDArray[np.integer]rV   z
None | strrs   r!   )r`   r   rs   rr   )rs   str)ri   z&typing.Sequence[tuple[Any, ...] | Any]rs   list[list[Any]])ri   rv   rs   zlist[tuple[Any, ...] | Any])ro   r   rs   rr   )__name__
__module____qualname____firstlineno____doc__r   r   propertyr3   r9   rD   r^   rd   rg   rl   rp   __static_attributes____classcell__)r   s   @r   r	   r	      s    0 26	++ /+ 	+ +4   D    <
<
< 
<|2<2 2 
	2BES>S	S
 
r   r	   .)r{   
__future__r   typingr   r   numpyr$   numpy.typingr   	gymnasiumr   gymnasium.spaces.spacer   r(   r	   r1   r   r   <module>r      sB    H "       (_
uU5c?C/01 _
r   