CartPole SwingUp environment for Gymnasium
Project description
Gymnasium CartPole SwingUp
A more challenging version of the classic CartPole environment for Gymnasium where the pole starts in a downward position.
Description
This package provides a port of the CartPole SwingUp environment to the modern Gymnasium API. It is based on:
The environment has been updated to work with the latest Gymnasium interface and includes enhanced rendering capabilities.
Installation
# Using pip
pip install gymnasium-cartpole-swingup
# Using uv
uv add gymnasium-cartpole-swingup
For the development version, install directly from GitHub:
pip install git+https://github.com/nkiyohara/gymnasium_cartpole_swingup.git
Usage
import gymnasium as gym
import gymnasium_cartpole_swingup # This import is required to register the environment, even if unused
# Create the environment
env = gym.make("CartPoleSwingUp-v0", render_mode="human")
observation, info = env.reset(seed=42)
for _ in range(1000):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
observation, info = env.reset()
env.close()
Note: The import gymnasium_cartpole_swingup line is necessary to register the environment with Gymnasium, even though it may appear unused. If you're using auto-formatters or linters that remove unused imports, you can add a # noqa comment or disable that specific check:
import gymnasium_cartpole_swingup # noqa: F401
Environment Details
- State: Initially, the pole hangs downward ($\theta \approx \pi$)
- Goal: Swing the pole upright and maintain balance
- Action Space: Force applied to cart $[-1, 1]$
- Observation Space: $[x, \dot{x}, \cos(\theta), \sin(\theta), \dot{\theta}]$
- Reward: Higher when pole is upright and cart is centered
Observation Space Detail
The observation is a 5-dimensional vector:
| Index | Observation | Description | Min | Max |
|---|---|---|---|---|
| 0 | $x$ | Cart position along the track | $-2.4$ | $2.4$ |
| 1 | $\dot{x}$ | Cart velocity | $-\infty$ | $\infty$ |
| 2 | $\cos(\theta)$ | Cosine of the pole angle | $-1.0$ | $1.0$ |
| 3 | $\sin(\theta)$ | Sine of the pole angle | $-1.0$ | $1.0$ |
| 4 | $\dot{\theta}$ | Angular velocity of the pole | $-\infty$ | $\infty$ |
Notes:
- The trigonometric representation $(\cos(\theta), \sin(\theta))$ is used instead of the raw angle to avoid discontinuities in the state space.
- When the pole is upright, $\cos(\theta) = 1$ and $\sin(\theta) = 0$.
- When the pole is hanging down, $\cos(\theta) = -1$ and $\sin(\theta) = 0$.
Action Space Detail
The action is a 1-dimensional continuous value:
| Index | Action | Description | Min | Max |
|---|---|---|---|---|
| 0 | $F$ | Horizontal force applied to the cart | $-1.0$ | $1.0$ |
Notes:
- The force is scaled internally by a factor of $10.0$
- Positive values move the cart to the right
- Negative values move the cart to the left
Reward Function
The reward function is a product of two components:
-
Pole angle component: $\frac{\cos(\theta) + 1}{2}$
- Maximum value of $1.0$ when the pole is upright ($\cos(\theta) = 1$)
- Minimum value of $0.0$ when the pole is hanging down ($\cos(\theta) = -1$)
-
Cart position component: $\cos\left(\frac{x}{x_{threshold}} \cdot \frac{\pi}{2}\right)$
- Maximum value of $1.0$ when the cart is centered ($x = 0$)
- Decreases to $0.0$ as the cart approaches the boundaries ($x = \pm 2.4$)
Total reward = pole angle component $\times$ cart position component
System Dynamics
The system dynamics follow the standard cart-pole physics model. The state update equations are:
$\ddot{x} = \frac{-2m_p l \dot{\theta}^2 \sin(\theta) + 3m_p g \sin(\theta)\cos(\theta) + 4F - 4b\dot{x}}{4(m_c + m_p) - 3m_p \cos^2(\theta)}$
$\ddot{\theta} = \frac{-3m_p l \dot{\theta}^2 \sin(\theta)\cos(\theta) + 6(m_c + m_p)g\sin(\theta) + 6(F - b\dot{x})\cos(\theta)}{4l(m_c + m_p) - 3m_p l \cos^2(\theta)}$
Where:
- $m_c = 0.5$ (kg): Mass of the cart
- $m_p = 0.5$ (kg): Mass of the pole
- $l = 0.6$ (m): Half-length of the pole
- $g = 9.82$ (m/s²): Gravitational acceleration
- $b = 0.1$: Friction coefficient
- $F$: Applied force, scaled from action value
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gymnasium_cartpole_swingup-0.1.0.tar.gz.
File metadata
- Download URL: gymnasium_cartpole_swingup-0.1.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2bf04065e2f200385490098de83a699c03caae5716bbc2e7c5734c76c4a756f
|
|
| MD5 |
4fcb4aa96b20e4325169d42aef0db497
|
|
| BLAKE2b-256 |
7843fbf67f278586c6946ee63265deb95369cb21caef205ffd58216e7b9c0231
|
Provenance
The following attestation bundles were made for gymnasium_cartpole_swingup-0.1.0.tar.gz:
Publisher:
publish-pypi.yml on nkiyohara/gymnasium-cartpole-swingup
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gymnasium_cartpole_swingup-0.1.0.tar.gz -
Subject digest:
f2bf04065e2f200385490098de83a699c03caae5716bbc2e7c5734c76c4a756f - Sigstore transparency entry: 194128339
- Sigstore integration time:
-
Permalink:
nkiyohara/gymnasium-cartpole-swingup@c7c02e033dd9e9b3bfbc8e2b047044eeb4369614 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nkiyohara
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@c7c02e033dd9e9b3bfbc8e2b047044eeb4369614 -
Trigger Event:
release
-
Statement type:
File details
Details for the file gymnasium_cartpole_swingup-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gymnasium_cartpole_swingup-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0607d603b82e56a6c732ebe94ce70a161398ed56353e8044c34c9b9ea2c1190b
|
|
| MD5 |
10afbf867a06ba71cd686f3750da6976
|
|
| BLAKE2b-256 |
f3d5af003648ef6c8dba50f8ef0b98c7f097a57535a3a8fbca51ff505364af8d
|
Provenance
The following attestation bundles were made for gymnasium_cartpole_swingup-0.1.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on nkiyohara/gymnasium-cartpole-swingup
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gymnasium_cartpole_swingup-0.1.0-py3-none-any.whl -
Subject digest:
0607d603b82e56a6c732ebe94ce70a161398ed56353e8044c34c9b9ea2c1190b - Sigstore transparency entry: 194128340
- Sigstore integration time:
-
Permalink:
nkiyohara/gymnasium-cartpole-swingup@c7c02e033dd9e9b3bfbc8e2b047044eeb4369614 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nkiyohara
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@c7c02e033dd9e9b3bfbc8e2b047044eeb4369614 -
Trigger Event:
release
-
Statement type: