Skip to main content

Module pendulum

Module pendulum 

Source
Expand description

Pendulum swing-up continuous-control environment.

PendulumSwingUp is the canonical SAC/TD3/DDPG smoke benchmark (issue #139, part of the SAC decomposition #136). It reimplements the classic Gym Pendulum-v1 task in-tree with no external dependencies and closed-form physics.

A rigid pendulum hangs from a fixed pivot. The agent applies a continuous torque at the pivot and is rewarded for swinging the pendulum upright and holding it there with minimal velocity and control effort.

  • Action: Vec<f32> of length 1 (torque u), clamped to [-2.0, 2.0].
  • Observation (3-dim): [cos θ, sin θ, θ̇]. The angle is encoded as (cos, sin) so the observation is continuous across the ±π wrap.
  • Dynamics: standard Pendulum-v1 physics with g = 10, m = 1, l = 1, dt = 0.05; angular velocity θ̇ clamped to [-8.0, 8.0].
  • Reward: -(θ_norm² + 0.1·θ̇² + 0.001·u²) where θ_norm is the angle wrapped to [-π, π]. Reward is always ≤ 0 and is 0 only at the upright rest state (θ = 0, θ̇ = 0, u = 0).
  • Episode length: 200 steps, then truncated = true. The env never sets terminated.

§Determinism contract

The only source of randomness is the initial angle/velocity drawn at Environment::reset, which is seeded (see PendulumSwingUp::with_seed). Reset is reproducible: two envs with the same seed produce identical episodes. The dynamics themselves are fully deterministic (no RNG), so Environment::restore_state followed by Environment::step reproduces every subsequent StepResult bit-for-bit. The PendulumState snapshot captures the simulation state (theta, theta_dot, steps) but not the reset RNG.

Structs§

PendulumState
Snapshot of PendulumSwingUp’s simulation state.
PendulumSwingUp
Pendulum swing-up task with a continuous torque action.