Simple Algorithm
Reinforcement Learning · Fixed Point · 26 min

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 γΦ(s′)−Φ(s)\gamma\Phi(s') - \Phi(s) shifts every policy's value by −Φ(s)-\Phi(s) 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 Φ\Phi (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 F(s,a,s′)F(s,a,s') to the reward, R′=R+FR' = R + F, 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 FF changes the problem: the agent maximizes R+FR + F, and the policies that are best for R+FR + F need not be best for RR.

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.

Predict before you look

In the bridge world below, every move pays a living reward cc. The +1+1 exit is one move from the start and the +10+10 exit five moves away, moves never slip, and γ=0.9\gamma = 0.9. In this widget an exit's reward arrives one step after entering it, so heading for the +1+1 is worth c+0.9⋅1c + 0.9 \cdot 1. Below which cc does the near exit beat crossing?

Show the answer

-1.62

Crossing is worth c(1+γ+γ2+γ3+γ4)+10γ5c(1 + \gamma + \gamma^2 + \gamma^3 + \gamma^4) + 10\gamma^5 and the near exit c+γc + \gamma. Crossing loses when c(γ+γ2+γ3+γ4)<γ−10γ5c(\gamma + \gamma^2 + \gamma^3 + \gamma^4) < \gamma - 10\gamma^5, that is 3.0951 c<0.9−5.90493.0951\,c < 0.9 - 5.9049, or c<−1.617c < -1.617. A constant per-step reward changes which exit is optimal.

Potential-based shaping preserves optimal policies

DerivationProof of sufficiency: the shaping terms telescope
  1. ∑t=0N−1γt(γΦ(St+1)−Φ(St))=γNΦ(SN)−Φ(S0)\sum_{t=0}^{N-1}\gamma^t\big(\gamma\Phi(S_{t+1}) - \Phi(S_t)\big) = \gamma^N\Phi(S_N) - \Phi(S_0)

    Along any trajectory, whatever policy generated it, the discounted shaping rewards collapse:

  2. G0′=G0−Φ(S0)G'_0 = G_0 - \Phi(S_0)

    Let N→∞N \to \infty. Since Φ\Phi is bounded and γ<1\gamma < 1, γNΦ(SN)→0\gamma^N\Phi(S_N) \to 0, so every trajectory's shaped return is its original return minus Φ(S0)\Phi(S_0), path by path:

  3. VM′π(s)=VMπ(s)−Φ(s),QM′π(s,a)=QMπ(s,a)−Φ(s)V^\pi_{M'}(s) = V^\pi_M(s) - \Phi(s),\qquad Q^\pi_{M'}(s,a) = Q^\pi_M(s,a) - \Phi(s)

    Take expectations under any policy, even a history-dependent one, starting from ss or from (s,a)(s,a):

  4. The shift does not depend on π\pi, so the ranking of policies from every state is unchanged and the optimal policies coincide. ■\blacksquare In an episodic task the sum stops at the terminal time TT and leaves γTΦ(ST)\gamma^T\Phi(S_T), which vanishes on every trajectory only if Φ\Phi is zero at terminal states (Grześ, 2017).

Check your understanding

Shape with the ideal potential, Φ=VM⋆\Phi = V^\star_M. What does the shaped problem look like?

  1. Its optimal values are 2VM⋆2V^\star_M
  2. Its optimal policy changes, because Φ\Phi was computed from the reward
  3. 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
  4. 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

VM′⋆=VM⋆−Φ=0V^\star_{M'} = V^\star_M - \Phi = 0, so QM′⋆(s,a)=QM⋆(s,a)−VM⋆(s)=A⋆(s,a)≤0Q^\star_{M'}(s,a) = Q^\star_M(s,a) - V^\star_M(s) = A^\star(s,a) \le 0. The expected immediate shaped reward is E[R+γV⋆(S′)−V⋆(s)∣s,a]=A⋆(s,a)\E[R + \gamma V^\star(S') - V^\star(s) \mid s,a] = A^\star(s,a) 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 Φ\Phi approximates V⋆V^\star.

Only potentials are safe

DerivationProof of necessity (γ < 1, at least two actions)
  1. FF cannot depend on the action. Suppose F(s,a1,s′)>F(s,a2,s′)F(s,a_1,s') > F(s,a_2,s') for some s,s′s, s', with gap Δ\Delta. Build an MDP in which both actions move ss to s′s' deterministically and R(s,a2)=R(s,a1)+Δ/2R(s,a_2) = R(s,a_1) + \Delta/2. In MM, a2a_2 is strictly better in ss: same future, more reward. In M′M', a1a_1 is strictly better by Δ/2\Delta/2. So F(s,a,s′)=F(s,s′)F(s,a,s') = F(s,s') for every action.

  2. Φ(s):=−F(s,s)1−γ\Phi(s) := -\frac{F(s,s)}{1-\gamma}

    The potential is forced. On a self-loop, F(s,s)=γΦ(s)−Φ(s)F(s,s) = \gamma\Phi(s) - \Phi(s) for any candidate potential, so the only possible candidate is

  3. F(s,s′)+γF(s′,s′)1−γ⏟go, then loop at s′  −  F(s,s)1−γ⏟loop at s  =  F(s,s′)−γΦ(s′)+Φ(s)  =  D\underbrace{F(s,s') + \frac{\gamma F(s',s')}{1-\gamma}}_{\text{go, then loop at } s'} \;-\; \underbrace{\frac{F(s,s)}{1-\gamma}}_{\text{loop at } s} \;=\; F(s,s') - \gamma\Phi(s') + \Phi(s) \;=\; D

    Suppose D:=F(s,s′)−γΦ(s′)+Φ(s)≠0D := F(s,s') - \gamma\Phi(s') + \Phi(s) \ne 0 for some s≠s′s \ne s'. Build an MDP in which, from ss, action stay loops on ss and action go moves to s′s', and every action loops on s′s'. The shaping returns of the two plans differ by exactly DD:

  4. Choose the rewards so that, in MM, the plan the shaping disfavors is better by ∣D∣/2\abs{D}/2. In M′M' the other plan then wins by ∣D∣/2\abs{D}/2, and the optimal action in ss differs. Hence D=0D = 0 for every pair, that is, F(s,s′)=γΦ(s′)−Φ(s)F(s,s') = \gamma\Phi(s') - \Phi(s) everywhere (for s=s′s = s' by the definition of Φ\Phi). ■\blacksquare

The argument is constructive: the potential is pinned down by the self-loop values of FF, 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 FF 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 NN, and the cut is treated as terminal. What happens to a potential-based shaping term?

The telescoped sum stops at the cut and leaves γNΦ(SN)−Φ(S0)\gamma^N\Phi(S_N) - \Phi(S_0). The shaped return now depends on which state the agent happens to occupy at step NN, 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 R+F+γV(SN)R + F + \gamma V(S_N) still estimates the full shaped return.

Shaping as initialization, and the alternatives

DerivationShaping equals initialization (Wiewiora, 2003)
  1. δL′=r+γmax⁡a′QL′(s′,a′)−QL′(s,a)=r+γΦ(s′)−Φ(s)+γmax⁡a′QL(s′,a′)−QL(s,a)=δL\delta_{L'} = r + \gamma\max_{a'}Q_{L'}(s',a') - Q_{L'}(s,a) = r + \gamma\Phi(s') - \Phi(s) + \gamma\max_{a'}Q_{L}(s',a') - Q_{L}(s,a) = \delta_L

    Run tabular Q-learning twice on the same stream of experience: learner LL with shaping F=γΦ(s′)−Φ(s)F = \gamma\Phi(s') - \Phi(s) and initial values Q0Q_0, and learner L′L' without shaping and initial values Q0+ΦQ_0 + \Phi. Suppose QL′=QL+ΦQ_{L'} = Q_L + \Phi entrywise before an update of (s,a)(s,a) with next state s′s'. Then the two TD errors are equal:

  2. QL′(t)(s,a)=QL(t)(s,a)+Φ(s)for all tQ_{L'}^{(t)}(s,a) = Q_L^{(t)}(s,a) + \Phi(s)\qquad\text{for all } t

    Both learners change Q(s,a)Q(s,a) by αδ\alpha\delta, 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.

  3. Shaping with Φ\Phi is therefore the same as starting from the values Φ\Phi. 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 Φ\Phi may not be representable, and the two can behave differently.

Two refinements extend the theorem. Potentials may change over time: with F=γΦ(s′,t′)−Φ(s,t)F = \gamma\Phi(s', t') - \Phi(s, t), 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.

MethodWhat changesWhat is guaranteedTypical failure
Potential-based shapingThe reward, by γΦ(s′)−Φ(s)\gamma\Phi(s') - \Phi(s)The same optimal policiesA poor Φ\Phi slows learning; a nonzero potential at termination or at a cut voids the guarantee
Other shaping rewardsThe reward, by any FFNothingReward cycles and loopholes, like the circling bicycle
Curriculum learningThe sequence of tasks or start statesThe final task is unchangedSkills learned on easy tasks may not transfer
Intrinsic rewards (curiosity, counts)The reward, by a novelty bonus that changes as the agent learnsNothing in general, unless the bonus fades awayThe 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, F=γΦ(s′)−Φ(s)F = \gamma\Phi(s') - \Phi(s), telescopes along every trajectory, shifts every policy's values by −Φ-\Phi, and keeps the optimal policies.
  • It is the only safe form: any other FF changes the optimal policy of some MDP, and the potential is pinned down by FF'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 Φ\Phi equals initializing Q-values with Φ\Phi; a good potential approximates V⋆V^\star.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In a continuing task with γ=0.9\gamma = 0.9 you add F=1F = 1 to every transition. Do the optimal policies change?

  1. No: F=1F = 1 is potential-based with Φ≡−10\Phi \equiv -10, and every value rises by exactly 10
  2. Yes: the agent now prefers longer paths
  3. Yes, unless all the original rewards are positive
  4. No, but only because the rewards are bounded
Show the answer

A. No: F=1F = 1 is potential-based with Φ≡−10\Phi \equiv -10, and every value rises by exactly 10

With Φ≡−c/(1−γ)=−10\Phi \equiv -c/(1-\gamma) = -10, γΦ−Φ=(1−γ)⋅10=1=F\gamma\Phi - \Phi = (1-\gamma)\cdot 10 = 1 = F. So V′=V−Φ=V+10V' = V - \Phi = V + 10 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.

Question 2

A designer shapes with F(s,a,s′)=Φ(s′)−Φ(s)F(s,a,s') = \Phi(s') - \Phi(s), forgetting the γ\gamma, in a task with γ=0.9\gamma = 0.9 and a potential that is not constant. Is this shaping safe?

  1. Yes: every difference of potentials is potential-based
  2. Yes, as long as Φ\Phi is bounded
  3. Only in episodic tasks
  4. No: F=γΦ(s′)−Φ(s)+(1−γ)Φ(s′)F = \gamma\Phi(s') - \Phi(s) + (1-\gamma)\Phi(s'), and the extra term is an ordinary reward for occupying high-potential states, which can change the optimal policy
Show the answer

D. No: F=γΦ(s′)−Φ(s)+(1−γ)Φ(s′)F = \gamma\Phi(s') - \Phi(s) + (1-\gamma)\Phi(s'), and the extra term is an ordinary reward for occupying high-potential states, which can change the optimal policy

For FF to equal γΨ(s′)−Ψ(s)\gamma\Psi(s') - \Psi(s) for some potential Ψ\Psi, the self-loop case forces 0=(γ−1)Ψ(s)0 = (\gamma - 1)\Psi(s), so Ψ≡0\Psi \equiv 0, which works only if Φ\Phi is constant. The leftover (1−γ)Φ(s′)(1-\gamma)\Phi(s') pays the agent for lingering where Φ\Phi is high. With γ=1\gamma = 1 the two forms coincide.

Question 3

Tabular Q-learning with potential-based shaping, started from Q0=0Q_0 = 0, behaves exactly like unshaped Q-learning on the same experience started from which values?

  1. Q0(s,a)=0Q_0(s,a) = 0
  2. Q0(s,a)=−Φ(s)Q_0(s,a) = -\Phi(s)
  3. Q0(s,a)=Φ(s)Q_0(s,a) = \Phi(s)
  4. Q0(s,a)=γΦ(s)Q_0(s,a) = \gamma\Phi(s)
Show the answer

C. Q0(s,a)=Φ(s)Q_0(s,a) = \Phi(s)

With Qunshaped=Qshaped+ΦQ_{\text{unshaped}} = Q_{\text{shaped}} + \Phi 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).

Question 4

An episodic task ends at a goal state gg, and you shape with a potential that has Φ(g)=50\Phi(g) = 50, computing F=γΦ(s′)−Φ(s)F = \gamma\Phi(s') - \Phi(s) on the final transition as on any other. What can go wrong?

  1. Nothing: potential-based shaping is always safe
  2. The telescoped shaping leaves γTΦ(g)−Φ(S0)\gamma^T\Phi(g) - \Phi(S_0), an extra reward for reaching gg that also grows the sooner gg is reached, so the optimal policy can change
  3. The agent can no longer reach the goal
  4. The values become unbounded
Show the answer

B. The telescoped shaping leaves γTΦ(g)−Φ(S0)\gamma^T\Phi(g) - \Phi(S_0), an extra reward for reaching gg that also grows the sooner gg is reached, so the optimal policy can change

The guarantee needs the leftover term γTΦ(ST)\gamma^T\Phi(S_T) to be the same for every trajectory, which in episodic tasks means Φ=0\Phi = 0 at terminal states. With Φ(g)=50\Phi(g) = 50 the shaping adds a terminal bonus of 50 at gg 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).