
    hk#                     l    S /r SSKrSSKJr  SSKJr  SSKJr  SSS.r	 " S S	\\R                  5      rg)
zKallinteris-Andreas    N)utils)	MujocoEnv)BoxgRQ @)trackbodyiddistancec            
       l    \ rS rSrSrS/ SQ0rSS\S4S\S	\S
\	\\
\-  4   S\
4S jjrS rS rS rSrg)InvertedPendulumEnv   a  
## Description
This environment is the Cartpole environment, based on the work of Barto, Sutton, and Anderson in ["Neuronlike adaptive elements that can solve difficult learning control problems"](https://ieeexplore.ieee.org/document/6313077),
just like in the classic environments, but now powered by the Mujoco physics simulator - allowing for more complex experiments (such as varying the effects of gravity).
This environment consists of a cart that can be moved linearly, with a pole attached to one end and having another end free.
The cart can be pushed left or right, and the goal is to balance the pole on top of the cart by applying forces to the cart.


## Action Space
The agent take a 1-element vector for actions.

The action space is a continuous `(action)` in `[-3, 3]`, where `action` represents
the numerical force applied to the cart (with magnitude representing the amount of
force and sign representing the direction)

| Num | Action                    | Control Min | Control Max | Name (in corresponding XML file) | Joint |Type (Unit)|
|-----|---------------------------|-------------|-------------|----------------------------------|-------|-----------|
| 0   | Force applied on the cart | -3          | 3           | slider                           | slide | Force (N) |


## Observation Space
The observation space consists of the following parts (in order):
- *qpos (2 element):* Position values of the robot's cart and pole.
- *qvel (2 elements):* The velocities of cart and pole (their derivatives).

The observation space is a `Box(-Inf, Inf, (4,), float64)` where the elements are as follows:

| Num | Observation                                   | Min  | Max | Name (in corresponding XML file) | Joint | Type (Unit)              |
| --- | --------------------------------------------- | ---- | --- | -------------------------------- | ----- | ------------------------- |
| 0   | position of the cart along the linear surface | -Inf | Inf | slider                           | slide | position (m)              |
| 1   | vertical angle of the pole on the cart        | -Inf | Inf | hinge                            | hinge | angle (rad)               |
| 2   | linear velocity of the cart                   | -Inf | Inf | slider                           | slide | velocity (m/s)            |
| 3   | angular velocity of the pole on the cart      | -Inf | Inf | hinge                            | hinge | angular velocity (rad/s)  |


## Rewards
The goal is to keep the inverted pendulum stand upright (within a certain angle limit) for as long as possible - as such, a reward of +1 is given for each timestep that the pole is upright.

The pole is considered upright if:
$|angle| < 0.2$.

and `info` also contains the reward.


## Starting State
The initial position state is $\mathcal{U}_{[-reset\_noise\_scale   imes I_{2}, reset\_noise\_scale         imes I_{2}]}$.
The initial velocity state is $\mathcal{U}_{[-reset\_noise\_scale   imes I_{2}, reset\_noise\_scale         imes I_{2}]}$.

where $\mathcal{U}$ is the multivariate uniform continuous distribution.


## Episode End
### Termination
The environment terminates when the Inverted Pendulum is unhealthy.
The Inverted Pendulum is unhealthy if any of the following happens:

1. Any of the state space values is no longer finite.
2. The absolute value of the vertical angle between the pole and the cart is greater than 0.2 radians.

### Truncation
The default duration of an episode is 1000 timesteps.


## Arguments
InvertedPendulum provides a range of parameters to modify the observation space, reward function, initial state, and termination condition.
These parameters can be applied during `gymnasium.make` in the following way:

```python
import gymnasium as gym
env = gym.make('InvertedPendulum-v5', reset_noise_scale=0.1)
```

| Parameter               | Type       | Default                 | Description                                                                                   |
|-------------------------|------------|-------------------------|-----------------------------------------------------------------------------------------------|
| `xml_file`              | **str**    |`"inverted_pendulum.xml"`| Path to a MuJoCo model                                                                        |
| `reset_noise_scale`     | **float**  | `0.01`                  | Scale of random perturbations of initial position and velocity (see `Starting State` section) |

## Version History
* v5:
    - Minimum `mujoco` version is now 2.3.3.
    - Added support for fully custom/third party `mujoco` models using the `xml_file` argument (previously only a few changes could be made to the existing models).
    - Added `default_camera_config` argument, a dictionary for setting the `mj_camera` properties, mainly useful for custom environments.
    - Added `env.observation_structure`, a dictionary for specifying the observation space compose (e.g. `qpos`, `qvel`), useful for building tooling and wrappers for the MuJoCo environments.
    - Added `frame_skip` argument, used to configure the `dt` (duration of `step()`), default varies by environment check environment documentation pages.
    - Fixed bug: `healthy_reward` was given on every step (even if the Pendulum is unhealthy), now it is only given if the Pendulum is healthy (not terminated) (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium/issues/500)).
    - Added `xml_file` argument.
    - Added `reset_noise_scale` argument to set the range of initial states.
    - Added `info["reward_survive"]` which contains the reward.
* v4: All MuJoCo environments now use the MuJoCo bindings in mujoco >= 2.1.3.
* v3: This environment does not have a v3 release. Moved to the [gymnasium-robotics repo](https://github.com/Farama-Foundation/gymnasium-robotics).
* v2: All continuous control environments now use mujoco-py >= 1.5. Moved to the [gymnasium-robotics repo](https://github.com/Farama-Foundation/gymnasium-robotics).
* v1: max_time_steps raised to 1000 for robot based tasks (including inverted pendulum).
* v0: Initial versions release.
render_modeshuman	rgb_arraydepth_array
rgbd_tuplezinverted_pendulum.xml   g{Gz?xml_file
frame_skipdefault_camera_configreset_noise_scalec                    [         R                  R                  " XX$40 UD6  [        [        R
                  * [        R
                  S[        R                  S9nX@l        [        R                  " U UU4UUS.UD6  / SQ[        [        R                  " SU R                  -  5      5      S.U l        U R                  R                  R                  U R                  R                   R                  S.U l        g )N)   )lowhighshapedtype)observation_spacer   r   g      ?)r   
render_fps)qposqvel)r   EzPickle__init__r   npinffloat64_reset_noise_scaler   introunddtmetadatadatar   sizer   observation_structure)selfr   r   r   r   kwargsr   s          d/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/mujoco/inverted_pendulum_v5.pyr!   InvertedPendulumEnv.__init__y   s     	
XQWXRVVG"&&BJJW"3	
 0"7	
 	
 bhhsTWW}56
 IINN''IINN''&
"    c                 v   U R                  XR                  5        U R                  5       n[        [        R
                  " U5      R                  5       (       + =(       d    [        R                  " US   5      S:  5      n[        U(       + 5      nSU0nU R                  S:X  a  U R                  5         X$USU4$ )N   g?reward_surviver   F)do_simulationr   _get_obsboolr"   isfiniteallabsr&   render_moderender)r-   actionobservation
terminatedrewardinfos         r/   stepInvertedPendulumEnv.step   s    6??3mmoK(,,..P266+a.3IC3O

 ^$ &)w&KKMJt;;r1   c                 b   U R                   * nU R                   nU R                  U R                  R                  U R                  R
                  XS9-   nU R                  U R                  R                  U R                  R                  XS9-   nU R                  X45        U R                  5       $ )N)r+   r   r   )
r%   	init_qpos	np_randomuniformmodelnq	init_qvelnv	set_stater6   )r-   	noise_low
noise_highr   r   s        r/   reset_modelInvertedPendulumEnv.reset_model   s    ,,,	,,
~~ 6 6I !7 !
 
 ~~ 6 6I !7 !
 
 	t"}}r1   c                     [         R                  " U R                  R                  U R                  R                  /5      R                  5       $ )N)r"   concatenater*   r   r   ravel)r-   s    r/   r6   InvertedPendulumEnv._get_obs   s/    ~~tyy~~tyy~~>?EEGGr1   )r%   r)   r,   N)__name__
__module____qualname____firstlineno____doc__r)   DEFAULT_CAMERA_CONFIGstrr&   dictfloatr!   rB   rO   r6   __static_attributes__ r1   r/   r	   r	      sr    ]@ 	 
H 08M#'#
#
 #
  $C$45	#

 !#
J<$Hr1   r	   )__credits__numpyr"   	gymnasiumr   gymnasium.envs.mujocor   gymnasium.spacesr   rZ   r    r	   r_   r1   r/   <module>re      s?   $%   +    nH)U^^ nHr1   