TL;DR
Reward shaping changes the reward an agent receives so that useful progress earns small rewards along the way, not only at the goal. It is a change to the reward in a reinforcement learning task. It solves the problem of an agent that gets no signal for thousands of steps and so learns very slowly. Potential-based shaping is a safe form: the extra reward is a difference of a score between two consecutive states, so the best behaviour stays the same.
Builds on: Markov Decision Process, Value Function · Leads to: Sparse Reward Problem, Goal-Conditioned Reinforcement Learning
Where it's used
Robot manipulation. A robot arm that is only rewarded when its gripper closes on a block may spend a long time moving at random before it ever succeeds. Adding a small reward for each centimetre the gripper closes on the block gives it a direction to improve in from the first episode.
Game playing. In games where the score only changes at the end of a level, agents are often given small rewards for the things that predict winning, such as collected items or cleared lines. Designers use this to speed up training, though a careless hint can teach the agent to chase the hint instead of winning.
Training language and tool agents. Multi-step agents that run code or browse may get a reward only when the final answer is checked. Intermediate checks, such as a test that passes or a step that is verified, act as shaping rewards so the agent can tell good partial work from bad.
How it works
Take a tiny world: a corridor with four cells, numbered 0, 1, 2 and 3. The agent starts at 0 and the goal is cell 3. The agent gets reward 1 only when it reaches cell 3, and 0 otherwise. Use a discount factor γ = 0.9.
Under this sparse reward, an agent that moves at random has to stumble onto cell 3 before it learns anything. Shaping gives it a hint: a score for each cell that says how far it is from the goal. Call that score Φ(s), the potential of cell s. Let Φ(s) = −(distance to goal), so the potentials are −3, −2, −1 and 0 for cells 0 to 3.
- Define the potential. Φ(0) = −3, Φ(1) = −2, Φ(2) = −1, Φ(3) = 0. Check: each step closer to the goal raises Φ by 1.
- Compute the shaping reward for a move. When the agent moves from s to s′, add F(s, s′) = γΦ(s′) − Φ(s) to the environment reward. Moving 0 → 1 gives 0.9 × (−2) − (−3) = −1.8 + 3 = 1.2. Check: the move is toward the goal, and the bonus is positive.
- Penalise a move away. Moving 1 → 0 gives 0.9 × (−3) − (−2) = −2.7 + 2 = −0.7. Check: a move away from the goal earns a negative bonus, so it looks worse than standing still.
- Add the total along a path. Walk 0 → 1 → 2 → 3. The three bonuses are 1.2, 1.1 and 1.0. Discount them: 1.2 + 0.9 × 1.1 + 0.81 × 1.0 = 3.0. Check: the total depends only on the start and the end, since the potential of cell 0 is −3 and the potential of cell 3 is 0.
- Learn from the shaped reward. The agent now gets a useful signal on every move. Its value estimates, updated by temporal-difference learning, rise toward the goal from the first episode, and it learns to move right long before it sees the sparse reward alone.
The shaping in step 2 has one important property. The bonuses along any path telescope into a term that depends only on where the path starts and ends. That is why the best path does not change, and it is the property that the policy comparison in the math relies on.
- Φ(s) = s − 3 (potential)
The math
Given a reward r(s, a, s′) and a function Φ over states, the shaped reward is
where γ is the discount factor, a number between 0 and 1 that makes rewards further in the future count less. Φ is called the potential.
Step 2 in symbols
For the move from cell 0 to cell 1, with Φ(0) = −3, Φ(1) = −2 and γ = 0.9:
Step 4 in symbols
Let a path visit states s₀, s₁, …, s_T. The discounted sum of the bonuses is
Each term cancels the next one except at the two ends. For the path 0 → 3 this gives 0.9³ × 0 − (−3) = 3, which matches the 3.0 found in step 4.
Step 5 in symbols
The value of a state under the shaped reward differs from the value under the original reward by the potential. Summing the telescoping bonus over an infinite discounted path, with a bounded potential so that , gives
This is the reason the best policy is unchanged, and it holds for every policy, since the shift does not depend on the policy. Adding a constant to every path's return that depends only on the start state does not change which action is best at any state.
Worked examples
Cells 0 to 3 as above, Φ(s) = s − 3, γ = 0.9.
- Compute Φ(2) = 2 − 3 = −1.
- Compute Φ(3) = 3 − 3 = 0.
- Compute the bonus for the move 2 → 3: F(2, 3) = 0.9 × 0 − (−1) = 1.
- Compute the bonus for the move 3 → 2: F(3, 2) = 0.9 × (−1) − 0 = −0.9.
Interpretation: stepping onto the goal earns a bonus of 1, and stepping off it costs 0.9. The agent is pushed toward cell 3 and away from leaving it.
Use the same corridor and the path 0 → 1 → 2 → 3.
- Bonus for 0 → 1: F = 0.9 × (−2) + 3 = 1.2.
- Bonus for 1 → 2: F = 0.9 × (−1) + 2 = 1.1.
- Bonus for 2 → 3: F = 0.9 × 0 + 1 = 1.0.
- Discounted total: 1.2 + 0.9 × 1.1 + 0.9² × 1.0 = 1.2 + 0.99 + 0.81 = 3.0.
- Check with the formula γ³Φ(3) − Φ(0) = 0.729 × 0 − (−3) = 3.
Interpretation: the step-by-step bonuses add to exactly the endpoint difference, so no matter how the agent gets from 0 to 3 along this path, the shaping adds the same total. The shaping changes what the agent sees, not what it should prefer.
A gripper must reach a target. The environment gives reward 1 on success and 0 otherwise. Let d be the distance in centimetres from the gripper to the target, and set Φ(s) = −d / 100 so the potential is in the same scale as the reward. Use γ = 0.99.
- The gripper moves from d = 40 cm to d = 35 cm. Φ(40) = −0.40 and Φ(35) = −0.35.
- Compute γΦ(35) = 0.99 × (−0.35) = −0.3465.
- Compute the bonus F = −0.3465 − (−0.40) = 0.0535.
- The total reward for this step is the environment reward 0 plus the bonus, so the agent receives 0.0535.
- The success reward of 1 is still much larger than any single bonus, so the agent still prefers success.
Interpretation: each centimetre of progress earns a small, positive signal. The bonus is scaled so it guides the agent without overwhelming the real goal.
Try it: the gripper now moves back from 35 cm to 40 cm. Compute the bonus F for this move using the same potential and γ = 0.99.
Answer: Φ(35) = −0.35 and Φ(40) = −0.40, so F = 0.99 × (−0.40) − (−0.35) = −0.396 + 0.35 = −0.046. The move away from the target earns a negative bonus of about −0.046.
Common mistakes and tradeoffs
Mistake 1: "Any extra reward for progress helps." It is tempting to think that rewarding every step that looks good will speed up learning. In fact an arbitrary bonus can change the best behaviour. A classic failure is an agent that earns a bonus for moving near a checkpoint and then circles it forever, collecting the bonus instead of finishing the task. Potential-based shaping avoids this because the bonuses telescope: the shaped return of any path equals its original return minus the potential of the start state, however many times the agent passes the checkpoint. Circling earns no extra reward.
Mistake 2: "Shaping is the same as designing a good reward." It is tempting to think a hand-written bonus is always a safe improvement. In fact a bonus is only safe when it has the potential form. A bonus that is not a difference of a state function can make the agent optimise the hint and ignore the goal.
Mistake 3: "A bigger bonus learns faster." It is tempting to think that a large bonus gives a stronger push. In fact a large bonus can dominate the real reward, so the agent learns the hint's structure rather than the task. The bonus should be small compared with the true reward, and its scale should be checked against exact solutions on small problems where they are available.
When to use it. Use shaping when the environment gives feedback only at rare moments, as in sparse reward problems, and when you can name a progress measure such as distance to the goal. Use the potential-based form when you need the optimal behaviour to stay the same. Its value estimates are shifted by the potential, so compare actions, not raw values, when checking it.
When not to use it. When no sensible progress measure exists, shaping is guesswork and may mislead. When the environment's reward is already dense, the extra signal adds little. When the reward itself is what you want to learn from human preferences, a learned Reward Model may be more reliable than a hand-written hint.
Alternatives. Curriculum learning starts the agent on easier versions of the task and makes the task harder over time. Relabelling goals, as in Goal-Conditioned Reinforcement Learning, turns a failed attempt into a success for the goal it did reach. Intrinsic rewards for exploration give bonuses for visiting new states, which is a different goal from shaping toward the task.
History
For decades, reinforcement learning researchers and animal trainers relied on informal shaping: rewarding partial steps toward the desired behaviour. Robot learners in the 1990s used hand-designed bonuses widely, and many of them failed in the ways described above, which left the field unsure which bonuses were safe.
In 1999 Andrew Ng, Daishi Harada and Stuart Russell showed that when the bonus has the potential form γΦ(s′) − Φ(s), the optimal policy is unchanged. That result gave researchers a safe recipe, and it is the form used in most modern shaping. The idea caught on because it is simple to implement, needs no change to the learning algorithm, and comes with a guarantee.
Recap
- Shaping adds extra reward to a task that gives feedback only at rare moments, so the agent has a signal from early on.
- Potential-based shaping uses a bonus γΦ(s′) − Φ(s), which is a difference of a state score and so cancels along any path.
- Because the bonuses telescope, the optimal behaviour does not change, and the value of each state shifts by −Φ(s).
- Arbitrary bonuses can create loops and mislead the agent, so only the potential form is safe.
- Read next: Sparse Reward Problem explains why shaping is needed, and Goal-Conditioned Reinforcement Learning shows a different way to get signal from failed attempts.
Check yourself
QuestionRecall: what property makes potential-based shaping safe, and what would happen with an arbitrary bonus?Show answer
The bonus is a difference γΦ(s′) − Φ(s) of a state score, so along any path the bonuses telescope to a term that depends only on the endpoints. The best behaviour is unchanged. An arbitrary bonus is not of this form, so it can reward loops or change which behaviour is best.
QuestionCompute: a state potential is Φ(4) = 2 and Φ(5) = 3, with γ = 0.9. What is the shaping bonus for the move from state 4 to state 5?Show answer
F(4, 5) = γΦ(5) − Φ(4) = 0.9 × 3 − 2 = 2.7 − 2 = 0.7.
QuestionWhy: a designer adds a bonus of +1 each time the agent moves within 1 cell of a checkpoint, and the agent learns to circle the checkpoint forever. Why did this happen, and how would potential-based shaping prevent it?Show answer
The bonus is paid again every time the agent passes near the checkpoint, so circling earns it without end. This is not a difference of a state function, so the bonuses do not telescope. Potential-based shaping gives bonuses whose sum depends only on the potentials at the start and the far end of the path, so repeated passes add nothing beyond the start term that every policy shares.
QuestionApplied: you are training a robot to open a drawer, and the only reward is given when the drawer is fully open. You can measure how far the handle is from its goal position. Describe the shaping you would use and the check you would run before trusting it.Show answer
Use a potential that decreases with the handle's distance from the goal, scaled so it is small compared with the success reward. Shape with the potential form γΦ(s′) − Φ(s). Before trusting it, check that the agent still opens the drawer with the sparse reward alone in a simpler setting, and confirm that the learned behaviour does not get stuck near the handle when the drawer is still shut.