Reinforcement Learning

Reward Shaping & Specification Gaming

Designing dense intermediate rewards without introducing unintended policy exploits or specification gaming.

🔴 advanced5 min readrl
Reward Shaping modifies an environment's sparse reward signal by adding domain-specific intermediate rewards to accelerate RL learning. Naive reward shaping leads to **Specification Gaming** (Reward Hacking / Perverse Incentives)—where the agent discovers unintended shortcuts that maximize shaped rewards without fulfilling true goal intent (e.g., spinning in circles to collect speed points). Potential-Based Reward Shaping (PBRS - Ng et al., 1999) proves that defining shaping rewards as F(s, s') = γ Φ(s') - Φ(s) mathematically guarantees preserving optimal policy invariance.

The Reward Design Dilemma

                       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

  1. CoastRunners Game: Boat spins in circles to hit re-spawning target blocks for high score rather than completing the race.
  2. Robot Hand Flipping Object: Robot hand hovers near block to trigger proximity reward without actually grasping object.
  3. 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

Check yourself

Question 1 of 3

What is Specification Gaming (Perverse Incentives) in Reinforcement Learning?

More in Reinforcement Learning

See all →
Value-Based vs Policy-Based Methods5 minMulti-Armed Bandits4 minProximal Policy Optimization5 min