Module cartpole
Expand description
CartPole-v1 and the Augmented Random Search learner behind the cartpole
problem (D113).
The environment follows Gymnasium’s CartPole-v1: gravity 9.8, a cart of
mass 1.0, a pole of mass 0.1 and half-length 0.5, a push of 10 newtons to
either side, and Euler integration at tau = 0.02 seconds. An episode
terminates when the cart leaves |x| ≤ 2.4 or the pole leaves |θ| ≤ 12°,
and is truncated at 500 steps; its return is its length.
The learner is basic Augmented Random Search (Mania, Guy and Recht 2018,
V1) on a linear deterministic policy w ∈ R⁵: it pushes right iff
w · (x, ẋ, θ, θ̇, 1) > 0. Each iteration draws DIRECTIONS Rademacher
directions δ ∈ {−1, +1}⁵, runs one episode at w + νδ and one at
w − νδ from a shared start state, and moves
w ← w + α / (N σ_R) · Σ (r₊ − r₋) δ, with σ_R the standard deviation of
the 2N returns (no update when it is 0). An epoch is ITERATIONS
iterations followed by EVAL_EPISODES evaluation episodes from fixed
start states; the epoch’s shortfall is 500 minus their mean length.
A run reports, after epoch k, the mean shortfall over its epochs 1..=k:
the area above its learning curve so far, which rewards learning fast as
well as learning well. After crate::training::STEPS epochs that mean is the objective,
so a scheduler’s rung at epoch k compares runs over the same epochs. 0 is
a lower bound: it would mean balancing the pole for all 500 steps of every
evaluation episode from the first epoch on. A checkpoint carries the
shortfall sum with the weights, so a paused run resumes its own mean, and a
PBT or PB2 fork inherits its parent’s: the child’s report at epoch k
averages the parent’s epochs up to the fork and its own after it.
A linear policy only reads the sign of w · s, so plain ARS from w = 0
is scale-equivariant: scaling α and ν together scales every iterate,
and the landscape would depend on ν / α alone. Training therefore starts
from the fixed weights INITIAL_WEIGHTS, which sets a scale: a step size
far below it barely moves the policy in crate::training::STEPS epochs, and a noise far
below it rarely changes an action.
Everything is integer hashing (the SplitMix64 finalizer of
crate::training::unit_noise) and basic float arithmetic: sine and
cosine are fixed polynomials (sin, cos), 10^x is pow10, and
the only other operations are + − × ÷, sqrt and floor, which IEEE 754
rounds exactly. The host and the browser therefore train bit for bit the
same policies.
Structs§
Constants§
- DIRECTIONS
- ARS directions per iteration (
N). - EVAL_
EPISODES - Evaluation episodes after each epoch.
- EVAL_
SEED - The seed of the fixed evaluation start states.
- FORCE
- The push to either side, N.
- GRAVITY
- Gravitational acceleration, m/s².
- HALF_
LENGTH - Half the pole’s length, m.
- INITIAL_
WEIGHTS - The weights every training run starts from (see the module docs): a policy that drops the pole within a few dozen steps.
- ITERATIONS
- ARS iterations per training epoch.
- MASS_
CART - The cart’s mass, kg.
- MASS_
POLE - The pole’s mass, kg.
- MAX_
STEPS - The step at which an episode is truncated, and the best return.
- START_
SPREAD - The half-width of the uniform start-state distribution.
- TAU
- The integration step, s: 50 steps per second of simulated time.
- THETA_
LIMIT - The pole angle beyond which an episode terminates: 12°, in radians.
- TRAIN_
SEED - The seed of the training draws (directions and start states). Every trial trains from it, so a trial’s value is a pure function of its point.
- WEIGHTS
- The policy’s weights: one per observation, then the bias.
- X_LIMIT
- The cart position beyond which an episode terminates, m.
Functions§
- act
- The policy’s action in
state: push right iffw · (s, 1) > 0. - advance
- One Euler step of the dynamics under a push to the right (
true) or left. - cos
cos xfor|x| ≤ 0.25by its Taylor polynomial throughx¹⁰: the truncation error is below0.25¹² / 12! < 1.3e-16(seesin).- episode
- The length of one episode of
weightsfromstart: the steps until termination (the terminating step included), at mostMAX_STEPS. - eval_
start - The
indexth fixed evaluation start state. - evaluate
- The shortfall of
weights: 500 minus the mean length of theEVAL_EPISODESevaluation episodes. - objective
- The objective at
(x, y) = (log10 α, log10 ν): a fresh run’s mean shortfall over itsSTEPSepochs. The heatmap shows it; an unscheduled trial reports it. - pow10
10^xfrom basic arithmetic:x = n + fwithn = ⌊x⌋,10^fasexp(f ln 10 / 16)¹⁶by a degree-11 Taylor polynomial, and10^|n|by exact multiplication (for|n| ≤ 22).- replay
- Every state of one episode of
weightsfrom the first evaluation start, the start included: what the policy panel replays. - sin
sin xfor|x| ≤ 0.25by its Taylor polynomial throughx⁹.- terminated
- Whether
stateends the episode: the cart or the pole is out of bounds. - train
- A fresh run’s
STEPSepochs at(log10 α, log10 ν): the final learner and its value.
Type Aliases§
- State
(x, ẋ, θ, θ̇): cart position and velocity, pole angle (0 is upright, positive leans right) and angular velocity.