CartPole is a classic control problem in reinforcement learning: a pole is mounted on a cart that can slide left or right along a frictionless track, and the agent must keep the pole balanced upright by applying horizontal force. Despite its apparent simplicity, CartPole demands that an agent learn timing, anticipation, and long-term correction — a single wrong push doesn’t cause immediate failure, but it cascades into one if left uncorrected. That combination of a small state space, a clear failure signal, and non-trivial strategy makes CartPole-v1 the standard entry-point benchmark for evaluating deep reinforcement learning algorithms.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/apursley2012/cartpole/llms.txt
Use this file to discover all available pages before exploring further.
The Environment
The browser simulation is a direct JavaScript port of the CartPole-v1 Gym specification. Every physics constant is taken from the OpenAI Gym reference implementation and is reproduced exactly incartpole-env.js.
| Parameter | Value | Description |
|---|---|---|
gravity | 9.8 | Gravitational acceleration (m/s²) |
massCart | 1.0 | Mass of the cart (kg) |
massPole | 0.1 | Mass of the pole (kg) |
totalMass | 1.1 | massPole + massCart (kg) |
length | 0.5 | Half-length of the pole (m) |
poleMassLength | 0.05 | massPole × length |
forceMag | 10.0 | Magnitude of the applied force (N) |
tau | 0.02 | Time step between updates (s) |
thetaThresholdRadians | ≈ 0.2094 | 12° expressed in radians (12 * Math.PI / 180) |
xThreshold | 2.4 | Maximum allowed cart displacement |
maxSteps | 500 | Episode length cap |
The browser implementation matches the CartPole-v1 Gym specification. The same physics constants, terminal conditions, and reward function are used — results are directly comparable to the original Python notebook.
State Space
At every time step the environment returns four continuous observations. The agent receives all four values as its input vector.| Index | Name | Description | Relevant Threshold |
|---|---|---|---|
| 0 | Cart Position (x) | Horizontal position of the cart | ±2.4 units |
| 1 | Cart Velocity (ẋ) | Rate of change of cart position | — |
| 2 | Pole Angle (θ) | Angle of the pole from vertical | ±12° |
| 3 | Pole Angular Velocity (θ̇) | Rate of change of pole angle | — |
Action Space
The agent chooses one of two discrete actions each step:| Action | Label | Effect |
|---|---|---|
0 | Push Left | Apply −10 N force to the cart |
1 | Push Right | Apply +10 N force to the cart |
forceMag = 10.0 N; there is no variable throttle.
Reward Function
The agent receives a reward of +1 for every step it survives, including the final step whenmaxSteps is reached. If the episode ends in failure (cart out of bounds or pole too far tilted), the terminal step awards −1 instead.
cartpole-env.js
Terminal Conditions
An episode ends as soon as any of the following become true:- Cart out of bounds:
|x| > 2.4 - Pole too tilted:
|θ| > 12°(i.e.|θ| > thetaThresholdRadians) - Time limit reached:
steps >= 500
Solve Criterion
The environment is considered solved when the agent achieves an average score of 195 or more over 100 consecutive episodes. This is the standard CartPole-v1 benchmark. In the original Python notebook, two completed training runs achieved averages of 197.25 and 199.43 respectively.Reset
At the start of each episode, all four state values are initialised to independent uniform random values drawn from the interval[−0.05, 0.05]:
cartpole-env.js
Physics Equations
Thestep() method integrates the equations of motion for an inverted pendulum on a cart using Euler integration with time step tau = 0.02 s. The full implementation from cartpole-env.js:
cartpole-env.js
thetaAcc is derived from the coupled dynamics of the pole and cart: gravity pulls the pole tip, the applied force accelerates the cart, and both effects feed back into one another through the cosine coupling terms. The cart acceleration xAcc is then computed from the resolved temp value and the pole’s angular acceleration.