Reward design and its traps
The agent chases exactly what you reward, not what you meant. Watch a go-kart agent find a loophole, then close it.
In 60 seconds
Reward design and its traps
The agent chases exactly what you reward, not what you meant. Watch a go-kart agent find a loophole, then close it.
Kinds of loopholes (described generally)
| Pattern | What was rewarded | What the agent did |
|---|---|---|
| Farming a bonus | Points for collecting items in a racing game | Drove in circles collecting items that kept coming back, and never finished the race |
| Fooling the sensor | "No mess seen by the camera" | Looked away from the mess, or covered the camera |
| Exploiting a bug | A high score in a simulator | Found a glitch in the physics that let it move in impossible ways |
| Pleasing the judge | Good ratings from a scoring model | Wrote answers that look impressive to the scorer but are not more correct |
Why it happens
- Rewards are shortcuts. We reward something easy to measure (points, pages, "no dirt seen") that usually goes along with what we want. Push hard on the measure and it stops going along.
- Repeatable bonuses. A bonus that can be collected again and again can be worth more than the real goal.
- The agent searches far more than a person would. It tries strange things no designer imagined.
gamma = 0.95
# Plan A: drive straight to the finish (9 moves), get +10 at the end.
plan_a = sum(gamma ** k * r for k, r in enumerate([0] * 8 + [10]))
# Plan B: reach the boost pad (+2), then bounce back and forth on it for ever.
plan_b = sum(gamma ** k * (2 if k % 2 == 0 else 0) for k in range(2, 400))
print(f"finish the race: {plan_a:.1f} bounce on the pad: {plan_b:.1f}")
Designing better rewards
- Bonuses that can be collected again and again
- Rewards for what a sensor sees rather than what is true
- Only small steps rewarded, never the real goal
- Nobody watches what the agent actually does
- Reward the real outcome, and make bonuses one-time or small
- Check against a second, independent measurement
- If you add hints (shaping), make them unable to add up to a loop
- Watch replays of the agent, not only the score
Watch and read more
Lab
A go-kart reward that no longer pays for bouncing on the boost pad, so the agent learns to finish the race.
The problem
A go-kart track has squares 0 to 9. The kart starts at 0 and the finish line is at 9. A boost pad at square 3 gives a bonus. The designer wrote reward_fn meaning "finish the race, and the boost is a nice extra". The state includes pad_used, which is True once the kart has been on the pad in this episode.
- Press Run. Read the squares the trained kart visits. What loophole did it find?
- Add up, by hand, what the loop is worth compared with finishing. Why does the agent prefer it?
- Change only
reward_fnto close the loophole. Some ideas: pay the pad bonus only the first time (usepad_used); remove it; or reward progressnew_pos - old_posplus a prize at the finish. Keep a positive reward for crossing the finish line. - Run again. The kart should now finish in 9 moves.
- Press Check. It tests your reward on a looping route and trains the agent on five seeds.
- Write two sentences: which fix did you choose, and could it create a new loophole?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1You fix the loop by adding -1 for every move, but keep the +2 pad bonus that repeats. Is the loophole closed?Reveal
Mostly, here: one round trip is -1 (back) plus -1 + 2 (onto the pad), which is 0, so bouncing gains nothing, and finishing is better. But it is fragile. If someone later raises the pad bonus to +3, the loop pays again. Fixes that remove the reason for the loop (one-time bonus, or progress rewards) are sturdier than fixes that balance numbers.
Q2Why does rewarding progress, new_pos - old_pos, make every loop worthless?Reveal
Over any route, the progress rewards add up to (final position - starting position), because each step's gain is cancelled if you walk back. A loop returns to the same square, so it adds exactly 0. Only real progress toward the finish earns reward. This is the simplest case of potential-based shaping, with "position" as the potential.
Questions people ask
Why not just punish every bad behaviour?
You can add penalties for loopholes you know about, but a strong agent may find one you did not think of. It is better to reward the true goal as directly as you can, and to keep watching the agent's actual behaviour.
Is reward hacking the agent being sneaky?
Not in any human sense. It has no intention to cheat. It is simply maximising the number we gave it. The loophole was in our instructions.
What is reward shaping?
Adding extra rewards to guide learning, such as small points for moving toward the goal. It can speed learning a lot, but badly designed shaping creates loops, as in the lab. Rewarding progress (change in distance) avoids that.
Can humans give the reward instead of a formula?
Yes, and that is the idea behind learning from human feedback, covered in the next lesson. But humans can be fooled or inconsistent too, so it reduces the problem without removing it.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.