
    hr'                    X   S r SSKJr  SSKrSSKJr  SSKJr  SSK	J
r
  SSKJr  SSKrSSKJ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  / S
Qr\S\4     SS jj5       r\RA                  \5      \RA                  \5      \RA                  \5      \RA                  \5      S\4   S S jj5       5       5       5       r!\RA                  \5      S\4S!S jj5       r"\RA                  \5      S\4S"S jj5       r#\RA                  \5      S\4S#S jj5       r$\RA                  \5      S\4S$S jj5       r%\RA                  \5      \RA                  \5      S\4S%S jj5       5       r&\ S&       S'S jj5       r'\'RA                  \5      \'RA                  \5      \'RA                  \5      \'RA                  \5       S&   S S jj5       5       5       5       r(\'RA                  \5      S&S!S jj5       r)\'RA                  \5      S&S"S jj5       r*\'RA                  \5       S&     S(S jj5       r+\'RA                  \5       S&     S)S jj5       r,\        S*S j5       r-\-RA                  \5      \-RA                  \5      \-RA                  \5      \-RA                  \5          S+S j5       5       5       5       r.\-RA                  \5            S,S j5       r/\-RA                  \5            S-S j5       r0\-RA                  \5      S.S j5       r1\-RA                  \5            S/S j5       r2g)0zLUtility functions for vector environments to share memory between processes.    )annotationsN)c_bool)singledispatch)SynchronizedArray)Any)CustomSpaceError)BoxDictDiscreteGraphMultiBinaryMultiDiscreteOneOfSequenceSpaceTextTupleflatten)create_shared_memoryread_from_shared_memorywrite_to_shared_memory   c                    [        U [        5      (       a$  [        S[        U 5       S[        U 5       S35      e[	        S[        U 5       SU  35      e)a  Create a shared memory object, to be shared across processes.

This eventually contains the observations from the vectorized environment.

Args:
    space: Observation space of a single environment in the vectorized environment.
    n: Number of environments in the vectorized environment (i.e. the number of processes).
    ctx: The multiprocess module

Returns:
    shared_memory for the shared object across processes.

Raises:
    CustomSpaceError: Space is not a valid :class:`gymnasium.Space` instance
Space of type `zH` doesn't have an registered `create_shared_memory` function. Register `z+` for `create_shared_memory` to support it.zVThe space provided to `create_shared_memory` is not a gymnasium Space instance, type: , 
isinstancer   r   type	TypeErrorspacenctxs      ^/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/vector/utils/shared_memory.pyr   r   !   so    & %d5k]*rswx}s~r  @k  l
 	
 deijoepdqqstysz{
 	
    c           	         U R                   c   eU R                   R                  nUS;   a  [        nUR                  X1[	        [
        R                  " U R                  5      5      -  5      $ )N?)dtypecharr   Arrayintnpprodshape)r!   r"   r#   r(   s       r$   _create_base_shared_memoryr/   >   sU     ;;"""KKE|99UBGGEKK$8 99::r%   c                F   ^^ [        UU4S jU R                   5       5      $ )Nc              3  :   >#    U  H  n[        UTTS 9v   M     g7fr"   r#   Nr   .0subspacer#   r"   s     r$   	<genexpr>._create_tuple_shared_memory.<locals>.<genexpr>N   s      EQX4\   )tuplespacesr    s    ``r$   _create_tuple_shared_memoryr=   L   s!     EJ\\  r%   c                ~    U R                   R                  5        VVs0 s H  u  p4U[        XAUS9_M     snn$ s  snnf )Nr3   )r<   itemsr   )r!   r"   r#   keyr7   s        r$   _create_dict_shared_memoryrA   S   sE      %||1133OS 	!(S993  s   9c                    UR                  [        R                  " [        R                  5      R                  XR
                  -  5      $ N)r*   r,   r(   int32r)   
max_lengthr    s      r$   _create_text_shared_memoryrF   [   s/    99RXXbhh',,a2B2B.BCCr%   c                   ^^ TR                  [        R                  " [        R                  5      R                  T5      4[        UU4S jU R                   5       5      -   $ )Nc              3  :   >#    U  H  n[        UTTS 9v   M     g7fr2   r4   r5   s     r$   r8   ._create_oneof_shared_memory.<locals>.<genexpr>b   s      <EQX4\r:   )r*   r,   r(   int64r)   r;   r<   r    s    ``r$   _create_oneof_shared_memoryrK   `   sI    IIbhhrxx(--q13e <EJ\\< 7  r%   c                     [        SU  S35      e)NzAs zw has a dynamic shape so its not possible to make a static shared memory. For `AsyncVectorEnv`, disable `shared_memory`.)r   r    s      r$   _create_dynamic_shared_memoryrM   g   s$     
eW  L  	M r%   c                    [        U [        5      (       a$  [        S[        U 5       S[        U 5       S35      e[	        S[        U 5       SU  35      e)a<  Read the batch of observations from shared memory as a numpy array.

..notes::
    The numpy array objects returned by `read_from_shared_memory` shares the
    memory of `shared_memory`. Any changes to `shared_memory` are forwarded
    to `observations`, and vice-versa. To avoid any side-effect, use `np.copy`.

Args:
    space: Observation space of a single environment in the vectorized environment.
    shared_memory: Shared object across processes. This contains the observations from the vectorized environment.
        This object is created with `create_shared_memory`.
    n: Number of environments in the vectorized environment (i.e. the number of processes).

Returns:
    Batch of observations as a (possibly nested) numpy array.

Raises:
    CustomSpaceError: Space is not a valid :class:`gymnasium.Space` instance
r   zK` doesn't have an registered `read_from_shared_memory` function. Register `z.` for `read_from_shared_memory` to support it.zYThe space provided to `read_from_shared_memory` is not a gymnasium Space instance, type: r   r   r!   shared_memoryr"   s      r$   r   r   o   s{    . %d5k]*uvz  |A  wB  vC  Cq  r
 	
 ghlmrhsgttvw|v}~
 	
r%   c                    [         R                  " UR                  5       U R                  S9R	                  U4U R
                  -   5      $ )Nr(   )r,   
frombufferget_objr(   reshaper.   rO   s      r$   _read_base_from_shared_memoryrV      s=     ==..0DLL	
u{{ r%   c                T   ^ [        U4S j[        XR                  5       5       5      $ )Nc              3  <   >#    U  H  u  p[        X!TS 9v   M     g7fr"   Nr   r6   memoryr7   r"   s      r$   r8   1_read_tuple_from_shared_memory.<locals>.<genexpr>   s"      "BV 	 A6"B   )r;   zipr<   rO   s     `r$   _read_tuple_from_shared_memoryra      s&     "%m\\"B  r%   c                    U R                   R                  5        VVs0 s H  u  p4U[        XAU   US9_M     snn$ s  snnf )NrZ   )r<   r?   r   )r!   rP   r"   r@   r7   s        r$   _read_dict_from_shared_memoryrc      sJ      %||1133OS 	$XS/AQGG3  s   <c                   ^  [         R                  " UR                  5       [         R                  S9R	                  UT R
                  45      n[        U 4S jU 5       5      $ )NrR   c           
   3     >#    U  HO  nS R                  U Vs/ s H-  nU[        TR                  5      :  d  M  TR                  U   PM/     sn5      v   MQ     gs  snf 7f) N)joinlencharacter_setcharacter_list)r6   valuesvalr!   s      r$   r8   0_read_text_from_shared_memory.<locals>.<genexpr>   sl      	 F 	 "!CU0011 *$$S)!	
 	
 s   AA
A
A)r,   rS   rT   rD   rU   rE   r;   )r!   rP   r"   datas   `   r$   _read_text_from_shared_memoryro      sY     ==..0AII	
ED  	 	 	 	r%   c                  ^^ [         R                  " US   R                  5       [         R                  S9n[	        U4S j[        USS  U R                  5       5       5      m[	        U4S j[        U5       5       5      $ )Nr   rR   c              3  <   >#    U  H  u  p[        X!TS 9v   M     g7frY   r[   r\   s      r$   r8   2_read_one_of_from_shared_memory.<locals>.<genexpr>   s"      "FV 	 A6"Fr_   r   c              3  <   >#    U  H  u  pUTU   U   4v   M     g 7frC    )r6   indexsample_indexsubspace_sampless      r$   r8   rr      s+      #<E 
'5e<=#<r_   )r,   rS   rT   rJ   r;   r`   r<   	enumerate)r!   rP   r"   sample_indexesrw   s     ` @r$   _read_one_of_from_shared_memoryrz      sr     ]]=#3#;#;#=RXXNN "%mAB&7"F   #,^#<  r%   c                    [        U [        5      (       a$  [        S[        U 5       S[        U 5       S35      e[	        S[        U 5       SU  35      e)a0  Write the observation of a single environment into shared memory.

Args:
    space: Observation space of a single environment in the vectorized environment.
    index: Index of the environment (must be in `[0, num_envs)`).
    value: Observation of the single environment to write to shared memory.
    shared_memory: Shared object across processes. This contains the observations from the vectorized environment.
        This object is created with `create_shared_memory`.

Raises:
    CustomSpaceError: Space is not a valid :class:`gymnasium.Space` instance
r   zJ` doesn't have an registered `write_to_shared_memory` function. Register `z-` for `write_to_shared_memory` to support it.zXThe space provided to `write_to_shared_memory` is not a gymnasium Space instance, type: r   r   )r!   ru   valuerP   s       r$   r   r      sw    & %d5k]*tuyz  vA  uB  Bo  p
 	
 fgklqgrfssuv{u|}
 	
r%   c                H   [        [        R                  " U R                  5      5      n[        R                  " UR                  5       U R                  S9n[        R                  " XQU-  US-   U-   [        R                  " X R                  S9R                  5       5        g NrR   r   )
r+   r,   r-   r.   rS   rT   r(   copytoasarrayr   )r!   ru   r|   rP   sizedestinations         r$   _write_base_to_shared_memoryr      sr     rwwu{{#$D-- 5 5 7u{{KKIIDLEAI#56


5,446r%   c                \    [        X#U R                  5       H  u  pEn[        XaXE5        M     g rC   )r`   r<   r   )r!   ru   rk   rP   r|   r]   r7   s          r$   _write_tuple_to_shared_memoryr      s)     $'vell#Kxx> $Lr%   c                l    U R                   R                  5        H  u  pE[        XQX$   X4   5        M     g rC   )r<   r?   r   )r!   ru   rk   rP   r@   r7   s         r$   _write_dict_to_shared_memoryr     s.     ++-x]=OP .r%   c                    U R                   n[        R                  " UR                  5       [        R                  S9n[        R
                  " XQU-  US-   U-   [        X5      5        g r~   )rE   r,   rS   rT   rD   r   r   )r!   ru   rk   rP   r   r   s         r$   _write_text_to_shared_memoryr     sS    D-- 5 5 7rxxHKIIDLEAI#56r%   c                    Uu  pE[         R                  " US   R                  5       [         R                  S9n[         R                  " XaUS-    U5        [        U R                  U   XUSU-      5        g )Nr   rR   r   )r,   rS   rT   rJ   r   r   r<   )r!   ru   rk   rP   subspace_idxspace_valuer   s          r$   _write_oneof_to_shared_memoryr     si     !'L--a 0 8 8 :"((KKIIk%!),l; \"Ea,FV8Wr%   )r!   z
Space[Any]r"   r+   return4dict[str, Any] | tuple[Any, ...] | SynchronizedArray)r!   ,Box | Discrete | MultiDiscrete | MultiBinaryr"   r+   )r!   r   r"   r+   )r!   r
   r"   r+   )r!   r   r"   r+   )r!   r   r"   r+   )r!   zGraph | Sequencer"   r+   )r   )r!   r   rP   z dict | tuple | SynchronizedArrayr"   r+   r   z-dict[str, Any] | tuple[Any, ...] | np.ndarray)r!   r   r"   r+   r   ztuple[str, ...])r!   r   r"   r+   r   tuple[Any, ...])r!   r   ru   r+   r|   z
np.ndarrayrP   r   )r!   r   ru   r+   )r!   r   ru   r+   rk   r   )r!   r
   ru   r+   rk   zdict[str, Any])r!   r   ru   r+   rk   str)r!   r   ru   r+   rk   ztuple[int, Any])3__doc__
__future__r   multiprocessingmpctypesr   	functoolsr   multiprocessing.sharedctypesr   typingr   numpyr,   gymnasium.errorr   gymnasium.spacesr	   r
   r   r   r   r   r   r   r   r   r   r   __all__r   registerr/   r=   rA   rF   rK   rM   r   rV   ra   rc   ro   rz   r   r   r   r   r   r   rt   r%   r$   <module>r      sb   R "   $ :   ,     X  !r


9
 
8 s#x(}-{+BC;7;<?; , . ) $; u%78b  & t$56B  % t$56B D %D u%78b  & u%x(DE2  ) & LM

!A
FI
2
 
@ !!#&!!(+!!-0!!+.QR7KN / 1 , ' !!%( ) !!$' ( !!$')*#& (& !!%(*+$' ) 


 
 H	
 
8   %  *  /  -7 . 0 + &   '???&5? (?   &QQQ%3Q 'Q   & '   '&5 (r%   