Reward Shaping
Potential-based shaping, why it keeps optimal policies, and how shaping backfires.
Sparse rewards make learning slow, and extra rewards that hint at progress make it fast, sometimes toward the wrong goal. A simulated bicycle rewarded for heading toward its destination learned to ride in circles. A year later, Ng, Harada and Russell (1999) showed exactly which extra rewards are safe: differences of a potential function, and nothing else.
What you’ll learn
- State and prove the potential-based shaping theorem: shaping by shifts every policy's value by and so preserves the optimal policies.
- Prove the converse: any shaping term that is not potential-based changes the optimal policy of some MDP.
- Show that potential-based shaping is equivalent to initializing Q-values with (Wiewiora, 2003), and handle terminal states, time limits and changing potentials correctly.
- Diagnose shaping failures, such as reward cycles and per-step rewards in episodic tasks, and compare shaping with curricula and intrinsic rewards.
Shaping, and how it backfires
Reward shaping adds a term to the reward, , so that the agent gets a signal before it ever reaches the real goal. The idea comes from animal training, where behavior is built up by rewarding successive approximations to it. The danger is that changes the problem: the agent maximizes , and the policies that are best for need not be best for .
The general mechanism: any cycle of states whose shaping rewards add up to something positive can be harvested indefinitely, and with enough shaping the harvest outweighs the true goal. Potential-based shaping rules this out. Along any trajectory its discounted terms telescope to the same total, so no cycle can earn more shaping reward than any other path from the same start.
In the bridge world below, every move pays a living reward . The exit is one move from the start and the exit five moves away, moves never slip, and . In this widget an exit's reward arrives one step after entering it, so heading for the is worth . Below which does the near exit beat crossing?
Show the answer
-1.62
Crossing is worth and the near exit . Crossing loses when , that is , or . A constant per-step reward changes which exit is optimal.
Potential-based shaping preserves optimal policies
Along any trajectory, whatever policy generated it, the discounted shaping rewards collapse:
Let . Since is bounded and , , so every trajectory's shaped return is its original return minus , path by path:
Take expectations under any policy, even a history-dependent one, starting from or from :
The shift does not depend on , so the ranking of policies from every state is unchanged and the optimal policies coincide. In an episodic task the sum stops at the terminal time and leaves , which vanishes on every trajectory only if is zero at terminal states (Grześ, 2017).
Shape with the ideal potential, . What does the shaped problem look like?
- Its optimal values are
- Its optimal policy changes, because was computed from the reward
- Its optimal values are all zero, its optimal Q-values are the optimal advantages, and a one-step greedy choice on the expected shaped reward is already optimal
- It is not allowed: a potential may not depend on the reward
Show the answer
C. Its optimal values are all zero, its optimal Q-values are the optimal advantages, and a one-step greedy choice on the expected shaped reward is already optimal
, so . The expected immediate shaped reward is as well, so choosing the action with the best immediate shaped reward is optimal: the ideal potential turns a long-horizon problem into a myopic one. Shaping helps most when approximates .
Only potentials are safe
cannot depend on the action. Suppose for some , with gap . Build an MDP in which both actions move to deterministically and . In , is strictly better in : same future, more reward. In , is strictly better by . So for every action.
The potential is forced. On a self-loop, for any candidate potential, so the only possible candidate is
Suppose for some . Build an MDP in which, from , action stay loops on and action go moves to , and every action loops on . The shaping returns of the two plans differ by exactly :
Choose the rewards so that, in , the plan the shaping disfavors is better by . In the other plan then wins by , and the optimal action in differs. Hence for every pair, that is, everywhere (for by the definition of ).
The argument is constructive: the potential is pinned down by the self-loop values of , and any other shaping term is exposed by an MDP with only two relevant states. Necessity is a worst-case statement, though. A particular non-potential can happen to leave a particular MDP's optimal policy alone; what it cannot offer is a guarantee for every MDP.
Think first Time limits revisited: an episode of a task with no natural end is cut off at step , and the cut is treated as terminal. What happens to a potential-based shaping term?
The telescoped sum stops at the cut and leaves . The shaped return now depends on which state the agent happens to occupy at step , which rewards being in high-potential states at the deadline: the shaping is no longer neutral. Bootstrapping at truncation, as in Agents, Environments & Rewards, removes the problem, because the target still estimates the full shaped return.
Shaping as initialization, and the alternatives
Run tabular Q-learning twice on the same stream of experience: learner with shaping and initial values , and learner without shaping and initial values . Suppose entrywise before an update of with next state . Then the two TD errors are equal:
Both learners change by , so the relation survives the update and, by induction, holds forever. Any action-selection rule that depends on the Q-values only through differences within a state (greedy, ε-greedy, softmax) makes the same choices in both learners, so they behave identically.
Shaping with is therefore the same as starting from the values . A good potential is an informed initialization; an optimistic one drives exploration like optimistic initial values; a pessimistic one discourages it. The equivalence is exact for tabular learners on the same experience. With function approximation the shift may not be representable, and the two can behave differently.
Two refinements extend the theorem. Potentials may change over time: with , the dynamic potential-based shaping of Devlin and Kudenko (2012), the terms still telescope and the policy-invariance guarantee carries over. Harutyunyan, Devlin, Vrancx and Nowé (2015) used dynamic potentials to recast an arbitrary advice reward in potential-based form: they learn a value function for the advice over state–action pairs and use its successive estimates as the potential, which captures the advice in expectation. Behboudian, Satsangi, Taylor, Harutyunyan and Bowling (2022) later showed that this construction can in fact change the optimal policy, and that once it is corrected to restore invariance it no longer speeds up learning with good advice. The guarantee belongs to the exact form of the theorem, not to constructions that resemble it.
| Method | What changes | What is guaranteed | Typical failure |
|---|---|---|---|
| Potential-based shaping | The reward, by | The same optimal policies | A poor slows learning; a nonzero potential at termination or at a cut voids the guarantee |
| Other shaping rewards | The reward, by any | Nothing | Reward cycles and loopholes, like the circling bicycle |
| Curriculum learning | The sequence of tasks or start states | The final task is unchanged | Skills learned on easy tasks may not transfer |
| Intrinsic rewards (curiosity, counts) | The reward, by a novelty bonus that changes as the agent learns | Nothing in general, unless the bonus fades away | The agent chases novelty for its own sake, as in the noisy-TV problem |
Key takeaways
- Shaping rewards change the problem being solved; positive cycles of shaping reward can be harvested forever, as the circling bicycle did.
- Potential-based shaping, , telescopes along every trajectory, shifts every policy's values by , and keeps the optimal policies.
- It is the only safe form: any other changes the optimal policy of some MDP, and the potential is pinned down by 's self-loop values.
- In episodic tasks the potential must vanish at termination; that is why a constant living reward, harmless in continuing tasks, changes policies in episodic ones.
- For tabular learners, shaping with equals initializing Q-values with ; a good potential approximates .
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In a continuing task with you add to every transition. Do the optimal policies change?
- No: is potential-based with , and every value rises by exactly 10
- Yes: the agent now prefers longer paths
- Yes, unless all the original rewards are positive
- No, but only because the rewards are bounded
Show the answer
A. No: is potential-based with , and every value rises by exactly 10
With , . So for every policy. In an episodic task the same constant is not potential-based, because the potential must vanish at termination, and it can change the optimal policy.
A designer shapes with , forgetting the , in a task with and a potential that is not constant. Is this shaping safe?
- Yes: every difference of potentials is potential-based
- Yes, as long as is bounded
- Only in episodic tasks
- No: , and the extra term is an ordinary reward for occupying high-potential states, which can change the optimal policy
Show the answer
D. No: , and the extra term is an ordinary reward for occupying high-potential states, which can change the optimal policy
For to equal for some potential , the self-loop case forces , so , which works only if is constant. The leftover pays the agent for lingering where is high. With the two forms coincide.
Tabular Q-learning with potential-based shaping, started from , behaves exactly like unshaped Q-learning on the same experience started from which values?
Show the answer
C.
With the two TD errors are equal at every step, so the relation is preserved forever, and choices that depend only on differences between actions in the same state are identical (Wiewiora, 2003).
An episodic task ends at a goal state , and you shape with a potential that has , computing on the final transition as on any other. What can go wrong?
- Nothing: potential-based shaping is always safe
- The telescoped shaping leaves , an extra reward for reaching that also grows the sooner is reached, so the optimal policy can change
- The agent can no longer reach the goal
- The values become unbounded
Show the answer
B. The telescoped shaping leaves , an extra reward for reaching that also grows the sooner is reached, so the optimal policy can change
The guarantee needs the leftover term to be the same for every trajectory, which in episodic tasks means at terminal states. With the shaping adds a terminal bonus of 50 at that the original task did not have.
Further reading
- A. Y. Ng, D. Harada & S. Russell, "Policy invariance under reward transformations: theory and application to reward shaping," ICML (1999).
- E. Wiewiora, "Potential-based shaping and Q-value initialization are equivalent," Journal of Artificial Intelligence Research 19, 205–208 (2003).
- J. Randløv & P. Alstrøm, "Learning to drive a bicycle using reinforcement learning and shaping," ICML, 463–471 (1998).
- S. Devlin & D. Kudenko, "Dynamic potential-based reward shaping," AAMAS, 433–440 (2012).
- M. Grześ, "Reward shaping in episodic reinforcement learning," AAMAS, 565–573 (2017).
- P. Behboudian, Y. Satsangi, M. E. Taylor, A. Harutyunyan & M. Bowling, "Policy invariant explicit shaping: an efficient alternative to reward shaping," Neural Computing and Applications 34(3), 1673–1686 (2022).