
    hR$                       % S r SSKJr  SSK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  SSKrSSKJrJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  \	R<                  rS\ S'   \	R<                  r!S\ S'   \"SS\\#   4   r$\RJ                   " S S5      5       r& " S S\\!\	R<                  \'\#\(\$\&4   5      r) " S S\\5      r* " S S\\5      r+g)z9Implementation of a Jax-accelerated pendulum environment.    )annotations)path)AnyOptional	TypeAliasN)struct)FunctionalJaxEnvFunctionalJaxVectorEnv)DependencyNotInstalled)ActTypeFuncEnv)EzPickle)AutoresetModer   PRNGKeyType	StateTypezpygame.Surfacezpygame.time.Clockc                      \ rS rSr% SrSrS\S'   SrS\S'   SrS\S	'   S
r	S\S'   S
r
S\S'   \R                  rS\S'   S
rS\S'   SrS\S'   Srg)PendulumParams   z,Parameters for the jax Pendulum environment.g       @float	max_speed皙?dtg      $@gg      ?mlhigh_xhigh_yi  int
screen_dim N)__name__
__module____qualname____firstlineno____doc__r   __annotations__r   r   r   r   jnppir   r   r   __static_attributes__r        X/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/phys2d/pendulum.pyr   r      sV    6IuBAuOAuNAuNFFFEFEJr*   r   c                     \ rS rSr% SrSrS\S'   \R                  R                  \
R                  * \
R                  S\
R                  S9r\R                  R                  \* \S\
R                  S9r\4     SS	 jjrS
\4         SS jjr\4       SS jjr\4           SS jjr\4       SS jjr\4       SS jjrSS\4       SS jjr\4   SS jjrSS jrSrg
)PendulumFunctional(   z-Pendulum but in jax and functional structure.g       @r   
max_torque)   )shapedtype)   c                    [         R                  " UR                  UR                  /5      n[        R
                  R                  X* X3R                  S9$ )zInitial state generation.)keyminvalmaxvalr1   )r'   arrayr   r   jaxrandomuniformr1   )selfrngparamshighs       r+   initialPendulumFunctional.initial2   s?     yy&--78zz!!c%JJ!WWr*   Nc                   Uu  pVUnUR                   nUR                  n	UR                  n
UR                  n[        R
                  " XpR                  * U R                  5      S   nUSU-  SU
-  -  [        R                  " U5      -  SXS-  -  -  U-  -   U-  -   n[        R
                  " XR                  * UR                  5      nX\U-  -   n[        R                  " X/5      nU$ )zPendulum transition.r   r0      g      @)
r   r   r   r   r'   clipr/   sinr   r8   )r<   stateactionr=   r>   ththdotur   r   r   r   newthdotnewth	new_states                  r+   
transitionPendulumFunctional.transition9   s     	HHHHHHYYHHQ($//:1=AEQUOcggbk9C1!t8<Lq<PPTVVV88H'7'7&79I9IJ]"IIu/0	r*   c                    Uu  pE[         R                  " [         R                  " U5      [         R                  " U5      U/5      $ )z,Generates an observation based on the state.)r'   r8   cosrE   )r<   rF   r=   r>   thetathetadots         r+   observationPendulumFunctional.observationR   s1      yy#''%.#''%.(CDDr*   c                   Uu  pgUn[         R                  " XR                  * U R                  5      S   nU[         R                  -   S[         R                  -  -  [         R                  -
  n	U	S-  SUS-  -  -   SUS-  -  -   n
U
* $ )z?Generates the reward based on the state, action and next state.r   rC   g?gMbP?)r'   rD   r/   r(   )r<   rF   rG   
next_stater=   r>   rH   rI   rJ   th_normalizedcostss              r+   rewardPendulumFunctional.rewardY   s~     	HHQ($//:1=svv+!cff*5?q 3>1EQTNBvr*   c                    g)z,Determines if the state is a terminal state.Fr    )r<   rF   r=   r>   s       r+   terminalPendulumFunctional.terminall   s     r*   c           	         SSK nSSK Jn  Uu  pxn	UR	                  UR
                  UR
                  45      n
U
R                  S5        SnUR
                  US-  -  nUR
                  S-  nSU-  nS	U-  nSXS-  U* S-  4u  nnnnUU4UU4UU4UU4/n/ nU He  nUR                  R                  U5      R                  US   [        R                  S-  -   5      nUS   U-   US   U-   4nUR                  U5        Mg     UR                  U
US
5        UR                  U
US
5        UR                  XU[!        US-  5      S
5        UR#                  XU[!        US-  5      S
5        US4nUR                  R                  U5      R                  US   [        R                  S-  -   5      n[!        US   U-   5      [!        US   U-   5      4nUR                  U
US   US   [!        US-  5      S
5        UR#                  U
US   US   [!        US-  5      S
5        [$        R&                  " [$        R(                  " [*        5      S5      nUR,                  R/                  U5      nU	b  UR0                  R3                  UU[        R4                  " U	5      -  S-  U[        R4                  " U	5      -  S-  45      n[7        U	S:  5      nUR0                  R9                  UUS5      nU
R;                  UUUR=                  5       R>                  -
  UUR=                  5       R@                  -
  45        UR                  XU[!        SU-  5      S5        UR#                  XU[!        SU-  5      S5        UR0                  R9                  U
SS5      n
UR;                  U
S5        XxU	4[        RB                  " [        RD                  " URF                  RI                  U5      5      SS94$ ! [         a  n[        S5      UeSnAff = f)zRenders an RGB image.r   N)gfxdrawGpygame is not installed, run `pip install "gymnasium[classic_control]"`)   rb   rb   g@rC   r3   g?)   M   rd   zassets/clockwise.pngTr   )r   r   r   F)r   r   )r3   r   rC   )axes)%pygamer`   ImportErrorr   Surfacer   fillmathVector2
rotate_radnpr(   append	aapolygonfilled_polygonaacircler   filled_circler   joindirname__file__imageload	transformsmoothscaleabsboolflipblitget_rectcenterxcentery	transposer8   	surfarraypixels3d)r<   rF   render_stater>   rf   r`   escreenclocklast_usurfboundscaleoffset
rod_length	rod_widthr   rtbcoordstransformed_coordscrod_endfnameimg	scale_imgis_flips                               r+   render_imagePendulumFunctional.render_imager   s   	&
 !-v~~v00&2C2CDE		/"!!UQY/""a'Y
%K	
MI:>A
1aa&1a&1a&1a&1A##A&11%(RUUQY2FGA1!v.A%%a(  	$ 2MBt%7Gvs9q=/A=QdFC	A4FVq/++%%g.99%(RUUQY:NOwqzF*+Sf1D-EF'!*gaj#i!m*<m	
 	'!*gaj#i!m*<m	
 		$,,x02HIll&((44'!+URVVF^-Ca-GHI 6A:&G((--i$GIIIY//1999Y//1999 	vs4%</@)LdFCu4EyQ$$T5$7D&!v&HHV%%..v67i)
 
 	
{  	(Y	s   
O& &
P0O<<PiX  i  c                     SSK nUR                  5         UR	                  X45      nUR
                  R                  5       nXgS4$ ! [         a  n[        S5      UeSnAff = f)zInitialises the render state.r   Nra   )rf   rg   r   initrh   timeClock)r<   screen_widthscreen_heightr>   rf   r   r   r   s           r+   render_initPendulumFunctional.render_init   sh    	 	 =>!!#d""  	(Y	s   A 
A!AA!c                     SSK nUR                  R	                  5         UR	                  5         g! [         a  n[        S5      UeSnAff = f)zCloses the render state.r   Nra   )rf   rg   r   displayquit)r<   r   r>   rf   r   s        r+   render_closePendulumFunctional.render_close   sJ    	
 	  	(Y	s   1 
AAAc                    [        S0 UD6$ )z3Returns the default parameters for the environment.r    )r   )r<   kwargss     r+   get_default_params%PendulumFunctional.get_default_params   s    '''r*   r    )r=   r   r>   r   returnr   )
rF   r   rG   zint | jax.Arrayr=   Noner>   r   r   r   )rF   r   r=   r   r>   r   r   z	jax.Array)rF   r   rG   r   rW   r   r=   r   r>   r   r   r   )rF   r   r=   r   r>   r   r   r{   )rF   r   r   RenderStateTyper>   r   r   z"tuple[RenderStateType, np.ndarray])r   r   r   r   r>   r   r   r   )r   r   r>   r   )r   r   )r!   r"   r#   r$   r%   r/   r&   gymspacesBoxrm   inffloat32observation_spaceaction_spacer   r@   rN   rT   rZ   r]   r   r   r   r   r)   r    r*   r+   r-   r-   (   s    8J

wd"**U::>>:+zRZZ>XL :HXX(6X	X !/   	
  
4 DREE%(E2@E	E "0  	
   
( DR%(2@	 "0	I
I
 &I
 	I

 
,I
Z   !/	## # 	#
 
#. "0% (r*   r-   c                  X   ^  \ rS rSrSrS/SS\R                  S.rS	S
U 4S jjjrSr	U =r
$ )PendulumJaxEnv   zDJax-based pendulum environment using the functional version as base.	rgb_array   T)render_modes
render_fpsr9   autoreset_modec                   > [         R                  " U 4SU0UD6  [        S0 UD6nUR                  [        R
                  5        [        TU ]  UU R                  US9  g)zYConstructor where the kwargs are passed to the base environment to modify the parameters.render_mode)metadatar   Nr    )r   __init__r-   rx   r9   jitsuperr   )r<   r   r   env	__class__s       r+   r   PendulumJaxEnv.__init__   sW    $BKB6B *6*cgg]]# 	 	
r*   r    )N)r   
str | Noner   r   )r!   r"   r#   r$   r%   r   	NEXT_STEPr   r   r)   __classcell__)r   s   @r+   r   r      s-    N %'11	H
 
r*   r   c                  F    \ rS rSrSrS/SSS.r  S
       SS jjrS	rg)PendulumJaxVectorEnv   z@Jax-based implementation of the vectorized CartPole environment.r   2   T)r   r   r9   Nc           	         [         R                  " U 4UUUS.UD6  [        S0 UD6nUR                  [        R
                  5        [        R                  " U UUU R                  UUS9  g)zcConstructor for the vectorized CartPole where the kwargs are applied to the functional environment.)num_envsr   max_episode_steps)func_envr   r   r   r   Nr    )r   r   r-   rx   r9   r   r
   r   )r<   r   r   r   r   r   s         r+   r   PendulumJaxVectorEnv.__init__  sp     		
#/		

 	
 !*6*cgg'']]#/	
r*   r    )N   )r   r   r   r   r   r   r   r   )r!   r"   r#   r$   r%   r   r   r)   r    r*   r+   r   r      sK    J!,RMH
 #'!$	

  
 	

 
 
r*   r   ),r%   
__future__r   osr   typingr   r   r   r9   	jax.numpynumpyr'   rm   flaxr   	gymnasiumr   !gymnasium.envs.functional_jax_envr	   r
   gymnasium.errorr   !gymnasium.experimental.functionalr   r   gymnasium.utilsr   gymnasium.vectorr   Arrayr   r&   r   tupler   r   	dataclassr   r   r{   r-   r   r   r    r*   r+   <module>r      s    ? "  + + 
     V 2 > $ * Y "yy	9  (*=xNO 
 
 
z(Isyy#udO^STz(z
%x 
0
18 
r*   