Exercises — Reward shaping and sparse rewards
Before we start, one symbol reminder so nothing is used unearned:
The five exercise levels below form a staircase of rising difficulty. The figure makes that ladder explicit — read it left to right, and notice the accent-red top step (Mastery) is where the discount-factor subtleties bite hardest.

Level 1 — Recognition
Exercise 1.1
Which of these reward functions is sparse, and which is dense?
- (a) only when the agent reaches the goal cell, else .
- (b) every single step until the goal is reached.
Recall Solution 1.1
(a) is sparse — the reward is almost everywhere, non-zero only at the rare goal state. Along a 200-step episode you might see a single non-zero number. (b) is dense — every step delivers an informative , telling the agent "still not done, hurry up." There is a gradient of "warmer/colder" at every step. Why the distinction matters: in (a) the learned -values stay flat (all ) over visited states, so no action looks better than another until the goal is hit by luck. That is the whole reason we shape rewards.
Exercise 1.2
State the potential-based shaping (PBRS) formula from memory, including the role of every symbol.
Recall Solution 1.2
- is any function from states to real numbers you choose.
- is the (discounted) potential of the state you enter.
- is the potential of the state you leave. The bonus is "discounted new height minus old height." Because entering and leaving cancel, loops net to zero.
Level 2 — Application
Exercise 2.1
Goal at ; sparse reward on reaching it. Use and potential where is Manhattan distance to the goal. The agent moves from a cell with to a cell with . Compute the shaping bonus .
Recall Solution 2.1
What it looks like: the agent stepped toward the goal (height rose from to ), so it earns a positive nudge. It now has a gradient at this step even though the real reward is still here.
Exercise 2.2
Same setup, but the agent moves away: from to . Compute .
Recall Solution 2.2
Moving away is penalized. Notice the toward-bonus () and away-penalty () are not equal magnitudes — the discount makes them asymmetric. That asymmetry is exactly what preserves policy invariance in the discounted setting.
Exercise 2.3
A loop: agent goes with , , . Compute the total shaping reward over the loop.
Recall Solution 2.3
- Step : .
- Step : .
- Total . Why: the entry bonus is exactly undone by the exit penalty. Loops earn nothing ⇒ no incentive to spin in circles ⇒ no reward hacking. (This clean cancellation is the special case ; Exercise 5.1 shows what changes when .)
Level 3 — Analysis
Exercise 3.1
Prove that under PBRS the shaped return satisfies for the infinite trajectory . State where each simplification comes from.
Recall Solution 3.1
Step 1 — Write the shaped return. Step 2 — Split linearly. Step 3 — Telescope . The potential appears with coefficient (from the piece at ) and (from the piece at ). These annihilate for every . Only (from ) survives. Step 4 — Collect. , so . Assumption used: as (true since and is bounded), otherwise the telescope's tail wouldn't vanish.
Exercise 3.2
From Exercise 3.1 conclude , and use it to argue .
Recall Solution 3.2
Taking expectations of the return starting from gives : the constant carries through because is fixed. Now does not depend on — it is a constant offset across all actions from the same state. Subtracting the same constant from every option never changes which option is largest: So the greedy (and hence optimal) policy is identical — this is policy invariance.
Exercise 3.3
Take non-potential shaping for every step spent adjacent to a wall (independent of ). Show it can change the optimal policy by computing the accumulated over a 4-step wall-hugging loop with .
Recall Solution 3.3
Over 4 steps each earning : total shaping . Why this breaks the guarantee: is not of the form (it depends only on the current state's wall-adjacency, with no cancelling exit term). So the telescoping of Exercise 3.1 does not happen. A closed loop nets , not . The agent can farm this reward indefinitely and will prefer hugging walls to solving the task — a form of reward hacking.
Level 4 — Synthesis
Exercise 4.1
You have a rough heuristic estimating "how good" a state is. Design a PBRS scheme, and explain what the ideal choice of would be and why it makes learning almost trivial.
Recall Solution 4.1
Design: set and shape with . By PBRS this cannot change the optimal policy no matter how bad is — worst case it just fails to help. Ideal choice: , the true optimal value. Then the shaped optimal values become . For the best action this is , and every worse action is negative. So good and bad actions are cleanly separated from step one — the agent barely needs to learn. See Value Functions and Bellman Equations for why . Reality: we don't know , but any pointing roughly the right way (e.g. negative distance-to-goal) already turns a flat, uninformative landscape into a downhill slope toward the goal.
Exercise 4.2
You are training a robot arm to push a puck to a target, but you have no distance heuristic and the reward is only on success. PBRS needs a heuristic you lack. Propose two alternative strategies from the parent note, and say which failure mode each avoids.
Recall Solution 4.2
(1) Hindsight Experience Replay (HER): when the arm misses the target and instead moves the puck to , relabel the episode as if was the goal. A failure becomes a success ⇒ dense synthetic signal, and because we only relabel achieved outcomes there is no fake reward to hack. (2) Intrinsic curiosity / exploration bonus: add (or a prediction-error bonus) so the agent is paid to visit novel states. This needs no domain knowledge and drives exploration until the real is found. What each avoids: HER avoids the "no heuristic, no shaping" dead end; curiosity avoids getting stuck exploiting a flat, zero-reward region forever.
Level 5 — Mastery
Exercise 5.1
Design a full grid-world shaping. Goal at , , (Manhattan distance). Compute for a step from to . Then, for a two-step round trip that returns to the same state , use the trajectory telescope to compute the total discounted shaping contribution and show it equals the constant (plus the discounted end-of-trajectory potential) — not zero, because .
Recall Solution 5.1
Toward step (): Round trip back to the same state. Let the state at be with , and the state at be with . The trajectory is (so here ).
- (): .
- (): . Correct accounting — the discounted trajectory sum (the quantity the return actually uses): Closure via the telescope. The finite-horizon identity (Exercise 3.1, stopped at ) says this sum must equal . Check: Punchline: with the round trip does not net zero — it nets . That non-zero value is not free hackable reward: it is exactly the fixed telescope quantity , which depends only on the start and end states, never on the actions taken between them. Because it is a constant offset over actions, policy invariance still holds. The "loops = 0" mantra is only the special case (Exercise 2.3).
Exercise 5.2
Full-trajectory check. A 3-step episode visits with potentials and . Here is terminal, so by our convention . Compute the total discounted shaping contribution and confirm it equals .
Recall Solution 5.2
The exact finite-horizon identity is . Compute each :
- Discounted sum: . Check RHS: . ✔ Because by the terminal-potential convention, the leftover boundary term vanishes and we recover exactly — the pure constant offset that makes the policy invariant.
Recall One-line summary to self-quiz
PBRS bonus ::: shifts every by the same constant , so the best action never changes — while giving a per-step gradient that cures sparse reward.