5.2.13 · D1Deep & Advanced RL

Foundations — Reward shaping and sparse rewards

3,433 words16 min readBack to topic

This page assumes nothing. Every letter, arrow, and Greek symbol the parent note used is rebuilt here from the ground up, in the order you need them. If a symbol appears in the parent, it is defined below before we lean on it. To keep the narrative anchored, each figure is labelled (Figure 1, Figure 2, …) and referred to by that label in the text.


The world we are in: states, actions, and steps

Before any reward, we need the stage on which everything happens. This comes from Markov Decision Processes (MDPs).

Figure 1 below draws these three symbols at once — a state , an action from the menu , and the resulting next state — plus the goal square we will reward.

Figure — Reward shaping and sparse rewards

The reward and its per-step name — the only feedback the agent gets


Adding up rewards over time: the sum symbol, the discount , and the return

One reward at one step is not enough — the agent cares about the whole future. To talk about "the whole future" we first need a compact way to say "add up a long list."

Why specifically? Because we apply the same shrink once per step, so after steps the reward is multiplied by . Repeated multiplication is exactly what powers mean.

Figure — Reward shaping and sparse rewards

Figure 2 shows the weights for : each future reward is multiplied by a smaller number, so the tail of an infinite sum fades toward zero.


Policies , the expectation , and value: and

To decide which policy is best, we score situations. These come from Value Functions and Bellman Equations.


The number line


The shaping toolkit: , , and

Now the pieces unique to reward shaping.

Figure — Reward shaping and sparse rewards

Figure 3 draws as a hill peaking at the goal; moving from to climbs a little, and the red arrow marks the potential difference that becomes the shaping bonus.


The algebraic WHY: telescoping to policy invariance

Here is the cancellation itself, so you see why shaping leaves the best policy alone — not just that it does.

Step 1 — Sum the shaping term along a trajectory. For a path , the total discounted shaping is Why: the shaped return is , so all the shaping's effect lives in this one sum.

Step 2 — Write out the first few terms. Why: seeing the pattern by hand is what reveals the cancellation.

Step 3 — Collect each . Look at : it appears as (from the first bracket) and as (from the second). Sum: . Same for : . Every interior potential is annihilated; only (which never gets a partner) survives. Why: this is the telescope folding shut.

Step 4 — Collect the result.

Step 5 — Read off policy invariance. Averaging gives . Since is the same constant for every action in state , Subtracting the same number from every bar in a bar chart does not change which bar is tallest — so the best action, and hence the optimal policy, is unchanged. ∎

(The parent note walks the same derivation in full; this sketch shows the load-bearing cancellation.)


How it all fits together

state s in set S and action a in set A

next state s prime

transition prob P of s prime given s a

reward R and per step r

discount gamma

return G equals sum of discounted r

terminal states and episodes

expectation averages the random return

value V and Q

policy pi and argmax

real numbers R

potential Phi over states

shaping F equals gamma Phi next minus Phi

shaped reward R prime equals R plus F

telescoping cancellation

policy unchanged

Read it top-down: the MDP gives you states (in ), actions (in ) and their random transitions; terminal states make the reward sum finite; rewards and discount build the return; expectation over that randomness defines value and the policy; potentials (real-valued) define the shaping term; and telescoping is what proves the shaped signal doesn't move the best policy.


Equipment checklist

Cover the right side and test yourself.

What is the difference between and ?
is one particular state; is the set of all states. means " is a member of ."
What is , and what does mean?
is the set of all available actions; means action is one of the allowed choices.
What does mean?
The state you land in after taking an action from (the prime = "next").
What is a terminal state, and why does it matter?
A state that ends the episode; after it every reward is , which makes the return a finite sum.
What does give you?
The probability of reaching when you take action in state — the world's randomness.
How does relate to ?
— the actual reward number produced on step by the reward function.
What makes a reward "sparse"?
Almost every is , giving signal only at rare goal/terminal states.
What does the symbol do?
Adds up , with stepping through .
What is the return ?
The discounted sum of all future rewards, .
Why do we multiply by ?
One shrink per step keeps the infinite sum finite and makes sooner rewards worth more.
When is safe, and when must ?
only in episodic tasks (finite episodes); continuing (never-ending) tasks require to stay finite.
What does mean and why is it needed?
The average over random transitions; the return is random, so value functions report its average.
What does measure?
Expected return starting from and following policy .
What does add over ?
It fixes the first action to , so we can compare individual moves.
What does return?
The action with the largest (an action, not a value).
What is ?
The set of all real numbers — every point on the continuous number line, positive, negative, and fractional.
What does say in words?
takes any state and returns one real number (its "potential").
What does "potential-based" shaping mean?
is a difference of a potential: .
What is the shaped reward ?
The original reward plus the shaping term: .
Why does telescoping give policy invariance?
The shaping sums to , so ; subtracting a constant over never changes .

Ready? With these symbols in hand, every line of the parent note — including the full telescoping proof and the Hindsight Experience Replay, Curriculum Learning, Exploration vs Exploitation and Reward Hacking and Specification Gaming extensions — reads without a single unexplained symbol.