
    h^\                    
   S r SSKJr  SSKrSSKJrJr  SSKJ	r	J
r
Jr  SSKrSSKJr  SSKrSSKJrJrJrJrJrJrJrJrJrJrJrJr  \S8S j5       r\RA                  \5      \RA                  \5      S9S	 j5       5       r!\RA                  \5      S:S
 j5       r"\RA                  \5      S;S j5       r#\RA                  \5      S<S j5       r$\RA                  \5      S=S j5       r%\RA                  \5      S>S j5       r&\RA                  \5      S?S j5       r'\RA                  \5      S@S j5       r(\
" S5      r)\\\	   \*\+\	4   \,\	S4   \4   r-\SAS j5       r.\.RA                  \5      \.RA                  \5      SBS j5       5       r/\.RA                  \5      SCS j5       r0\.RA                  \5            SDS j5       r1\.RA                  \5      SES j5       r2\.RA                  \5      SFS j5       r3\.RA                  \5      SGS j5       r4\.RA                  \5      SHS j5       r5\.RA                  \5            SIS j5       r6\.RA                  \5      SJS j5       r7\SKS j5       r8\8RA                  \5      \8RA                  \5            SBS j5       5       r9\8RA                  \5      SLS j5       r:\8RA                  \5            SMS  j5       r;\8RA                  \5            SNS! j5       r<\8RA                  \5      SOS" j5       r=\8RA                  \5      SGS# j5       r>\8RA                  \5      SPS$ j5       r?\8RA                  \5      SQS% j5       r@\8RA                  \5      SRS& j5       rA\SSS' j5       rB\BRA                  \5      STS( j5       rC\BRA                  \5      \BRA                  \5      \BRA                  \5      SUS) j5       5       5       rD\BRA                  \5      SVS* j5       rE\BRA                  \5      SWS+ j5       rF\BRA                  \5      SXS, j5       rG\BRA                  \5      SYS- j5       rH\BRA                  \5      SZS. j5       rI\BRA                  \5      S[S/ j5       rJ\S\S0 j5       rK\KRA                  \5      \KRA                  \5      \KRA                  \5      \KRA                  \5      S1 5       5       5       5       rL\KRA                  \5      S]S2 j5       rM\KRA                  \5      S^S3 j5       rN\KRA                  \5      S4 5       rO\KRA                  \5      S_S5 j5       rP\KRA                  \5      S`S6 j5       rQ\KRA                  \5      SaS7 j5       rRg)bzImplementation of utility functions that can be applied to spaces.

These functions mostly take care of flattening and unflattening elements of spaces
 to facilitate their usage in learning code.
    )annotationsN)reducesingledispatch)AnyTypeVarUnion)NDArray)BoxDictDiscreteGraphGraphInstanceMultiBinaryMultiDiscreteOneOfSequenceSpaceTextTuplec                Z    U R                   SL a  [        U  S35      e[        SU  S35      e)a8  Return the number of dimensions a flattened equivalent of this space would have.

Args:
    space: The space to return the number of dimensions of the flattened spaces

Returns:
    The number of dimensions for the flattened spaces

Raises:
     NotImplementedError: if the space is not defined in :mod:`gym.spaces`.
     ValueError: if the space cannot be flattened into a :class:`gymnasium.spaces.Box`

Example:
    >>> from gymnasium.spaces import Dict, Discrete
    >>> space = Dict({"position": Discrete(2), "velocity": Discrete(3)})
    >>> flatdim(space)
    5
Fd cannot be flattened to a numpy array, probably because it contains a `Graph` or `Sequence` subspaceUnknown space: ``)is_np_flattenable
ValueErrorNotImplementedErrorspaces    P/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/utils.pyflatdimr    !   sA    ( %'gyz
 	
  0q9
::    c                L    [        [        R                  U R                  S5      $ )N   )r   opmulshaper   s    r   _flatdim_box_multibinaryr'   =   s     "&&%++q))r!   c                ,    [        U R                  5      $ N)intnr   s    r   _flatdim_discreter,   C   s    uww<r!   c                T    [        [        R                  " U R                  5      5      $ r)   )r*   npsumnvecr   s    r   _flatdim_multidiscreter1   H   s    rvvejj!""r!   c                x    U R                   (       a  [        S U R                   5       5      $ [        U  S35      e)Nc              3  8   #    U  H  n[        U5      v   M     g 7fr)   r    .0ss     r   	<genexpr>!_flatdim_tuple.<locals>.<genexpr>P        4|!71::|   r   )r   r/   spacesr   r   s    r   _flatdim_tupler=   M   s8    4u||444
'uv r!   c                    U R                   (       a*  [        S U R                  R                  5        5       5      $ [	        U  S35      e)Nc              3  8   #    U  H  n[        U5      v   M     g 7fr)   r4   r5   s     r   r8    _flatdim_dict.<locals>.<genexpr>Y   s     ='<!71::'<r;   r   )r   r/   r<   valuesr   r   s    r   _flatdim_dictrB   V   sA    =u||':':'<===
'uv r!   c                    [        S5      e)NzGCannot get flattened size as the Graph Space in Gym has a dynamic size.)r   r   s    r   _flatdim_graphrD   _   s    
Q r!   c                    U R                   $ r)   )
max_lengthr   s    r   _flatdim_textrG   f   s    r!   c                @    S[        S U R                   5       5      -   $ )Nr#   c              3  8   #    U  H  n[        U5      v   M     g 7fr)   r4   r5   s     r   r8   !_flatdim_oneof.<locals>.<genexpr>m   r:   r;   )maxr<   r   s    r   _flatdim_oneofrL   k   s    s4u||4444r!   T.c                     [        SU  S35      e)a>  Flatten a data point from a space.

This is useful when e.g. points from spaces must be passed to a neural
network, which only understands flat arrays of floats.

Args:
    space: The space that ``x`` is flattened by
    x: The value to flatten

Returns:
    The flattened datapoint

        - For :class:`gymnasium.spaces.Box` and :class:`gymnasium.spaces.MultiBinary`, this is a flattened array
        - For :class:`gymnasium.spaces.Discrete` and :class:`gymnasium.spaces.MultiDiscrete`, this is a flattened one-hot array of the sample
        - For :class:`gymnasium.spaces.Tuple` and :class:`gymnasium.spaces.Dict`, this is a concatenated array the subspaces (does not support graph subspaces)
        - For graph spaces, returns :class:`GraphInstance` where:
            - :attr:`GraphInstance.nodes` are n x k arrays
            - :attr:`GraphInstance.edges` are either:
                - m x k arrays
                - None
            - :attr:`GraphInstance.edge_links` are either:
                - m x 2 arrays
                - None

Raises:
    NotImplementedError: If the space is not defined in :mod:`gymnasium.spaces`.

Example:
    >>> from gymnasium.spaces import Box, Discrete, Tuple
    >>> space = Box(0, 1, shape=(3, 5))
    >>> flatten(space, space.sample()).shape
    (15,)
    >>> space = Discrete(4)
    >>> flatten(space, 2)
    array([0, 0, 1, 0])
    >>> space = Tuple((Box(0, 1, shape=(2,)), Box(0, 1, shape=(3,)), Discrete(3)))
    >>> example = ((.5, .25), (1., 0., .2), 1)
    >>> flatten(space, example)
    array([0.5 , 0.25, 1.  , 0.  , 0.2 , 0.  , 1.  , 0.  ])
r   r   r   r   xs     r   flattenrR   t   s    T  0q9
::r!   c                Z    [         R                  " XR                  S9R                  5       $ Ndtype)r.   asarrayrV   rR   rP   s     r   _flatten_box_multibinaryrX      s      ::a{{+3355r!   c                z    [         R                  " U R                  U R                  S9nSX!U R                  -
  '   U$ )NrU   r#   )r.   zerosr+   rV   start)r   rQ   onehots      r   _flatten_discreter]      s/    XXeggU[[1FFu{{?Mr!   c                l   [         R                  " U R                  R                  S-   4[         R                  S9n[         R
                  " U R                  R                  5       5      USS & [         R                  " US   4U R                  S9nSX2S S XR                  -
  R                  5       -   '   U$ )Nr#   rU   )	r.   rZ   r0   sizeint32cumsumrR   rV   r[   )r   rQ   offsetsr\   s       r   _flatten_multidiscreterd      s     hh

!+-RXX>G))EJJ..01GABKXXwr{nEKK8F9:F3B<1{{?33556Mr!   c                0   U R                   (       a[  [        R                  " [        XR                  5       VVs/ s H$  u  p#[        R
                  " [        X25      5      PM&     snn5      $ [        S [        XR                  5       5       5      $ s  snnf )Nc              3  <   #    U  H  u  p[        X!5      v   M     g 7fr)   )rR   )r6   x_partr7   s      r   r8   !_flatten_tuple.<locals>.<genexpr>   s     J5I	##5I   )r   r.   concatenatezipr<   arrayrR   tuple)r   rQ   rg   r7   s       r   _flatten_tuplern      sk    ~~;>q,,;OP;OifRXXga();OP
 	
 JSLL5IJJJ Qs   +B
c                z   U R                   (       ac  [        R                  " U R                  R	                  5        VVs/ s H'  u  p#[        R
                  " [        X1U   5      5      PM)     snn5      $ U R                  R	                  5        VVs0 s H  u  p#U[        X1U   5      _M     snn$ s  snnf s  snnf r)   )r   r.   rj   r<   itemsrl   rR   )r   rQ   keyr7   s       r   _flatten_dictrr      s    ~~8=8J8J8LM8LfcRXXga3()8LM
 	
 38,,2D2D2FG2FCcF##2FGG NGs   .B1
B7c                          SS jnU" U R                   UR                  5      nUc   eU" U R                  UR                  5      n[	        X4UR
                  5      $ )zWe're not using ``.unflatten()`` for :class:`Box` and :class:`Discrete` because a graph is not a homogeneous space, see `.flatten` docstring.c                   S nU b  Ub  [        U [        5      (       a!  UR                  UR                  S   S5      nU$ [        U [        5      (       d   e[
        R                  " UR                  S   U R                  U R                  -
  4U R                  S9nSU[
        R                  " UR                  S   5      XR                  -
  4'   U$ )Nr   r_   rU   r#   )
isinstancer
   reshaper&   r   r.   rZ   r+   r[   rV   arange)unflatten_spaceunflatten_xrets      r   _graph_unflatten(_flatten_graph.<locals>._graph_unflatten   s     &;+B/3//!))+*;*;A*>C 
 "/8<<<<hh &&q)?+<+<?T?T+TU)//  IIk//23[CXCX5XX 
r!   )rx   zDiscrete | Box | Nonery   NDArray[Any] | Nonereturnr}   
node_spacenodes
edge_spaceedgesr   
edge_linksr   rQ   r{   r   r   s        r   _flatten_graphr      sl    .( 
& U--qww7EU--qww7Eq||44r!   c                    [         R                  " U R                  4[        U R                  5      [         R
                  S9n[        U5       H  u  p4U R                  U5      X#'   M     U$ )N)r&   
fill_valuerV   )r.   fullrF   lencharacter_setra   	enumeratecharacter_index)r   rQ   arrivals        r   _flatten_textr      sX    
''!c%2E2E.FbhhC A,&&s+ Jr!   c                  ^  T R                   (       a  [        R                  R                  R	                  T R
                  U5      nU Vs/ s H  n[        T R                  U5      PM     nn[        T R                  5      n[        R                  R                  R                  U[        U5      S9n[        R                  R                  R                  XTU5      $ [        U 4S jU 5       5      $ s  snf )N)r+   c              3  P   >#    U  H  n[        TR                  U5      v   M     g 7fr)   )rR   feature_spacer6   itemr   s     r   r8   $_flatten_sequence.<locals>.<genexpr>  s!     FADWU00$77A   #&)stackgymvectorutilsiteratestacked_feature_spacerR   r   flatten_spacecreate_empty_arrayr   rj   rm   )r   rQ   samples_iterssampleflattened_samplesflattened_spaceouts   `      r   _flatten_sequencer      s     {{

((001L1LaP?L
?LVGE''0} 	 
 ((;(;<jj11s#45 2 
 zz++OPSTTFAFFF
s    C2c                8   Uu  p#U R                   U   n[        XC5      n[        U 5      S-
  nUR                  U:  aF  [        R
                  " XeR                  -
  US   UR                  S9n[        R                  " XW/5      n[        R                  " U/U/5      $ )Nr#   r   rU   )r<   rR   r    r`   r.   r   rV   rj   )r   rQ   idxr   	sub_spaceflat_samplemax_flatdimpaddings           r   _flatten_oneofr     s    KCS!I),K%.1$K+%''***KN+BSBS
 nnk%;<>>C5+.//r!   c                     [        SU  S35      e)a  Unflatten a data point from a space.

This reverses the transformation applied by :func:`flatten`. You must ensure
that the ``space`` argument is the same as for the :func:`flatten` call.

Args:
    space: The space used to unflatten ``x``
    x: The array to unflatten

Returns:
    A point with a structure that matches the space.

Raises:
    NotImplementedError: if the space is not defined in :mod:`gymnasium.spaces`.
r   r   rO   rP   s     r   	unflattenr     s    "  0q9
::r!   c                p    [         R                  " XR                  S9R                  U R                  5      $ rT   )r.   rW   rV   rv   r&   rP   s     r   _unflatten_box_multibinaryr   *  s&    
 ::a{{+33EKK@@r!   c                    [         R                  " U5      n[        US   5      S:X  a  [        U SU  S35      eU R                  US   S   -   $ )Nr   zK is not a valid one-hot encoded vector and can not be unflattened to space @. Not all valid samples in a flattened space can be unflattened.)r.   nonzeror   r   r[   )r   rQ   r   s      r   _unflatten_discreter   2  s`    jjmG
71:!c\]b\c dM M
 	
 ;;A&&r!   c                   [         R                  " U R                  R                  S-   4U R                  S9n[         R
                  " U R                  R                  5       5      USS & [         R                  " U5      u  n[        U5      S:X  a  [        U SU  S35      e[         R                  " X2S S -
  U R                  S9R                  U R                  5      U R                  -   $ )Nr#   rU   r   zW is not a concatenation of one-hot encoded vectors and can not be unflattened to space r   r_   )r.   rZ   r0   r`   rV   rb   rR   r   r   r   rW   rv   r&   r[   )r   rQ   rc   indicess       r   _unflatten_multidiscreter   =  s     hh

!+-U[[AG))EJJ..01GABKAJW
7|qchinho pM M
 	

 	

7Sb\)=EEekkR
++	r!   c                h   U R                   (       a  [        U[        R                  5      (       d   U  S[	        U5       35       e[        R
                  " U R                   Vs/ s H  n[        U5      PM     sn[        R                  S9n[        R                  " U[        R                  " US S 5      5      n[        S [        X@R                  5       5       5      $ [        U[        5      (       d   U  S[	        U5       35       e[        S [        XR                  5       5       5      $ s  snf )NzZ is numpy-flattenable. Thus, you should only unflatten numpy arrays for this space. Got a rU   r_   c              3  <   #    U  H  u  p[        X!5      v   M     g 7fr)   r   r6   	flattenedr7   s      r   r8   #_unflatten_tuple.<locals>.<genexpr>Y  s!      
 A	 a## Ari   zX is not numpy-flattenable. Thus, you should only unflatten tuples for this space. Got a c              3  <   #    U  H  u  p[        X!5      v   M     g 7fr)   r   r   s      r   r8   r   `  s     R=Q\Y1((=Qri   )r   ru   r.   ndarraytyperW   r<   r    int_splitrb   rm   rk   )r   rQ   r7   dimslist_flatteneds        r   _unflatten_tupler   O  s    rzz
 
 	yWnostuovnwx	y 
 zzu||<|!71:|<BGGL!RYYtCRy%9: 
 #NLL A
 
 	
 	5  s
himnoiphqrs  RSLL=QRRR =s   $D/c           
        U R                   (       a  [        R                  " U R                  R	                  5        Vs/ s H  n[        U5      PM     sn[        R                  S9n[        R                  " U[        R                  " US S 5      5      n[        X@R                  R                  5       5       VVVs0 s H  u  nu  pbU[        X%5      _M     snnn$ [        U[        5      (       d   U  S[        U5       35       eU R                  R                  5        VVs0 s H  u  pbU[        X!U   5      _M     snn$ s  snf s  snnnf s  snnf )NrU   r_   z\ is not numpy-flattenable. Thus, you should only unflatten dictionary for this space. Got a )r   r.   rW   r<   rA   r    r   r   rb   rk   rp   r   ru   dictr   )r   rQ   r7   r   r   r   rq   s          r   _unflatten_dictr   c  s(   zzu||/B/B/DE/D!71:/DERWWU!RYYtCRy%9: (+><<;M;M;O'P
'P#	8C 1(('P
 	

 	4  w
lmqrsmtluvw  5:LL4F4F4HI4H&#C1f%%4HII F
 Js   E<E#Ec                    S nU" U R                   UR                  5      nU" U R                  UR                  5      n[	        X4UR
                  5      $ )zWe're not using `.unflatten() for :class:`Box` and :class:`Discrete` because a graph is not a homogeneous space.

The size of the outcome is actually not fixed, but determined based on the number of
nodes and edges in the graph.
c                
   S nU b}  Ubz  [        U [        5      (       a  UR                  " S/U R                  Q76 nU$ [        U [        5      (       a1  [
        R                  " [
        R                  " U5      5      SS S 24   nU$ )Nr_   )ru   r
   rv   r&   r   r.   rW   r   )rx   ry   results      r   r{   *_unflatten_graph.<locals>._graph_unflatten{  su    &;+B/3//$,,RH/2G2GH  OX66BJJ{$;<RUCr!   r   r   s        r   _unflatten_graphr   s  sF     U--qww7EU--qww7Eq||44r!   c           	         SR                  U Vs/ s H,  o"[        U R                  5      :  d  M  U R                  U   PM.     sn5      $ s  snf )N )joinr   r   character_list)r   rQ   r   s      r   _unflatten_textr     sF    77./RasU=P=P9Q3Q	"		c	"aR Rs
   A
A
c                  ^  T R                   (       a  [        T R                  5      n[        R                  R
                  R                  X!5      nU Vs/ s H  n[        T R                  U5      PM     nn[        R                  R
                  R                  T R                  [        U5      5      n[        R                  R
                  R                  T R                  XV5      $ [        U 4S jU 5       5      $ s  snf )Nc              3  P   >#    U  H  n[        TR                  U5      v   M     g 7fr)   )r   r   r   s     r   r8   &_unflatten_sequence.<locals>.<genexpr>  s!     HadYu22D99ar   )r   r   r   r   r   r   r   r   r   r   rj   rm   )r   rQ   r   flatten_itersr   unflattened_samplesr   s   `      r   _unflatten_sequencer     s    {{'(;(;<

((00DAN
ANvIe))62 	 
 jj11%8!9
 zz++!4
 	
 HaHHH
s    C=c                    [         R                  " US   5      nU R                  U   n[        U5      nUSSU-    nU[	        X55      4$ )Nr   r#   )r.   int64r<   r    r   )r   rQ   r   r   original_sizetrimmed_samples         r   _unflatten_oneofr     sL    
((1Q4.CS!II&Mq1},-N	)444r!   c                     [        SU  S35      e)a'
  Flatten a space into a space that is as flat as possible.

This function will attempt to flatten ``space`` into a single :class:`gymnasium.spaces.Box` space.
However, this might not be possible when ``space`` is an instance of :class:`gymnasium.spaces.Graph`,
:class:`gymnasium.spaces.Sequence` or a compound space that contains a :class:`gymnasium.spaces.Graph`
or :class:`gymnasium.spaces.Sequence` space.
This is equivalent to :func:`flatten`, but operates on the space itself. The
result for non-graph spaces is always a :class:`gymnasium.spaces.Box` with flat boundaries. While
the result for graph spaces is always a :class:`gymnasium.spaces.Graph` with
:attr:`Graph.node_space` being a ``Box``
with flat boundaries and :attr:`Graph.edge_space` being a ``Box`` with flat boundaries or
``None``. The box has exactly :func:`flatdim` dimensions. Flattening a sample
of the original space has the same effect as taking a sample of the flattened
space. However, sampling from the flattened space is not necessarily reversible.
For example, sampling from a flattened Discrete space is the same as sampling from
a Box, and the results may not be integers or one-hot encodings. This may result in
errors or non-uniform sampling.

Args:
    space: The space to flatten

Returns:
    A flattened Box

Raises:
    NotImplementedError: if the space is not defined in :mod:`gymnasium.spaces`.

Example - Flatten spaces.Box:
    >>> from gymnasium.spaces import Box
    >>> box = Box(0.0, 1.0, shape=(3, 4, 5))
    >>> box
    Box(0.0, 1.0, (3, 4, 5), float32)
    >>> flatten_space(box)
    Box(0.0, 1.0, (60,), float32)
    >>> flatten(box, box.sample()) in flatten_space(box)
    True

Example - Flatten spaces.Discrete:
    >>> from gymnasium.spaces import Discrete
    >>> discrete = Discrete(5)
    >>> flatten_space(discrete)
    Box(0, 1, (5,), int64)
    >>> flatten(discrete, discrete.sample()) in flatten_space(discrete)
    True

Example - Flatten spaces.Dict:
    >>> from gymnasium.spaces import Dict, Discrete, Box
    >>> space = Dict({"position": Discrete(2), "velocity": Box(0, 1, shape=(2, 2))})
    >>> flatten_space(space)
    Box(0.0, 1.0, (6,), float64)
    >>> flatten(space, space.sample()) in flatten_space(space)
    True

Example - Flatten spaces.Graph:
    >>> from gymnasium.spaces import Graph, Discrete, Box
    >>> space = Graph(node_space=Box(low=-100, high=100, shape=(3, 4)), edge_space=Discrete(5))
    >>> flatten_space(space)
    Graph(Box(-100.0, 100.0, (12,), float32), Box(0, 1, (5,), int64))
    >>> flatten(space, space.sample()) in flatten_space(space)
    True
r   r   rO   r   s    r   r   r     s    ~  0q9
::r!   c                    [        U R                  R                  5       U R                  R                  5       U R                  S9$ rT   )r
   lowrR   highrV   r   s    r   _flatten_space_boxr     s/    uyy  "EJJ$6$6$8LLr!   c                B    [        SS[        U 5      4U R                  S9$ )Nr   r#   r   r   r&   rV   )r
   r    rV   r   s    r   _flatten_space_binaryr     s      11WU^$5U[[IIr!   c           
        U R                   (       a  U R                   Vs/ s H  n[        U5      PM     nn[        [        R
                  " U Vs/ s H  oR                  PM     sn5      [        R
                  " U Vs/ s H  oR                  PM     sn5      [        R                  " U Vs/ s H  oR                  PM     sn6 S9$ [        U R                   Vs/ s H  n[        U5      PM     snS9$ s  snf s  snf s  snf s  snf s  snf N)r   r   rV   )r<   )r   r<   r   r
   r.   rj   r   r   result_typerV   r   )r   r7   
space_lists      r   _flatten_space_tupler     s    05=1mA&
=z:z!z:; <A <=..J"?Jq77J"?@
 	

 5<<@<aq)<@AA >: <"?@s   C6C;?D .DD
c           
     b   U R                   (       a  U R                  R                  5        Vs/ s H  n[        U5      PM     nn[	        [
        R                  " U Vs/ s H  oR                  PM     sn5      [
        R                  " U Vs/ s H  oR                  PM     sn5      [
        R                  " U Vs/ s H  oR                  PM     sn6 S9$ [        U R                  R                  5        VV s0 s H  u  p0U[        U 5      _M     sn nS9$ s  snf s  snf s  snf s  snf s  sn nf r   )r   r<   rA   r   r
   r.   rj   r   r   r   rV   r   rp   )r   r7   r   rq   s       r   _flatten_space_dictr     s    050C0C0EF0E1mA&0E
Fz:z!z:; <A <=..J"?Jq77J"?@
 	

 <ALL<N<N<PQ<Pjc]5))<PQ  G: <"? Rs   DDD!<D&9D+
c                    [        [        U R                  5      U R                  b  [        U R                  5      S9$ S S9$ )N)r   r   )r   r   r   r   r   s    r   _flatten_space_graphr     sG     !1!12/4/?/?/KM%**+  RV r!   c                r    [        S[        U R                  5      U R                  4[        R
                  S9$ )Nr   r   )r
   r   r   rF   r.   ra   r   s    r   _flatten_space_textr      s0    C++,U5E5E4Grxx r!   c                P    [        [        U R                  5      U R                  S9$ )N)r   )r   r   r   r   r   s    r   _flatten_space_sequencer   '  s    M%"5"56ekkJJr!   c           
        [        U R                  5      n[        S U R                   5       5      S-   n[        R                  " U R                   Vs/ s H,  n[        R
                  " [        U5      R                  5      PM.     sn5      n[        R                  " U R                   Vs/ s H,  n[        R                  " [        U5      R                  5      PM.     sn5      n[        R
                  " U5      n[        R                  " U5      n[        R                  " S/[        R                  " US-
  U5      /5      n[        R                  " US-
  /[        R                  " US-
  U5      /5      n	[        R                  " U R                   Vs/ s H"  n[        US5      (       d  M  UR                  PM$     sn6 n
[        XU4U
S9$ s  snf s  snf s  snf )Nc              3  8   #    U  H  n[        U5      v   M     g 7fr)   r4   r5   s     r   r8   '_flatten_space_oneof.<locals>.<genexpr>/  s     7,Qgajj,r;   r#   r   rV   r   )r   r<   rK   r.   rl   minr   r   r   rj   r   r   hasattrrV   r
   )r   num_subspacesr   r7   lowshighsoverall_lowoverall_highr   r   rV   s              r   _flatten_space_oneofr   ,  sK   %M7%,,77!;K885<<H<aRVVM!,001<HIDHHU\\J\bff]1-223\JKE&&,K66%=L
..1#rww{QDE
FC>>MA-.a0VWXDNNellRlga>QWQWWlRSE3+uEE IJ Ss   3G+3GG-Gc                    [        U [        5      (       a   [        U[        5      (       a  [        S5      e[        5       e)a`  Returns if two spaces share a common dtype and shape (plus any critical variables).

This function is primarily used to check for compatibility of different spaces in a vector environment.

Args:
    space_1: A Gymnasium space
    space_2: A Gymnasium space

Returns:
    If the two spaces share a common dtype and shape (plus any critical variables).
zJ`check_dtype_shape_equivalence` doesn't support Generic Gymnasium Spaces, )ru   r   r   	TypeErrorspace_1space_2s     r   is_space_dtype_shape_equivr  >  s7     '5!!j%&@&@!X
 	
 kr!   c                    [        U 5      [        U5      L =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r)   )r   r&   rV   r  s     r   '_is_space_fundamental_dtype_shape_equivr  S  sC     	Wg& 	+MMW]]*	+MMW]]*	r!   c                    [        U[        5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r)   )ru   r   rF   r   r  s     r    _is_space_text_dtype_shape_equivr  `  sE     	7D! 	;'"4"44	;!!W%:%::r!   c                   ^ ^ [        T[        5      =(       aL    T R                  5       TR                  5       :H  =(       a$    [        U U4S jT R                  5        5       5      $ )Nc              3  H   >#    U  H  n[        TU   TU   5      v   M     g 7fr)   r  )r6   rq   r  r  s     r   r8   3_is_space_dict_dtype_shape_equiv.<locals>.<genexpr>n  s*      
% 'ws|WS\BB%   ")ru   r   keysallr  s   ``r    _is_space_dict_dtype_shape_equivr  i  sM     	7D! 	
LLNglln,	
 
||~
 
r!   c                   ^ ^ [        T[        5      =(       a(    [        U U4S j[        [	        T 5      5       5       5      $ )Nc              3  H   >#    U  H  n[        TU   TU   5      v   M     g 7fr)   r  r6   r   r  r  s     r   r8   4_is_space_tuple_dtype_shape_equiv.<locals>.<genexpr>w  s(      .DWq"71:wqz::DWr  )ru   r   r  ranger   r  s   ``r   !_is_space_tuple_dtype_shape_equivr  u  s4    gu% # .DI#g,DW. + r!   c                f   [        U[        5      =(       a    [        U R                  UR                  5      =(       at    U R                  S L =(       a    UR                  S L =(       dJ    U R                  S L=(       a5    UR                  S L=(       a     [        U R                  UR                  5      $ r)   )ru   r   r  r   r   r  s     r   !_is_space_graph_dtype_shape_equivr  |  s     	7E" 		
&w'9'97;M;MN		
 4'FG,>,>$,F ""$. W&&d2W.w/A/A7CUCUVr!   c                   ^ ^ [        T[        5      =(       aF    [        T 5      [        T5      :H  =(       a(    [        U U4S j[	        [        T 5      5       5       5      $ )Nc              3  H   >#    U  H  n[        TU   TU   5      v   M     g 7fr)   r  r  s     r   r8   4_is_space_oneof_dtype_shape_equiv.<locals>.<genexpr>  s*      
( 'wqz71:>>(r  )ru   r   r   r  r  r  s   ``r   !_is_space_oneof_dtype_shape_equivr    sL     	7E" 	
LCL(	
 
3w<(
 
r!   c                    [        U[        5      =(       a?    U R                  UR                  L =(       a     [        U R                  UR                  5      $ r)   )ru   r   r   r  r   r  s     r   $_is_space_sequence_dtype_shape_equivr    sE     	7H% 	UMMW]]*	U&w'<'<g>S>STr!   )r   
Space[Any]r~   r*   )r   Box | MultiBinaryr~   r*   )r   r   r~   r*   )r   r   r~   r*   )r   r   r~   r*   )r   r   r~   r*   )r   r   )r   r   r~   r*   )r   r   r~   r*   )r   Space[T]rQ   rM   r~   FlatType)r   r   rQ   NDArray[Any]r~   r#  )r   r   rQ   np.int64r~   NDArray[np.int64])r   r   rQ   r%  r~   r%  )r   r   rQ   tuple[Any, ...]r~   ztuple[Any, ...] | NDArray[Any])r   r   rQ   dict[str, Any]r~   zdict[str, Any] | NDArray[Any])r   r   rQ   r   r~   r   )r   r   rQ   strr~   NDArray[np.int32])r   r   rQ   tuple[Any, ...] | Anyr~   r*  )r   r   rQ   tuple[int, Any]r~   r#  )r   r!  rQ   r"  r~   rM   )r   r   rQ   r%  r~   r$  )r   r   rQ   NDArray[np.integer[Any]]r~   r,  )r   r   rQ   zNDArray[Any] | tuple[Any, ...]r~   r&  )r   r   rQ   zNDArray[Any] | dict[str, Any]r~   r'  )r   r   rQ   r)  r~   r(  )r   r   rQ   r&  r~   r*  )r   r   rQ   r#  r~   r+  )r   r  r~   z%Box | Dict | Sequence | Tuple | Graph)r   r
   r~   r
   )r   z&Discrete | MultiBinary | MultiDiscreter~   r
   )r   r   r~   zBox | Tuple)r   r   r~   z
Box | Dict)r   r   r~   r   )r   r   r~   r
   )r   r   r~   r   )r   r   r~   r
   )r  r   r  r   r~   bool)r  r   )r  r   )r  r   )r  r   )r  r   )S__doc__
__future__r   operatorr$   	functoolsr   r   typingr   r   r   numpyr.   numpy.typingr	   	gymnasiumr   gymnasium.spacesr
   r   r   r   r   r   r   r   r   r   r   r   r    registerr'   r,   r1   r=   rB   rD   rG   rL   rM   r   r(  rm   r"  rR   rX   r]   rd   rn   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r   r!   r   <module>r9     s   #  , & &         ; ;6 	#+*  * 	(  	- # !# 	%  	$  	%  	$  	%5 5 CLtCH~uS#XMN ); );X 	#+6  6 	(  	- . ! 	%K K 	$H H 	%5 5: 	$  	(GG-GG G" 	%0 0 ; ;& C
K AA!-AA ! A H' ' M"5 #" ESS3SS S& DJ J E5 5, D  HI I" E5 5 >; >;B M M !$&J ' % "J B B 
 
     !K "K F F"  ( $$S)$$X.$$]3$$[1 2 4 / * $$T* + $$T* + $$U+ , $$U+ , $$U+ , $$X. /r!   