Intuition The one core idea
An agent learning by trial and error only improves when it receives a signal telling it "that was good" or "that was bad." Reward shaping is the art of adding extra hints to a signal that is otherwise almost always silent — done in a special way (which this page builds up piece by piece) so the hints speed up learning without changing which behaviour is actually best .
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.
Before any reward, we need the stage on which everything happens. This comes from Markov Decision Processes (MDPs) .
s and the state set S
A state s is a complete snapshot of the situation the agent is in — "where I am and what everything looks like right now." In a maze it is the agent's grid cell.
The symbol S (a fancy "S") means the set of all possible states — the whole maze, every square at once. Writing s ∈ S reads "s is one of the states in the set S " (the symbol ∈ means "is a member of").
Picture: a single square in the maze grid, with the little agent standing on it; S is the whole grid.
Why the topic needs it: rewards depend on where you are , so we must be able to name each situation.
a and the action set A
An action a is a choice the agent makes from a state — "move up," "move left."
The symbol A (a fancy "A") is the set of all actions available to the agent. Writing a ∈ A reads "a is one of the allowed actions." In a grid maze A = { up , down , left , right } .
Picture: the four arrows leaving a square, one per direction; A is that little menu of arrows.
Why: the whole point of learning is to pick good actions from A , so shaping is judged by whether it keeps the best actions best.
s ′ (read "s-prime")
After taking action a in state s , the agent lands in a new state written s ′ (also a member of S ). The tick mark ′ just means "the one that comes after."
Picture: the square at the head of the action-arrow.
Why: shaping formulas compare where you were (s ) with where you ended up (s ′ ).
Figure 1 below draws these three symbols at once — a state s , an action a from the menu A , and the resulting next state s ′ — plus the goal square we will reward.
Definition Terminal state (and episodes)
Some states end the run — reaching the goal, falling in a pit, the game finishing. These are called terminal states . Once the agent enters one, the episode stops and (conceptually) all future rewards are 0 .
Picture: the goal square with a little "STOP / end of episode" flag on it.
Why: whether a task has terminal states decides how we add up rewards — see the episodic-vs-continuing box below. This is what makes the infinite-looking sum in G t well-behaved.
Definition Transition dynamics
P ( s ′ ∣ s , a )
The world is not perfectly predictable — the same action can lead to different next states. P ( s ′ ∣ s , a ) is the probability of landing in s ′ when you take action a in state s . The bar ∣ reads "given."
Picture: several faint arrows fanning out from a square, each labelled with a chance like 0.8 and 0.2 .
Why the topic needs it: this randomness is the reason values are written as averages (expectations) later — the agent cannot be sure which s ′ it will reach.
Definition Reward function
R ( s , a , s ′ )
A reward is a single number the environment hands the agent after each step. Think of it as points. R ( s , a , s ′ ) reads: "the points you get for being in s , doing a , and landing in s ′ ."
Picture: a little "+1" popping up over the goal square, and "0" popping up everywhere else.
Why: this number is the entire teaching signal. Learning means chasing more of it.
Definition The reward at time step
t , written r t
As the agent walks a path s 0 , s 1 , s 2 , … , it collects one reward number per step. We give the reward received on step t its own short name r t . It is just the reward function evaluated on that step's transition:
r t = R ( s t , a t , s t + 1 ) .
Picture: a ticket stamped at each step of the path, showing the points earned there.
Why: R ( s , a , s ′ ) is the general rule ; r t is the actual number that came out on step t . We need the short name r t to add up rewards along a real path.
Intuition Why "sparse" is a disaster
A sparse reward is one where almost every r t is 0 . If you only see "0, 0, 0, 0, …" for thousands of steps, you have no idea whether any move was smart. There is no "warmer / colder" — just silence until you accidentally hit the goal.
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."
Definition The sum symbol
∑
∑ k ≥ 0 x k means "add up x 0 + x 1 + x 2 + ⋯ ." The letter underneath (k ) is a counter that steps through 0 , 1 , 2 , … , and x k is whatever we are adding for each value of the counter.
Picture: a row of boxes being poured, one after another, into a single bucket.
Why: the return below is a sum of many rewards; without this shorthand we'd have to write "⋯ " forever.
Definition Discount factor
γ (Greek "gamma", 0 ≤ γ ≤ 1 )
γ is a number between 0 and 1 (often 0.99 ) that makes future rewards worth a little less than immediate ones .
Picture: a reward far in the future, faded and shrunk; the further away, the fainter.
Why this tool and not just adding? Without shrinking, an infinite future would sum to infinity and nothing could be compared. Multiplying by γ each step keeps the total finite AND encodes "sooner is better."
Why γ k specifically? Because we apply the same shrink γ once per step , so after k steps the reward is multiplied by γ ⋅ γ ⋯ γ = γ k . Repeated multiplication is exactly what powers mean.
G t
The return from time t is the discounted total of all future rewards. Using the sum symbol and the per-step reward r t + k (the reward k steps after time t , defined above):
G t = ∑ k ≥ 0 γ k r t + k .
Picture: a stack of fading reward-numbers, each shorter than the last, summed into one total (Figure 2 ).
Why: the agent's goal is literally to maximize G t . Everything — values, policies, shaping — is measured against this number.
Common mistake "The sum runs to infinity, so with
γ = 1 it must diverge."
Why it feels right: ∑ k ≥ 0 r t + k with equal weights on an endless walk really can blow up. When it's fine: two settings tame it.
Episodic tasks have terminal states, so an episode ends after a finite number of steps; every r after termination is 0 , making the sum a finite sum. Here γ = 1 is safe.
Continuing tasks never terminate, so we must take γ < 1 strictly; then the geometric shrinkage keeps G t finite even over an infinite horizon.
Rule of thumb: allow γ = 1 only in episodic tasks; in continuing tasks insist on γ < 1 . This is why the definition writes 0 ≤ γ ≤ 1 but the loop example below can safely use γ = 1 — that example is a short, finite episode.
Figure 2 shows the weights γ k for γ = 0.8 : each future reward is multiplied by a smaller number, so the tail of an infinite sum fades toward zero.
π (Greek "pi")
A policy π is the agent's strategy — a rule that says which action (from A ) to take in each state. "In this square, go left."
Picture: an arrow drawn on every square of the maze, showing where you'd go from there.
Why: learning = finding the best policy. Shaping's promise ("policy unchanged") is a promise about π .
Definition The expectation operator
E [ ⋅ ]
Because the transitions P ( s ′ ∣ s , a ) are random, the return G t is not a fixed number — different rolls of the dice give different totals. E [ X ] means the average value of X over all that randomness, weighted by how likely each outcome is.
Picture: running the same episode thousands of times and averaging the returns into one representative number.
Why the topic needs it: it turns a random return into a single "how good, on average" score, which is exactly what a value function reports.
To decide which policy is best, we score situations. These come from Value Functions and Bellman Equations .
V π ( s )
V π ( s ) is the expected return if you start in state s and then follow policy π :
V π ( s ) = E [ G t ∣ s t = s , follow π ] .
"How good is it to be here, playing this way?" The average is taken over the random transitions P ( s ′ ∣ s , a ) .
Picture: a heat-map colouring the maze — bright near the goal, dark far away.
Why: shaping's key result is stated as an equation about V .
Q π ( s , a )
Q π ( s , a ) is the expected return if you start in s , take action a now , then follow π :
Q π ( s , a ) = E [ G t ∣ s t = s , a t = a , then π ] .
"How good is this move from here?"
Picture: each action-arrow out of a square gets its own number.
Why: the best action is the one with the biggest Q . Shaping is "safe" precisely when it keeps the ranking of Q -values intact.
arg max operator
arg max a Q ( s , a ) means "which action a gives the largest Q ?" It returns the action , not the value.
Picture: pointing at the tallest bar in a bar chart of the actions.
Why: the greedy policy is "always pick arg max a Q ." Policy invariance is the statement that arg max doesn't move when we shape.
Definition The real numbers
R
R (a bold, doubled "R") is the set of all real numbers — every point on a continuous number line: − 3 , 0 , 0.5 , 2 7 , π , and everything in between. Not just whole numbers, but all the fractions and in-betweens too.
Picture: an unbroken horizontal line stretching left (negatives) and right (positives) with 0 in the middle.
Why the topic needs it: the potential we introduce next assigns a plain number to each state, and R is the precise name for "any plain number." That is why its type is written Φ : S → R — "state in, real number out."
Now the pieces unique to reward shaping.
Definition Potential function
Φ ( s ) (Greek "Phi")
Φ assigns a single real number to each state — think "height above sea level" or "closeness to goal." The notation Φ : S → R reads "Φ takes any state (from the state set S ) and returns a real number (from R )."
Picture: a landscape draped over the maze — a hill whose peak is at the goal (Figure 3 ).
Why: potential-based shaping (defined right below) builds its shaping term out of differences in Φ , and that structure is what makes loops harmless.
Figure 3 draws Φ as a hill peaking at the goal; moving from s to s ′ climbs a little, and the red arrow marks the potential difference that becomes the shaping bonus.
F ( s , a , s ′ ) — "potential-based"
F is an extra reward you invent to give the agent hints ("you're getting warmer"). The special choice called potential-based shaping — the one that keeps the best policy unchanged — is built entirely from the potential Φ :
F ( s , a , s ′ ) = γ Φ ( s ′ ) − Φ ( s ) .
In words: you gain the discounted potential of where you arrive, γ Φ ( s ′ ) , and pay back the potential of where you left, Φ ( s ) . (This is what "potential-based" means: F is a difference of a potential.)
Picture: a faint green glow that grows as you climb toward the goal-peak of the Φ landscape.
Why: F is the whole idea of shaping — it breaks the silence of a sparse reward, and this exact form is what makes loops cancel to zero.
R ′ ( s , a , s ′ )
The prime ′ marks the modified reward: it is the real reward plus the shaping term,
R ′ ( s , a , s ′ ) = R ( s , a , s ′ ) + F ( s , a , s ′ ) .
Same environment, louder signal.
Picture: the "+1 at goal" reward plus the green nudges layered on every step.
Intuition Why "difference of potential" kills loops
If entering a state gives you + γ Φ ( s ′ ) worth of bonus and leaving it later costs you − Φ ( s ′ ) , then walking in and back out cancels — like climbing a hill and coming back down: net altitude change zero. That is the seed of the "policy is unchanged" proof, which we now sketch.
Here is the cancellation itself, so you see why shaping leaves the best policy alone — not just that it does.
A telescoping sum is one where each term partly cancels the next, so a long chain collapses to just its endpoints — like a folding telescope shrinking to a stub. Example: ( + 3 − 3 ) + ( + 5 − 5 ) = 0 ; the middle bits annihilate, only the ends survive.
Step 1 — Sum the shaping term along a trajectory. For a path s 0 , s 1 , s 2 , … , the total discounted shaping is
T = ∑ t ≥ 0 γ t ( γ Φ ( s t + 1 ) − Φ ( s t ) ) .
Why: the shaped return is G t ′ = G t + T , so all the shaping's effect lives in this one sum.
Step 2 — Write out the first few terms.
T = ( γ Φ ( s 1 ) − Φ ( s 0 ) ) + γ ( γ Φ ( s 2 ) − Φ ( s 1 ) ) + γ 2 ( γ Φ ( s 3 ) − Φ ( s 2 ) ) + ⋯
Why: seeing the pattern by hand is what reveals the cancellation.
Step 3 — Collect each Φ ( s t ) . Look at Φ ( s 1 ) : it appears as + γ Φ ( s 1 ) (from the first bracket) and as − γ Φ ( s 1 ) (from the second). Sum: 0 . Same for Φ ( s 2 ) : + γ 2 Φ ( s 2 ) − γ 2 Φ ( s 2 ) = 0 . Every interior potential is annihilated; only − Φ ( s 0 ) (which never gets a partner) survives.
Why: this is the telescope folding shut.
Step 4 — Collect the result.
T = − Φ ( s 0 ) , so G 0 ′ = G 0 − Φ ( s 0 ) .
Step 5 — Read off policy invariance. Averaging gives Q ′ π ( s , a ) = Q π ( s , a ) − Φ ( s ) . Since Φ ( s ) is the same constant for every action a in state s ,
arg max a Q ′ ( s , a ) = arg max a ( Q ( s , a ) − Φ ( s ) ) = arg max a Q ( s , a ) .
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.)
state s in set S and action a in set A
transition prob P of s prime given s a
return G equals sum of discounted r
terminal states and episodes
expectation averages the random return
potential Phi over states
shaping F equals gamma Phi next minus Phi
shaped reward R prime equals R plus F
Read it top-down: the MDP gives you states (in S ), actions (in A ) 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.
Cover the right side and test yourself.
What is the difference between s and S ? s is one particular state; S is the set of all states. s ∈ S means "s is a member of S ."
What is A , and what does a ∈ A mean? A is the set of all available actions; a ∈ A means action a is one of the allowed choices.
What does s ′ mean? The state you land in after taking an action from s (the prime = "next").
What is a terminal state, and why does it matter? A state that ends the episode; after it every reward is 0 , which makes the return a finite sum.
What does P ( s ′ ∣ s , a ) give you? The probability of reaching s ′ when you take action a in state s — the world's randomness.
How does r t relate to R ( s , a , s ′ ) ? r t = R ( s t , a t , s t + 1 ) — the actual reward number produced on step t by the reward function.
What makes a reward "sparse"? Almost every r t is 0 , giving signal only at rare goal/terminal states.
What does the symbol ∑ k ≥ 0 x k do? Adds up x 0 + x 1 + x 2 + ⋯ , with k stepping through 0 , 1 , 2 , … .
What is the return G t ? The discounted sum of all future rewards, ∑ k ≥ 0 γ k r t + k .
Why do we multiply by γ k ? One shrink γ per step keeps the infinite sum finite and makes sooner rewards worth more.
When is γ = 1 safe, and when must γ < 1 ? γ = 1 only in episodic tasks (finite episodes); continuing (never-ending) tasks require γ < 1 to stay finite.
What does E [ ⋅ ] mean and why is it needed? The average over random transitions; the return is random, so value functions report its average.
What does V π ( s ) measure? Expected return starting from s and following policy π .
What does Q π ( s , a ) add over V ? It fixes the first action to a , so we can compare individual moves.
What does arg max a Q ( s , a ) return? The action with the largest Q (an action, not a value).
What is R ? The set of all real numbers — every point on the continuous number line, positive, negative, and fractional.
What does Φ : S → R say in words? Φ takes any state and returns one real number (its "potential").
What does "potential-based" shaping mean? F is a difference of a potential: F ( s , a , s ′ ) = γ Φ ( s ′ ) − Φ ( s ) .
What is the shaped reward R ′ ? The original reward plus the shaping term: R ′ = R + F .
Why does telescoping give policy invariance? The shaping sums to − Φ ( s 0 ) , so Q ′ ( s , a ) = Q ( s , a ) − Φ ( s ) ; subtracting a constant over a never changes arg max a .
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.