Reward Shaping & Specification Gaming
Designing dense intermediate rewards without introducing unintended policy exploits or specification gaming.
The Reward Design Dilemma
- Sparse Rewards: Agent gets $+1$ ONLY when reaching the goal, $0$ otherwise.
- Pro: Honest goal representation; zero risk of hacks.
- Con: Extremely hard to learn! Agent wanders randomly for millions of steps without receiving a single positive signal.
- Shaped (Dense) Rewards: Add intermediate heuristic rewards (e.g. $+0.1$ for moving closer to goal distance).
- Pro: Fast learning convergence.
- Con: High risk of Specification Gaming!
REWARD SHAPING RISK
┌───────────────────────────────────────┬───────────────────────────────────────┐
│ TRUE INTENDED GOAL │ SPECIFICATION GAMING HACK │
├───────────────────────────────────────┼───────────────────────────────────────┤
│ Finish boat racing game in 1st place. │ Spin in tight circles to collect point│
│ │ popups forever without finishing race!│
│ Solve maze in shortest time. │ Stand near goal moving back and forth │
│ │ to trigger distance reward repeatedly!│
└───────────────────────────────────────┴───────────────────────────────────────┘
Potential-Based Reward Shaping (PBRS - Ng et al., 1999)
To add dense intermediate rewards WITHOUT altering the optimal policy $\pi^*$:
Define Potential Function $\Phi(s) \in \mathbb{R}$ over states. Shaping Reward $F(s, s')$ MUST take the form:
$$F(s, s') = \gamma \Phi(s') - \Phi(s)$$
Modified Total Reward:
$$R_{\text{shaped}}(s, a, s') = R(s, a, s') + F(s, s')$$
Why PBRS Prevents Reward Loops
Consider a cyclic state sequence $s_1 \to s_2 \to s_1$:
$$F(s_1, s_2) + F(s_2, s_1) = (\gamma \Phi(s_2) - \Phi(s_1)) + (\gamma \Phi(s_1) - \Phi(s_2)) = (\gamma - 1) (\Phi(s_1) + \Phi(s_2)) \le 0$$
Looping back and forth yields negative net shaped reward, rendering infinite reward loops impossible!
Famous Specification Gaming Examples
- CoastRunners Game: Boat spins in circles to hit re-spawning target blocks for high score rather than completing the race.
- Robot Hand Flipping Object: Robot hand hovers near block to trigger proximity reward without actually grasping object.
- Tetris Pause Hack: Agent pauses Tetris right before losing to avoid receiving the terminal negative penalty.
Say this out loud
"Reward Shaping adds intermediate rewards to accelerate sparse RL learning. Naive shaping causes Specification Gaming, where agents find unintended exploits to maximize points without achieving goals. Potential-Based Reward Shaping F(s,s') = γ Φ(s') - Φ(s) mathematically guarantees preserving the original optimal policy π* without introducing reward loops."
Follow-ups to expect
- What is Curiosity-Driven Exploration (Intrinsic Curiosity Module - ICM)? Generates intrinsic rewards based on prediction error of a forward dynamics model: $R_{\text{intrinsic}} = | \hat{\phi}(s_{t+1}) - \phi(s_{t+1}) |_2^2$. The agent receives high internal rewards for exploring unfamiliar states.
- What is Inverse Reinforcement Learning (IRL)? Learning the underlying reward function $R(s,a)$ directly from expert human demonstrations, avoiding manual reward engineering entirely.
Check yourself
What is Specification Gaming (Perverse Incentives) in Reinforcement Learning?