
    hI,                     n    S r SSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
   " S S\R                  5      rg)a  
@author: Olivier Sigaud

A merge between two sources:

* Adaptation of the MountainCar Environment from the "FAReinforcement" library
of Jose Antonio Martin H. (version 1.0), adapted by  'Tom Schaul, tom@idsia.ch'
and then modified by Arnaud de Broissia

* the gymnasium MountainCar environment
itself from
http://incompleteideas.net/sutton/book/MountainCar/MountainCar1.cp
permalink: https://perma.cc/6Z2N-PFWC
    N)spaces)utils)DependencyNotInstalledc                      ^  \ rS rSrSrSS/SS.rSS\S-  4S	 jjrS
\R                  4S jr
SSS.S\S-  S\S-  4U 4S jjjrS rS rS rSrU =r$ )Continuous_MountainCarEnv   a_  
## Description

The Mountain Car MDP is a deterministic MDP that consists of a car placed stochastically
at the bottom of a sinusoidal valley, with the only possible actions being the accelerations
that can be applied to the car in either direction. The goal of the MDP is to strategically
accelerate the car to reach the goal state on top of the right hill. There are two versions
of the mountain car domain in gymnasium: one with discrete actions and one with continuous.
This version is the one with continuous actions.

This MDP first appeared in [Andrew Moore's PhD Thesis (1990)](https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-209.pdf)

```
@TECHREPORT{Moore90efficientmemory-based,
    author = {Andrew William Moore},
    title = {Efficient Memory-based Learning for Robot Control},
    institution = {University of Cambridge},
    year = {1990}
}
```

## Observation Space

The observation is a `ndarray` with shape `(2,)` where the elements correspond to the following:

| Num | Observation                          | Min   | Max  | Unit          |
|-----|--------------------------------------|-------|------|---------------|
| 0   | position of the car along the x-axis | -1.2  | 0.6  | position (m)  |
| 1   | velocity of the car                  | -0.07 | 0.07 | velocity (v)  |

## Action Space

The action is a `ndarray` with shape `(1,)`, representing the directional force applied on the car.
The action is clipped in the range `[-1,1]` and multiplied by a power of 0.0015.

## Transition Dynamics:

Given an action, the mountain car follows the following transition dynamics:

*velocity<sub>t+1</sub> = velocity<sub>t</sub> + force * self.power - 0.0025 * cos(3 * position<sub>t</sub>)*

*position<sub>t+1</sub> = position<sub>t</sub> + velocity<sub>t+1</sub>*

where force is the action clipped to the range `[-1,1]` and power is a constant 0.0015.
The collisions at either end are inelastic with the velocity set to 0 upon collision with the wall.
The position is clipped to the range [-1.2, 0.6] and velocity is clipped to the range [-0.07, 0.07].

## Reward

A negative reward of *-0.1 * action<sup>2</sup>* is received at each timestep to penalise for
taking actions of large magnitude. If the mountain car reaches the goal then a positive reward of +100
is added to the negative reward for that timestep.

## Starting State

The position of the car is assigned a uniform random value in `[-0.6 , -0.4]`.
The starting velocity of the car is always assigned to 0.

## Episode End

The episode ends if either of the following happens:
1. Termination: The position of the car is greater than or equal to 0.45 (the goal position on top of the right hill)
2. Truncation: The length of the episode is 999.

## Arguments

Continuous Mountain Car has two parameters for `gymnasium.make` with `render_mode` and `goal_velocity`.
On reset, the `options` parameter allows the user to change the bounds used to determine the new random state.

```python
>>> import gymnasium as gym
>>> env = gym.make("MountainCarContinuous-v0", render_mode="rgb_array", goal_velocity=0.1)  # default goal_velocity=0
>>> env
<TimeLimit<OrderEnforcing<PassiveEnvChecker<Continuous_MountainCarEnv<MountainCarContinuous-v0>>>>>
>>> env.reset(seed=123, options={"low": -0.7, "high": -0.5})  # default low=-0.6, high=-0.4
(array([-0.5635296,  0.       ], dtype=float32), {})

```

## Version History

* v0: Initial versions release
human	rgb_array   )render_modes
render_fpsNrender_modec                    SU l         SU l        SU l        SU l        SU l        SU l        X l        SU l        [        R                  " U R                  U R                  * /[        R                  S9U l        [        R                  " U R                  U R                  /[        R                  S9U l        Xl        S	U l        S
U l        S U l        S U l        SU l        [&        R(                  " U R                   U R                  S[        R                  S9U l        [&        R(                  " U R                  U R                  [        R                  S9U l        g )Ng      g      ?g333333g333333?gQ??g~jtX?dtypeiX  i  T)   )lowhighshaper   )r   r   r   )
min_action
max_actionmin_positionmax_position	max_speedgoal_positiongoal_velocitypowernparrayfloat32	low_state
high_stater   screen_widthscreen_heightscreenclockisopenr   Boxaction_spaceobservation_space)selfr   r   s      p/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/classic_control/continuous_mountain_car.py__init__"Continuous_MountainCarEnv.__init__t   s     	 +
0


 ((/rzz
 ' 
"JJdooT
 "(T__BJJ"
    actionc                 f   U R                   S   nU R                   S   n[        [        US   U R                  5      U R                  5      nX4U R
                  -  S[        R                  " SU-  5      -  -
  -  nX0R                  :  a  U R                  nX0R                  * :  a  U R                  * nX#-  nX R                  :  a  U R                  nX R                  :  a  U R                  nX R                  :X  a  US:  a  Sn[        X R                  :  =(       a    X0R                  :  5      nSnU(       a  SnU[        R                  " US   S5      S-  -  n[        R                   " X#/[        R"                  S9U l         U R$                  S	:X  a  U R'                  5         U R                   XeS
0 4$ )Nr   r   g{Gzd?   g      Y@   g?r   r	   F)stateminmaxr   r   r   mathcosr   r   r   boolr   r   powr   r    r!   r   render)r,   r1   positionvelocityforce
terminatedrewards          r-   stepContinuous_MountainCarEnv.step   sp   ::a=::a=Cq	4??3T__EDJJ&$((1x<2H)HHHnn$~~H~~o%H'''((H'''((H(((X\H ***Mx;M;M/M

 F$((6!9a(3..XXx2"**E
w&KKMzz6ub88r0   )seedoptionsrD   rE   c                X  > [         TU ]  US9  [        R                  " USS5      u  p4[        R
                  " U R                  R                  X4S9S/5      U l        U R                  S:X  a  U R                  5         [        R
                  " U R                  [        R                  S90 4$ )N)rD   g333333gٿ)r   r   r   r	   r   )superresetr   maybe_parse_reset_boundsr   r    	np_randomuniformr5   r   r<   r!   )r,   rD   rE   r   r   	__class__s        r-   rH   Continuous_MountainCarEnv.reset   s    4  227D$G	XXt~~55#5I1MN
w&KKMxx

"**5r99r0   c                 @    [         R                  " SU-  5      S-  S-   $ )Nr3   r   g?)r   sin)r,   xss     r-   _height!Continuous_MountainCarEnv._height   s    vva"f~$t++r0   c           
      \   U R                   cG  U R                  c   e[        R                  R	                  SU R                  R
                   S35        g  SS KnSSKJn  U R                  c  UR                  5         U R                   S:X  aQ  UR                  R                  5         UR                  R                  U R                  U R                  45      U l
        O,UR!                  U R                  U R                  45      U l
        U R"                  c  UR$                  R'                  5       U l        U R(                  U R*                  -
  nU R                  U-  nSnSnUR!                  U R                  U R                  45      U l        U R,                  R/                  S	5        U R0                  S   n[2        R4                  " U R*                  U R(                  S
5      n	U R7                  U	5      n
[9        [;        XR*                  -
  U-  X-  5      5      nUR<                  R?                  U R,                  USSS9  SnU* S-  US-  US4u  pnn/ nUU4X4X4UU44 H  nUR@                  RC                  U5      RE                  [@        RF                  " SU-  5      5      nURI                  US   XR*                  -
  U-  -   US   U-   U R7                  U5      U-  -   45        M     URK                  U R,                  US5        URM                  U R,                  US5        US-  S4U* S-  S44 H  nUR@                  RC                  U5      RE                  [@        RF                  " SU-  5      5      n[O        US   XR*                  -
  U-  -   5      [O        US   U-   U R7                  U5      U-  -   5      4nURQ                  U R,                  US   US   [O        US-  5      S5        URS                  U R,                  US   US   [O        US-  5      S5        M     [O        U RT                  U R*                  -
  U-  5      n[O        U R7                  U RT                  5      U-  5      nUS-   nURW                  U R,                  UUUS5        URK                  U R,                  UU4UUS-
  4US-   US-
  4/S5        URM                  U R,                  UU4UUS-
  4US-   US-
  4/S5        URX                  R[                  U R,                  SS5      U l        U R                  R]                  U R,                  S5        U R                   S:X  a]  UR^                  Ra                  5         U R"                  Rc                  U Rd                  S   5        UR                  R[                  5         g U R                   S:X  aL  [2        Rf                  " [2        Rh                  " URj                  Rm                  U R                  5      5      SS9$ g ! [         a  n[        S5      UeS nAff = f)NzYou are calling render method without specifying any render mode. You can specify the render_mode at initialization, e.g. gym.make("z", render_mode="rgb_array")r   )gfxdrawzGpygame is not installed, run `pip install "gymnasium[classic_control]"`r	   (      )   rW   rW   d   F)r   r   r   )pointsclosedcolor
   r4   r3   r      g      @)   r^   r^   2         )   rb   r   T)r   r   r   r
   )r   r   r4   )axes)7r   specgymloggerwarnidpygamerT   ImportErrorr   r&   initdisplayset_moder$   r%   Surfacer'   timeClockr   r   surffillr5   r   linspacerQ   listzipdrawaalinesr8   Vector2
rotate_radr9   append	aapolygonfilled_polygonintaacirclefilled_circler   vline	transformflipbliteventpumptickmetadata	transposer    	surfarraypixels3d)r,   ri   rT   eworld_widthscalecarwidth	carheightposrP   ysxys	clearancelrtbcoordscwheelflagxflagy1flagy2s                          r-   r<    Continuous_MountainCarEnv.render   s~   #99(((JJOO""&)),,/JL
 	& ;;KKM7*##%$nn55&&(:(:; %nnd.?.?ASAS-TU::**,DJ''$*;*;;!!K/	NND$5$5t7I7I#JK			'jjm[[**D,=,=sC\\"3...%7DEDIIc%yQ	Y]HqL)Q>
aa&1&1&1a&1A##A&11$((1s72CDAMMaDC"3"33u<<aD9$t||C'85'@@ 2 	$))VY7tyy&)<a<#xi!mQ%78A##A&11$((1s72CDAAaDC"3"33u<<=AaD9$t||C'85'@@AE
 		58U1Xs9s?/C_ !!		58U1Xs9s?/C_ 9 T''$*;*;;uDET\\$"4"45=>"dii	BIIV_ufrk2URZ!4LM	

 	IIV_ufrk2URZ!4LM	
 $$))$))UDA	F+w&LLJJOODMM,78NN!,<<))224;;?@y  -m  	(Y	s   
X 
X+X&&X+c                     U R                   b6  SS KnUR                  R                  5         UR                  5         SU l        g g )Nr   F)r&   ri   rl   quitr(   )r,   ri   s     r-   closeContinuous_MountainCarEnv.close/  s4    ;;"NN!KKMDK #r0   )r*   r'   r   r   r#   r(   r"   r   r   r   r   r   r+   r   r   r&   r%   r$   r5   rq   )Nr   )__name__
__module____qualname____firstlineno____doc__r   strr.   r   ndarrayrB   r}   dictrH   rQ   r<   r   __static_attributes____classcell__)rL   s   @r-   r   r      s}    Rj !+.H
 
C$J  
D!92:: !9F +/t 	:S4Z 	: 	: 	:,fP   r0   r   )r   r8   numpyr   	gymnasiumre   r   gymnasium.envs.classic_controlr   gymnasium.errorr   Envr    r0   r-   <module>r      s0        0 2[  [ r0   