
    h&                       % S r SSKJ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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  \R6                  rS\S'   \R6                  rS\S'   \S   r \RB                   " S S5      5       r" " S S\\\R6                  \#\$\%\ \"4   5      r& " S S\\5      r' " S S\\5      r(g)z9Implementation of a Jax-accelerated cartpole environment.    )annotations)Any	TypeAliasN)struct)FunctionalJaxEnvFunctionalJaxVectorEnv)DependencyNotInstalled)ActTypeFuncEnv)EzPickle)AutoresetModer   PRNGKeyType	StateType)zpygame.Surfacezpygame.time.Clockc                  
   \ rS rSr% SrSrS\S'   SrS\S'   SrS\S	'   \\-   r	S\S
'   Sr
S\S'   \\
-   rS\S'   SrS\S'   SrS\S'   S\R                  -  S-  rS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S '   S!rg")#CartPoleParams   z,Parameters for the jax CartPole environment.g#@floatgravity      ?masscartg?masspole
total_mass      ?lengthpolemass_length      $@	force_magg{Gz?tau   ih  theta_threshold_radiansg333333@x_thresholdg?x_initFboolsutton_barto_rewardX  intscreen_width  screen_height N)__name__
__module____qualname____firstlineno____doc__r   __annotations__r   r   r   r   r   r   r   nppir    r!   r"   r$   r'   r)   __static_attributes__r*       X/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/phys2d/cartpole.pyr   r      s    6GUHeHe 8+J+FE%.OU.IuC%+bee^c%9U9KFE %%L#M3r4   r   c                     \ rS rSrSr\R                  R                  \R                  * \R                  S\R                  S9r\R                  R                  S5      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)CartPoleFunctional.   z#Cartpole but in jax and functional.   )shapedtype   c                j    [         R                  R                  XR                  * UR                  SS9$ )zInitial state generation.r9   )keyminvalmaxvalr;   )jaxrandomuniformr"   )selfrngparamss      r5   initialCartPoleFunctional.initial6   s1     zz!!]]N6== " 
 	
r4   Nc                   Uu  pVpx[         R                  " US-
  5      UR                  -  n	[         R                  " U5      n
[         R                  " U5      nXR
                  US-  -  U-  -   UR                  -  nUR                  U-  X-  -
  UR                  SUR                  U
S-  -  UR                  -  -
  -  -  nXR
                  U-  U
-  UR                  -  -
  nXTR                  U-  -   nXdR                  U-  -   nXtR                  U-  -   nXR                  U-  -   n[         R                  " XVXx4[         R                  S9nU$ )zCartpole transition.r   r=   gUUUUUU?)r<   )jnpsignr   cossinr   r   r   r   r   r   arrayfloat32)rE   stateactionrF   rG   xx_dottheta	theta_dotforcecosthetasinthetatempthetaaccxaccs                  r5   
transitionCartPoleFunctional.transition>   sA    &+"%#&)9)99775>775>
 **Y\9HDD NNX-?MM6??Xq[86;L;LLLN
 ,,x7(BVEVEVVV

U""

T))

Y..

X 55			1U6ckkJr4   c                    U$ )zCartpole observation.r*   )rE   rQ   rF   rG   s       r5   observationCartPoleFunctional.observation_   s	     r4   c                    Uu  pEpeXCR                   * :  XCR                   :  -  XcR                  * :  -  XcR                  :  -  nU$ )z Checks if the state is terminal.)r!   r    )rE   rQ   rF   rG   rS   _rU   
terminateds           r5   terminalCartPoleFunctional.terminale   sc     e $$$$%%%'66668 5557 	 r4   c                   ^
 Uu  pgpXeR                   * :  XeR                   :  -  XR                  * :  -  XR                  :  -  m
[        R                  R	                  UR
                  U
4S jS 5      n	U	$ )z>Computes the reward for the state transition using the action.c                 J   > [         R                  R                  T S S 5      $ )Nc                     g)Ng      r*   r*   r4   r5   <lambda>=CartPoleFunctional.reward.<locals>.<lambda>.<locals>.<lambda>   s    Tr4   c                     g)Ng        r*   r*   r4   r5   rj   rk      s    3r4   )rB   laxcond)rd   s   r5   rj   +CartPoleFunctional.reward.<locals>.<lambda>   s    CGGLL\;Gr4   c                     g)Nr   r*   r*   r4   r5   rj   ro      s    Cr4   )r!   r    rB   rm   rn   r$   )rE   rQ   rR   
next_staterF   rG   rS   rc   rU   rewardrd   s             @r5   rr   CartPoleFunctional.rewardt   s     e $$$$%%%'66668 5557 	 &&G
 r4   c           	         SSK nSSK Jn  Uu  pxUR                  S-  n	UR
                  U	-  n
SnU
SUR                  -  -  nSnSnUnUR                  UR
                  UR                  45      nUR                  S	5        U* S-  US-  US-  U* S-  4u  nnnnUS
-  nUS   U
-  UR
                  S-  -   nSnUU4UU4UU4UU4/nU Vs/ s H  nUS   U-   US   U-   4PM     nnUR                  UUS5        UR                  UUS5        U* S-  US-  XS-  -
  U* S-  4u  nnnn/ nUU4UU4UU4UU44 HU  nUR                  R                  U5      R                  US   * 5      nUS   U-   US   U-   U-   4nUR                  U5        MW     UR                  UUS5        UR                  UUS5        UR!                  U[#        U5      [#        UU-   5      [#        US-  5      S5        UR%                  U[#        U5      [#        UU-   5      [#        US-  5      S5        UR'                  USUR
                  US5        UR(                  R+                  USS5      nUR-                  US5        Xx4[.        R0                  " [.        R2                  " UR4                  R7                  U5      5      SS94$ ! [         a  n[        S5      UeSnAff = fs  snf )z5Renders an image of the state using the render state.r   N)gfxdrawGpygame is not installed, run `pip install "gymnasium[classic_control]"`r=   r   g      I@g      >@)   rw   rw   g      @g       @d      )r   r   r   )      e   )         FT)r   r   )ry   r   r=   )axes)pygameru   ImportErrorr	   r!   r'   r   Surfacer)   fill	aapolygonfilled_polygonmathVector2
rotate_radappendaacircler&   filled_circlehline	transformflipblitr1   	transposerO   	surfarraypixels3d)rE   rQ   render_staterG   r   ru   escreenclockworld_widthscale	polewidthpolelen	cartwidth
cartheightrS   surflrtb
axleoffsetcartxcartycart_coordscpole_coordscoords                               r5   render_imageCartPoleFunctional.render_image   s4   	&
 %((1,##k1	1v}},-	
~~v22F4H4HIJ		/"Z!^Y]JNZKRSOS
1a#%
!uv22S881v1v1v1v6=HI[!uadUl3[I$Y7t[)< JNM!m#JN	

1a !fq!fq!fq!f5EKK''.991Q4%@E1X%uQx%'7*'DEEu% 6 	$_=t[/BJ
"#	A	
 	J
"#	A	
 	dAv22E9E$$T5$7D&!HHV%%..v67i!
 
 	
}  	(Y	. Js   
J5 
K5
K?KKr%   r(   c                     SSK nUR                  5         UR	                  X#45      nUR
                  R                  5       nXg4$ ! [         a  n[        S5      UeSnAff = f)z;Initialises the render state for a screen width and height.r   Nrv   )r   r   r	   initr   timeClock)rE   rG   r'   r)   r   r   r   r   s           r5   render_initCartPoleFunctional.render_init   se    	 	 =>!!#}  	(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   Nrv   )r   r   r	   displayquit)rE   r   rG   r   r   s        r5   render_closeCartPoleFunctional.render_close   sJ    	
 	  	(Y	s   1 
AAAc                    [        S0 UD6$ )z3Returns the default parameters for the environment.r*   )r   )rE   kwargss     r5   get_default_params%CartPoleFunctional.get_default_params   s    '''r4   r*   )rF   r   rG   r   returnr   )
rQ   r   rR   zint | jax.ArrayrF   NonerG   r   r   r   )rQ   r   rF   r   rG   r   r   	jax.Array)rQ   r   rR   r
   rq   r   rF   r   rG   r   r   r   )rQ   r   r   RenderStateTyperG   r   r   z"tuple[RenderStateType, np.ndarray])rG   r   r'   r&   r)   r&   r   r   )r   r   rG   r   r   r   )r   r   )r+   r,   r-   r.   r/   gymspacesBoxr1   infrP   observation_spaceDiscreteaction_spacer   rH   r]   r`   re   rr   r   r   r   r   r3   r*   r4   r5   r7   r7   .   s    .

wd"**U::&&q)L :H

(6
	
 !/   	
  
D DR%(2@	 DR%(2@	* "0  	
   
< "0	J
J
 &J
 	J

 
,J
\ "0 	  	
 
* GU+5C	(r4   r7   c                  4    \ rS rSrSrS/SSS.rS
SS jjrS	rg)CartPoleJaxEnv   z5Jax-based implementation of the CartPole environment.	rgb_array2   T)render_modes
render_fpsrB   Nc                    [         R                  " U 4SU0UD6  [        S0 UD6nUR                  [        R
                  5        [        R                  " U UU R                  US9  g)zXConstructor for the CartPole where the kwargs are applied to the functional environment.render_mode)metadatar   Nr*   )r   __init__r7   r   rB   jitr   r   )rE   r   r   envs       r5   r   CartPoleJaxEnv.__init__  sU    $BKB6B *6*cgg!!]]#		
r4   r*   )N)r   
str | Noner   r   )r+   r,   r-   r.   r/   r   r   r3   r*   r4   r5   r   r      s    ?!,RMH
 
r4   r   c                  \    \ rS rSrSrS/SS\R                  S.r  S
       SS jjrS	r	g)CartPoleJaxVectorEnvi  z@Jax-based implementation of the vectorized CartPole environment.r   r   T)r   r   rB   autoreset_modeNc           	         [         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   r7   r   rB   r   r   r   )rE   r   r   r   r   r   s         r5   r   CartPoleJaxVectorEnv.__init__  sp     		
#/		

 	
 !*6*cgg'']]#/	
r4   r*   )N   )r   r&   r   r   r   r&   r   r   )
r+   r,   r-   r.   r/   r   	NEXT_STEPr   r   r3   r*   r4   r5   r   r     sZ    J %'11	H #'!$	

  
 	

 
 
r4   r   ))r/   
__future__r   typingr   r   rB   	jax.numpynumpyrK   r1   flaxr   	gymnasiumr   !gymnasium.envs.functional_jax_envr   r   gymnasium.errorr	   !gymnasium.experimental.functionalr
   r   gymnasium.utilsr   gymnasium.vectorr   Arrayr   r0   r   tupler   	dataclassr   r&   r   r#   r7   r   r   r*   r4   r5   <module>r      s    ? " ! 
     V 2 > $ * Y "yy	9  =>   (O(Isyy#udO^STO(d
%x 
($
18 $
r4   