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.
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
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