
    h,              	          S r SSKJr  SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	  SSK
rSSKJr   " S S	\\\\	4      \R                   \\\	   4   5      rg)
z`Implementation of a space that represents the cartesian product of other spaces as a dictionary.    )annotationsN)OrderedDict)KeysViewSequence)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 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$ ) Dict   a  A dictionary of :class:`Space` instances.

Elements of this space are (ordered) dictionaries of elements from the constituent spaces.

Example:
    >>> from gymnasium.spaces import Dict, Box, Discrete
    >>> observation_space = Dict({"position": Box(-1, 1, shape=(2,)), "color": Discrete(3)}, seed=42)
    >>> observation_space.sample()
    {'color': np.int64(0), 'position': array([-0.3991573 ,  0.21649833], dtype=float32)}

    With a nested dict:

    >>> from gymnasium.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete
    >>> Dict(  # doctest: +SKIP
    ...     {
    ...         "ext_controller": MultiDiscrete([5, 2, 2]),
    ...         "inner_state": Dict(
    ...             {
    ...                 "charge": Discrete(100),
    ...                 "system_checks": MultiBinary(10),
    ...                 "job_status": Dict(
    ...                     {
    ...                         "task": Discrete(5),
    ...                         "progress": Box(low=0, high=100, shape=()),
    ...                     }
    ...                 ),
    ...             }
    ...         ),
    ...     }
    ... )

It can be convenient to use :class:`Dict` spaces if you want to make complex observations or actions more human-readable.
Usually, it will not be possible to use elements of this space directly in learning code. However, you can easily
convert :class:`Dict` observations to flat arrays by using a :class:`gymnasium.wrappers.FlattenObservation` wrapper.
Similar wrappers can be implemented to deal with :class:`Dict` actions.
c                  > [        U[        5      (       a  [        UR                  5       5      nO[        U[        R
                  R                  5      (       a$   [        [        UR                  5       5      5      nOF[        U[        5      (       a  [        U5      nO%Uc  [        5       nO[        S[        U5       35      eUR                  5        H  u  pEXA;  a  XQU'   M  [        SU S35      e   Xl        U R                  R                  5        H'  u  pE[        U[        5      (       a  M   SU SU 35       e   [        TU ]=  SSU5        g! [         a    [        UR                  5       5      n Nf = f)a  Constructor of :class:`Dict` space.

This space can be instantiated in one of two ways: Either you pass a dictionary
of spaces to :meth:`__init__` via the ``spaces`` argument, or you pass the spaces as separate
keyword arguments (where you will need to avoid the keys ``spaces`` and ``seed``)

Args:
    spaces: A dictionary of spaces. This specifies the structure of the :class:`Dict` space
    seed: Optionally, you can use this argument to seed the RNGs of the spaces that make up the :class:`Dict` space.
    **spaces_kwargs: If ``spaces`` is ``None``, you need to pass the constituent spaces as keyword arguments, as described above.
NzSUnexpected Dict space input, expecting dict, OrderedDict or Sequence, actual type: zDict space keyword 'z*' already exists in the spaces dictionary.z5Dict space element is not an instance of Space: key='z	', space=)
isinstancer   dictitemscollectionsabcMappingsorted	TypeErrorr   type
ValueErrorspacesr   super__init__)selfr   seedspaces_kwargskeyspace	__class__s         O/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/dict.pyr   Dict.__init__6   s_   " fk**&,,.)F 7 788.fV\\^45
 ))&\F^VFefjkqfrest 
 (--/JC #s *3%/YZ 	 0 .4++++-JCu  ]Fse9UZT[\]  . 	tT*;  . flln-.s   "E #E87E8c                V    [        S U R                  R                  5        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>)Dict.is_np_flattenable.<locals>.<genexpr>p   s     M8Lu**8Ls   )allr   valuesr   s    r    r%   Dict.is_np_flattenablem   s"     M8J8J8LMMM    c                  > Uc>  U R                   R                  5        VVs0 s H  u  p#X#R                  S5      _M     snn$ [        U[        5      (       a  [
        TU ]	  U5        U R                  R                  [        R                  " [        R                  5      R                  [        U R                   5      S9n[        U R                   R                  5       U5       VVVs0 s H"  u  u  p#nX#R                  [	        U5      5      _M$     snnn$ [        U[        5      (       a  UR                  5       U R                   R                  5       :w  a7  [!        SUR                  5        SU R                   R                  5        35      eUR                  5        Vs0 s H#  o"U R                   U   R                  X   5      _M%     sn$ [#        S[%        U5       35      es  snnf s  snnnf s  snf )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:`Dict` space that is used to generate seed values for each of the subspaces. Warning, this does not guarantee unique seeds for all subspaces, though is very unlikely.
* ``Dict`` - A dictionary of seeds for each subspace, requires a seed key for every subspace. This supports seeding of multiple composite subspaces (``Dict["space": Dict[...], ...]`` with ``{"space": {...}, ...}``).

Args:
    seed: An optional int or dictionary of subspace keys to int to seed each PRNG. See above for more details.

Returns:
    A dictionary for the seed values of the subspaces
N)sizezThe seed keys: z" are not identical to space keys: z4Expected seed type: dict, int or None, actual type: )r   r   r   r   intr   	np_randomintegersnpiinfoint32maxlenzipr   keysr   r   r   )r   r   r   subspacesubseedssubseedr   s         r    r   	Dict.seedr   s    <DHKKDUDUDWXDW#Ct,,DWXXc""GL~~.."&&S-= / H
 14DKK4E4E4G0R0R,OSW ]]3w<000R  d##yy{dkk..00 %diik]2TUYU`U`UeUeUgThi  FJYY[Q[cS)..ty99[QQFtDzlS ' Y Rs   G$()G* *G1c           	        Ub  Ub  [        SU SU 35      eUb  [        U[        5      (       d   S[        U5       35       eUR	                  5       U R
                  R	                  5       :X  d3   SUR	                  5        SU R
                  R	                  5        35       eU R
                  R                  5        VVs0 s H  u  p4X4R                  X   S9_M     snn$ Ub  [        U[        5      (       d   S[        U5       35       eUR	                  5       U R
                  R	                  5       :X  d3   SUR	                  5        SU R
                  R	                  5        35       eU R
                  R                  5        VVs0 s H  u  p4X4R                  X#   S	9_M     snn$ U R
                  R                  5        VVs0 s H  u  p4X4R                  5       _M     snn$ s  snnf s  snnf s  snnf )
a  Generates a single random sample from this space.

The sample is an ordered dictionary of independent samples from the constituent spaces.

Args:
    mask: An optional mask for each of the subspaces, expects the same keys as the space
    probability: An optional probability mask for each of the subspaces, expects the same keys as the space

Returns:
    A dictionary with the same key and sampled values from :attr:`self.spaces`
zIOnly one of `mask` or `probability` can be provided, actual values: mask=z, probability=z0Expected sample mask to be a dict, actual type: z?Expected sample mask keys to be same as space keys, mask keys: z, space keys: )maskz<Expected sample probability mask to be a dict, actual type: zKExpected sample probability mask keys to be same as space keys, mask keys: )probability)r   r   r   r   r9   r   r   sample)r   r?   r@   kr   s        r    rA   Dict.sample   s8      7[\`[aaop{o|}  d  OA$t*NO  		t{{//11APQUQZQZQ\P]]klplwlwl|l|l~k  AA1 CG++BSBSBUVBUhaA|||11BUVV$T  bMdS^N_M`ab    "dkk&6&6&88T\]h]m]m]o\pp~  @D  @K  @K  @P  @P  @R  S  TT8
 !% 1 1 3 3HA <<KN<;; 3 
 7;kk6G6G6IJ6I(!A||~%6IJJ W
 Ks   ?G%G+G1c                   ^ ^ [        T[        5      (       aZ  TR                  5       T R                  R                  5       :X  a.  [	        U U4S jT R                  R                  5        5       5      $ g)z?Return boolean specifying if x is a valid member of this space.c              3  N   >#    U  H  nTU   TR                   U   ;   v   M     g 7fr$   r   )r&   r   r   xs     r    r'    Dict.contains.<locals>.<genexpr>   s%     P=OcqvS!11=Os   "%F)r   r   r9   r   r)   )r   rG   s   ``r    containsDict.contains   sK    a1668t{{/?/?/A#APT[[=M=M=OPPPr-   c                     U R                   U   $ )z*Get the space that is associated to `key`.rF   )r   r   s     r    __getitem__Dict.__getitem__   s    {{3r-   c                ,    [        U R                  5      $ )zReturns the keys of the Dict.)r   r   r+   s    r    r9   	Dict.keys   s    $$r-   c                v    [        U[        5      (       d   SU S[        U5       35       eX R                  U'   g)z*Set the space that is associated to `key`.zTrying to set zF to Dict space with value that is not a gymnasium space, actual type: N)r   r   r   r   )r   r   values      r    __setitem__Dict.__setitem__   sL    5
 
 	uC5 fgklqgrfst	u 
 !Cr-   c              #  8   #    U R                    Sh  vN   g N7f)z+Iterator through the keys of the subspaces.NrF   r+   s    r    __iter__Dict.__iter__   s     ;;s   c                ,    [        U R                  5      $ )zAGives the number of simpler spaces that make up the `Dict` space.)r7   r   r+   s    r    __len__Dict.__len__   s    4;;r-   c           
         SSR                  U R                  R                  5        VVs/ s H  u  pU< SU 3PM     snn5      -   S-   $ s  snnf )z,Gives a string representation of this space.zDict(z, z: ))joinr   r   )r   rB   ss      r    __repr__Dict.__repr__   sM     dii4;;;L;L;N O;N41A51#;N OPPSVV	
 Os   Ac                b    [        U[        5      =(       a    U R                  UR                  :H  $ )z5Check whether `other` is equivalent to this instance.)r   r
   r   )r   others     r    __eq__Dict.__eq__   s(     ud# ,u||+	
r-   c                    U R                   R                  5        VVVs0 s H)  u  p#X#R                  U Vs/ s H  oDU   PM	     sn5      _M+     snnn$ s  snf s  snnnf )zCConvert a batch of samples from this space to a JSONable data type.)r   r   to_jsonable)r   sample_nr   r   rA   s        r    re   Dict.to_jsonable   sZ    
 #kk//1
1
 ""h#GhF3Kh#GHH1
 	
#G
s   AAAAc                   U R                   R                  5        VVs0 s H  u  p#X#R                  X   5      _M     nnn[        [	        [        UR                  5       5      5      5      n[        U5       VVVs/ s H+  nUR                  5        VVs0 s H
  u  p'X'U   _M     snnPM-     nnnnU$ s  snnf s  snnf s  snnnf )zCConvert a JSONable data type to a batch of samples from this space.)r   r   from_jsonabler7   nextiterr*   range)	r   rf   r   r   dict_of_list
n_elementsnrQ   results	            r    ri   Dict.from_jsonable   s     #kk//1.
1
 $$X]331 	 .

 d<#6#6#89:;
 :&
& .:-?-?-AB-AzsS(]-AB& 	 
 .
 C
s   B2:B>B8$B>8B>rF   )NN)r   z5None | dict[str, Space] | Sequence[tuple[str, Space]]r   z'dict | int | np.random.Generator | Noner   r   r$   )r   zint | dict[str, Any] | Nonereturnzdict[str, int])r?   dict[str, Any] | Noner@   rs   rr   zdict[str, Any])rG   r   rr   bool)r   strrr   
Space[Any])rr   r   )r   ru   rQ   rv   )rr   r0   )rr   ru   )ra   r   rr   rt   )rf   zSequence[dict[str, Any]]rr   dict[str, list[Any]])rf   rw   rr   zlist[dict[str, Any]])__name__
__module____qualname____firstlineno____doc__r   propertyr%   r   rA   rI   rL   r9   rR   rU   rX   r^   rb   re   ri   __static_attributes____classcell__)r   s   @r    r
   r
      s    #N IM8<5+E5+ 65+ 	5+ 5+n N N% %R '+-1*K#*K +*K 
	*KX %! 


 r-   r
   )r|   
__future__r   collections.abcr   typingr   r   r   r   numpyr3   gymnasium.spaces.spacer   r   ru   r   r
    r-   r    <module>r      sN    f "   # .   (t5c3h &..eCj"A tr-   