Reward Shaping & Specification Gaming

Reward the wrong thing and the agent will find a loophole, every time.

🔴 advanced5 min readrl
Reward shaping adds small extra rewards to guide an agent toward its goal faster when the real reward is rare. The danger is that the agent optimises exactly what you reward, not what you meant. In one famous case, a boat-racing agent learned to circle forever collecting bonus points instead of finishing the race. This is called reward hacking. A safe way to shape rewards is to base them on progress toward the goal, a method proven not to change which behaviour is truly best.

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 Φ(s)∈R\Phi(s) \in \mathbb{R} over states. Shaping Reward F(s,s′)F(s, s') MUST take the form:

F(s,s′)=γΦ(s′)−Φ(s)F(s, s') = \gamma \Phi(s') - \Phi(s)

Modified Total Reward:

Rshaped(s,a,s′)=R(s,a,s′)+F(s,s′)R_{\text{shaped}}(s, a, s') = R(s, a, s') + F(s, s')

Why PBRS Prevents Reward Loops

Consider a cyclic state sequence s1→s2→s1s_1 \to s_2 \to s_1:

F(s1,s2)+F(s2,s1)=(γΦ(s2)−Φ(s1))+(γΦ(s1)−Φ(s2))=(γ−1)(Φ(s1)+Φ(s2))≤0F(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