Skip to content

CartPole-v1

CartPole

CartPole()

Bases: Environment[EnvState, EnvParams]

Balance a pole on a cart by pushing the cart left or right.

JAX port of the Gymnasium CartPole-v1 environment. The cart, pole and their velocities start at uniformly random values in [-0.05, 0.05].

  • Observation: Box of shape (4,) with the cart position, cart velocity, pole angle and pole angular velocity.
  • Actions: Discrete(2). 0 pushes the cart to the left, 1 to the right.
  • Reward: 1 for every step, including the terminating one.
  • Termination: the pole angle exceeds params.theta_threshold_radians or the cart position exceeds params.x_threshold.
  • Truncation: after params.max_steps_in_episode steps (500 by default).
References

Parameters and state

EnvParams

Bases: EnvParams

Parameters of the CartPole environment.

Attributes:

  • gravity (float) –

    Gravitational acceleration.

  • masscart (float) –

    Mass of the cart.

  • masspole (float) –

    Mass of the pole.

  • total_mass (float) –

    Combined mass of cart and pole. Not derived from masscart and masspole; update it when changing either.

  • length (float) –

    Half the length of the pole.

  • polemass_length (float) –

    Product of masspole and length. Not derived; update it when changing either.

  • force_mag (float) –

    Magnitude of the force applied to the cart by either action.

  • tau (float) –

    Duration of one step in seconds, used for Euler integration.

  • theta_threshold_radians (float) –

    Pole angle beyond which the episode terminates.

  • x_threshold (float) –

    Cart position beyond which the episode terminates.

  • max_steps_in_episode (int) –

    Number of steps after which the episode is truncated.

EnvState

Bases: EnvState

State of the CartPole environment.

Attributes:

  • x (Array) –

    Horizontal position of the cart.

  • x_dot (Array) –

    Velocity of the cart.

  • theta (Array) –

    Angle of the pole from vertical, in radians.

  • theta_dot (Array) –

    Angular velocity of the pole.

  • time (int) –

    Number of steps taken in the current episode.