5.1.1 · D3Reinforcement Learning Foundations

Worked examples — Agent, environment, state, action, reward

2,164 words10 min readBack to topic

This page is the "grind through every case" companion to the parent note. We take the two ideas that the parent introduced — the reward (a single number the world hands you at step ) and the return (the whole future added up) — and we hammer them against every situation the maths can throw at us.

Before we compute anything, let's recall the two objects in plain words, because every example below is just these two, dressed differently.

Recall What is

vs ? (open me first) ::: the reward at one step — a single scalar (one number) the environment emits right after action . ::: the return — the sum of this reward plus all future rewards, each future one shrunk by a factor per step into the future. ::: the discount factor, a dial between and that says "how much do I care about the future?"


The scenario matrix

Every return calculation you will ever meet is one of the cells below. The examples that follow are labelled with the cell they cover, and together they hit every cell.

Cell What makes it special Covered by
A. Finite horizon, plain sum episode ends at step , ignored (or ) Ex 1
B. Infinite horizon, constant reward same forever, geometric series Ex 2
C. Mixed signs some rewards , some (sacrifice now for later) Ex 3
D. Degenerate myopic agent, future ignored entirely Ex 4
E. Limiting far-sighted, sum blows toward its cap Ex 4
F. Recursive / bootstrapped compute from Ex 5
G. Stochastic reward → expectation reward is random, we need the average Ex 6
H. Real-world word problem translate a story into , then sum Ex 7
I. Exam twist reward AFTER episode end / self-consistency check Ex 8
Figure — Agent, environment, state, action, reward

The figure above is our mental picture for the whole page: a timeline of steps, each with a reward bar, and the discount curtain fading the far-away bars. Look at how the plum curve drops — a reward far in the future contributes almost nothing.


Example 1 — Finite horizon, plain sum (Cell A)

Forecast: guess before reading — three penalties then a payoff. Positive or negative total?

  1. Write the finite-horizon sum. With the return is just addition: Why this step? When an episode has a clear end, there is no infinite tail to worry about — the "sum forever" collapses to a plain finite sum.

  2. Plug the numbers. Why this step? Substitution — nothing subtle. Signs just add.

Verify: three steps cost units, the goal pays , net . Positive: the goal was worth the walk. Units: all rewards are the same abstract "score" unit, so adding is legal. ✓


Example 2 — Infinite horizon, constant reward (Cell B)

Forecast: the reward never stops. Will be infinite, or finite?

  1. Substitute the constant into the sum. Why this step? Because for all , we pull the constant out of the sum. This is why we need : the constant reward would give without the fading.

  2. Use the geometric-series fact. For , Why this step? This closed form is exactly why discounting was invented — it turns an infinite process into one clean number.

  3. Evaluate. Why this step? , and .

Verify: the cap from the parent note is . We hit the cap exactly, which is correct because every reward equals . ✓ (See the Bellman Equations deep dive for why constant reward saturates the bound.)


Example 3 — Mixed signs, sacrifice now for later (Cell C)

Forecast: immediate reward is negative. Is the return still positive?

  1. Write the discounted finite sum. Why this step? Mixed signs change nothing structurally — we still discount each future term by its own .

  2. Plug in. Why this step? ; the two zeros vanish.

  3. Compute.

Verify: the immediate reward alone () would tell a greedy agent to never sacrifice. But the return is , so the sacrifice pays. This is the exact "reward return" lesson from the parent's [!mistake] callout. ✓


Example 4 — The two extreme dials: and (Cells D & E)

Forecast: the big prize is one step away. Which "sees" it?

  1. Case (a), . The formula becomes Why this step? always, but when . Every future term is multiplied by zero — the agent is myopic, blind to anything past now.

  2. Case (b), . Why this step? near keeps almost the full value of the future reward — the agent is far-sighted.

Figure — Agent, environment, state, action, reward

Look at the figure: as slides from to nearly , the value of the -reward rises from to . The teal curve is ; the dashed line is the myopic value .

Verify: at we get exactly (the definition of myopic). At we get . As the return , its finite-horizon cap. The tuning of is what the parent called "the control knob." ✓ (Deeper choices of live in Markov Decision Process (MDP).)


Example 5 — Recursive / bootstrapped form (Cell F)

Forecast: can you get from with one multiply and one add?

  1. Use the recursion. From the parent note, Why this step? The infinite tail starting at step is by definition; discounting it by one folds it into . This is bootstrapping — reusing a computed value instead of re-summing.

  2. Plug in.

Verify: unroll one more layer to sanity-check the logic — , and must equal for consistency. It does by assumption, so . This recursion is the literal seed of the Bellman Equations. ✓


Example 6 — Stochastic reward → expectation (Cell G)

Forecast: the reward is random. What single number summarises it?

  1. The return of one step is just its reward. Since the episode ends immediately, . Why this step? No future terms exist, so no discounting is needed.

  2. Take the expectation because is random. Expectation = "probability-weighted average of outcomes": Why this step? When rewards are stochastic, the agent maximises the average return, not any single lucky draw. This is exactly the setting of Multi-Armed Bandits.

  3. Compute.

Verify: the answer must lie between the smallest () and largest () possible reward — and . ✓ It is closer to because the -outcome is more likely (). The tension between "try the risky " and "the average is only " is Exploration vs Exploitation.


Example 7 — Real-world word problem (Cell H)

Forecast: all rewards are negative (penalties). Is negative? By how much?

  1. Compute . Substitute : Why this step? The squared-error term punishes being off target; the tiny term punishes energy use.

  2. Compute . Substitute : Why this step? Closer to target ( off), so a smaller penalty — good, the controller is working.

  3. Discount and sum. Why this step? Standard two-term discounted return.

Verify: both rewards are negative, so must be negative — and . ✓ The magnitude is dominated by the first step's large error, exactly as the reward design intended: penalise deviation heavily, energy lightly. (Continuous actions like the heater power sit in Model-Free vs Model-Based RL control tasks.)


Example 8 — Exam twist: reward after the horizon, and self-consistency (Cell I)

Forecast: a reward listed after the episode ends is a trap. How much does it wrongly add?

  1. (a) Respect the horizon . Only count: Why this step? Once the episode ends, no further rewards exist — the sum stops at .

  2. (b) The student's mistaken sum adds the phantom at : Why this step? We just append the extra discounted term to see the size of the error.

  3. The inflation is . Why this step? Subtracting isolates exactly the phantom term's contribution.

Verify: the phantom term is , matching the inflation exactly. ✓ Lesson: always stop the sum at the true episode end. Partial-information versions of "when does the episode end?" are studied in Partial Observability (POMDP). Policy quality across such returns is the subject of Policy and Value Functions.


Recall Rapid self-test

Constant reward , , infinite horizon — ? ::: . makes the agent... ::: myopic — it only sees the immediate reward. Given , then ? ::: . A reward listed after the episode ends should be... ::: ignored — the sum stops at the horizon .