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.
CartPoleEnvironment implements the CartPole-v1 physics in vanilla JavaScript. It models a cart on a frictionless track with a rigid pole attached at a pivot point, computing real inverted-pendulum dynamics on every time step. The environment is self-contained — no external libraries are required — and its interface mirrors the OpenAI Gym CartPole-v1 convention of reset() and step().
Class Overview
The complete source ofcartpole-env.js:
Constructor Properties
Every property is set in the constructor and remains constant throughout an episode.reset() is called automatically at the end of the constructor to initialize this.state and this.steps.
Gravitational acceleration (m/s²). Used directly in the pole angular acceleration formula.
Mass of the cart (kg).
Mass of the pole (kg).
Combined mass of cart and pole:
massPole + massCart = 0.1 + 1.0 = 1.1 kg. Computed in the constructor rather than hard-coded.Half the length of the pole (m). The physics equations use this as the distance from the pivot to the pole’s center of mass.
Pre-computed convenience product:
massPole * length = 0.1 × 0.5 = 0.05. Appears repeatedly in the acceleration equations.Magnitude of the force applied to the cart on each step (N). A left action applies
-forceMag; a right action applies +forceMag.Seconds per simulation step. Used as the Euler integration time-delta to update position and velocity from acceleration.
Maximum allowable pole angle before the episode terminates. Computed as
12 * Math.PI / 180.Maximum allowable cart position (m) in either direction from center. If
|x| > 2.4 the episode ends.Maximum number of steps per episode. Reaching this count is considered a successful episode and does not yield a negative reward.
reset()
Resets the environment to a fresh starting state. Called automatically by the constructor and by app.js at the start of every new episode.
[x, xDot, theta, thetaDot] — is drawn independently from a uniform distribution over [-0.05, 0.05].
Returns number[] — a shallow copy of the initial 4-element state array.
step(action)
Advances the simulation by one time step using Euler integration of the inverted-pendulum equations of motion.
Input
Integer action to apply this step.
| Value | Direction | Force applied |
|---|---|---|
0 | Left | -10.0 N |
1 | Right | +10.0 N |
Physics Update Equations
The step computes two accelerations from the current state and the applied force, then integrates them forward bytau = 0.02 seconds:
Intermediate quantity:
Return Value
step() returns a plain object with four fields:
Terminal Conditions
The episode ends (terminal = true) when any of the following is true:
| Condition | Expression |
|---|---|
| Cart out of bounds | Math.abs(state[0]) > 2.4 |
| Pole too far from vertical | Math.abs(state[2]) > thetaThresholdRadians (≈ 0.2094 rad / 12°) |
| Max steps reached | this.steps >= 500 |
Reward Schedule
| Outcome | Reward |
|---|---|
| Survived this step (non-terminal) | +1 |
| Episode ended by reaching 500 steps | +1 |
| Episode ended by going out of bounds or tilting too far | -1 |
The physics equations closely mirror the OpenAI Gym
CartPole-v1 implementation, adapted from Barto, Sutton & Anderson (1983). The state vector, threshold values, force magnitude, and time step are all identical to the Gym reference environment.