Skip to content
Physical AI
All experiments

Rewards, penalties and the discounted return

How a per-step reward is built from several weighted terms, how the discount factor γ turns a sequence of rewards into the return G_t, and how the choice of γ can change which behaviour looks best.

Loading simulation…

What it demonstrates

How a per-step reward is built from several weighted terms, how the discount factor γ turns a sequence of rewards into the return G_t, and how the choice of γ can change which behaviour looks best.

How it works

Three fixed (not learned) controllers drive the same cart-pole simulation from the same initial state: uniformly random force, a pole-only PD controller that ignores cart position and has a 0.01 rad angle-sensor offset (so it drifts off the track after a few seconds), and a full-state linear controller that balances and re-centres the cart.

Every 20 ms step yields a reward made of five terms: an alive bonus, an upright penalty proportional to (θ/θmax)², a centring penalty proportional to (x/xmax)², an effort penalty proportional to (F/Fmax)², and a one-off failure penalty on the step the episode terminates. The discounted return is computed backwards with G_t = r_{t+1} + γ G_{t+1}. The table compares G₀ for all three controllers at the current γ.

Adjustable parameters

  • Controller: random, pole-only or full-state
  • Discount γ (0–0.999)
  • Alive bonus, upright, centre and effort weights
  • Failure penalty (0–100)
  • New initial state

Known limitations

  • Nothing learns in this experiment; it shows what a learner would be optimising. The reward terms are illustrative, loosely modelled on the kinds of terms used in locomotion rewards.
  • Real locomotion rewards have many more terms (velocity tracking, foot air time, joint limits, action rate). Menlo's reward design is in a guide page not yet reviewed for this site.

Equations

rt=walive−wθ (θ/θmax)2−wx (x/xmax)2−wu (F/Fmax)2−wfail 1[fell]r_t = w_{alive} - w_{\theta}\,(\theta/\theta_{max})^2 - w_x\,(x/x_{max})^2 - w_u\,(F/F_{max})^2 - w_{fail}\,\mathbb 1[\text{fell}]
Gt=∑k=0∞γkrt+k+1=rt+1+γ Gt+1G_t = \sum_{k=0}^{\infty} \gamma^k r_{t+k+1} = r_{t+1} + \gamma\, G_{t+1}
effective horizon≈11−γ steps\text{effective horizon} \approx \frac{1}{1-\gamma} \text{ steps}

Core update rule

// lib/sim/ch07-reward.ts
r.total = w.alive
        - w.upright * (th / thMax) ** 2
        - w.center  * (x / xMax) ** 2
        - w.effort  * (F / Fmax) ** 2
        - (fell ? w.failure : 0);

// discounted return, computed backwards over the episode
let G = 0;
for (let k = rewards.length - 1; k >= 0; k--) {
  G = rewards[k] + gamma * G;
  returns[k] = G;
}

Explained in Chapter 7: RL Fundamentals