
    h~(                     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)z^
http://incompleteideas.net/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
\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SrU =r$ )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 discrete 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

There are 3 discrete deterministic actions:

- 0: Accelerate to the left
- 1: Don't accelerate
- 2: Accelerate to the right

## Transition Dynamics:

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

*velocity<sub>t+1</sub> = velocity<sub>t</sub> + (action - 1) * force - cos(3 * position<sub>t</sub>) * gravity*

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

where force = 0.001 and gravity = 0.0025. 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:

The goal is to reach the flag placed on top of the right hill as quickly as possible, as such the agent is
penalised with a reward of -1 for each 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.5 (the goal position on top of the right hill)
2. Truncation: The length of the episode is 200.

## Arguments

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("MountainCar-v0", render_mode="rgb_array", goal_velocity=0.1)  # default goal_velocity=0
>>> env
<TimeLimit<OrderEnforcing<PassiveEnvChecker<MountainCarEnv<MountainCar-v0>>>>>
>>> env.reset(seed=123, options={"x_init": np.pi/2, "y_init": 0.5})  # default x_init=np.pi, y_init=1.0
(array([-0.46352962,  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        X l        SU 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&                  " S5      U l        [$        R*                  " U R                  U R                  [        R                  S9U l        g )Ng333333g333333?gQ?g      ?gMbP?g{Gzd?dtypeiX  i  T   )min_positionmax_position	max_speedgoal_positiongoal_velocityforcegravitynparrayfloat32lowhighr   screen_widthscreen_heightscreenclockisopenr   Discreteaction_spaceBoxobservation_space)selfr   r   s      e/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/classic_control/mountain_car.py__init__MountainCarEnv.__init__l   s      *
88T..@

SHHd//@

S	& 
"OOA.!'DHHdiirzz!R    actionc                    U R                   R                  U5      (       d   U< S[        U5       S35       eU R                  u  p#X1S-
  U R                  -  [
        R                  " SU-  5      U R                  * -  -   -  n[        R                  " X0R                  * U R                  5      nX#-  n[        R                  " X R                  U R                  5      nX R                  :X  a  US:  a  Sn[        X R                  :  =(       a    X0R                  :  5      nSnX#4U l        U R                   S:X  a  U R#                  5         [        R$                  " U R                  [        R&                  S9XTS	0 4$ )
Nz (z	) invalid   r   r   g      r	   r   F)r%   containstypestater   mathcosr   r   clipr   r   r   boolr   r   r   renderr   r   )r(   r-   positionvelocity
terminatedrewards         r)   stepMountainCarEnv.step   sE     ))
 
 	2Zr$v,y1	2 
 "ZZaZ4::-X0F4<<-0XXX778nn_dnnE778%6%68I8IJ(((X\H***Mx;M;M/M

 )
w&KKMxx

"**5v5RTTTr,   )seedoptionsr>   r?   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)r>   g333333gٿ)r   r   r   r	   r   )superresetr   maybe_parse_reset_boundsr   r   	np_randomuniformr2   r   r7   r   )r(   r>   r?   r   r   	__class__s        r)   rB   MountainCarEnv.reset   s     	4  227D$G	XXt~~55#5I1MN
w&KKMxx

"**5r99r,   c                 @    [         R                  " SU-  5      S-  S-   $ )Nr   g?g?)r   sin)r(   xss     r)   _heightMountainCarEnv._height   s    vva"f~$t++r,   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	   (      )   rQ   rQ   d   F)r   r   r   )pointsclosedcolor
      r   r/      g      @)   rY   rY   2         )   r]   r   T)r   r   r   r
   )r/   r   rW   )axes)7r   specgymloggerwarnidpygamerN   ImportErrorr   r!   initdisplayset_moder   r    Surfacer"   timeClockr   r   surffillr2   r   linspacerK   listzipdrawaalinesr3   Vector2
rotate_radr4   append	aapolygonfilled_polygonintaacirclefilled_circler   vline	transformflipbliteventpumptickmetadata	transposer   	surfarraypixels3d)r(   rd   rN   eworld_widthscalecarwidth	carheightposrJ   ysxys	clearancelrtbcoordscwheelflagxflagy1flagy2s                          r)   r7   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                     SSSSS.$ )Nr/   r   rW   ) )  )  )r   r   r   )r(   s    r)   get_keys_to_action!MountainCarEnv.get_keys_to_action  s    q!;;r,   c                     U R                   b6  SS KnUR                  R                  5         UR                  5         SU l        g g )Nr   F)r!   rd   rg   quitr#   )r(   rd   s     r)   closeMountainCarEnv.close  s4    ;;"NN!KKMDK #r,   )r%   r"   r   r   r   r   r   r#   r   r   r   r   r'   r   r!   r    r   r2   rl   )Nr   )__name__
__module____qualname____firstlineno____doc__r   strr*   rx   r<   dictrB   rK   r7   r   r   __static_attributes____classcell__)rF   s   @r)   r   r      s    Tn !+.H
SC$J S0U3 U6  #	: Dj: 	: : ,fP<   r,   r   )r   r3   numpyr   	gymnasiumr`   r   gymnasium.envs.classic_controlr   gymnasium.errorr   Envr   r   r,   r)   <module>r      s0   
     0 2Q SWW Q r,   