5.1.4 · D3Reinforcement Learning Foundations

Worked examples — State-value and action-value functions

3,035 words14 min readBack to topic

This page is a drill. The parent note built the tools and . Here we throw every kind of situation at them — every sign of reward, every degenerate policy, the two limits and , a word problem, and an exam trap. If you can do all ten, no gridworld can surprise you.

Before we start, one line of vocabulary insurance so nothing here is assumed:

The two engines we will crank over and over. Notice the superscript is never optional: it names which policy's values we mean, and every and on this page carries it.


The scenario matrix

Every value-function problem is really a combination of a few knobs. Here is the full board. Each of the ten examples below is stamped with the cell it lives in.

# Cell class The twist it tests Example
C1 Deterministic transition, negative reward value goes down as goal recedes Ex 1
C2 Stochastic transition, mixed-sign rewards probability-weighting outcomes Ex 2
C3 Stochastic policy, known as a weighted average Ex 3
C4 (myopic limit) future collapses to zero Ex 4
C5 (far-sighted limit) infinite sum must still converge Ex 5
C6 Terminal / degenerate state , the recursion's floor Ex 6
C7 Self-loop (agent can get stuck) geometric series inside one state Ex 7
C8 round trip consistency check both directions Ex 8
C9 Real-world word problem translating English into Ex 9
C10 Exam twist: "best " why Ex 10

[!example] Example 1 — Cell C1: deterministic path, negative rewards

A grid. We label each cell by using zero-based indexing: the top-left cell is , rows increase downward, columns increase rightward. So the top-right corner is and the exact centre is . The goal is the top-right corner ; each step costs ; reaching the goal is terminal. . The policy is deterministic: "go right if you can, else go up." We want (the centre), given it is two moves from the goal.

Forecast: guess — will be positive or negative, and roughly how big?

The figure below draws this grid. The green cell (top-right) is the goal with ; the blue cell is our start ; the orange cell is the intermediate . The two red arrows are the forced policy path, each labelled with its cost.

The key insight the figure carries: the red arrows point toward the goal — that is the direction the agent moves — but the numbers printed in the cells get filled in the opposite direction, starting from the green cell and flowing back to the blue one. Value and motion run in opposite directions. That is the single most important habit in all of dynamic programming: you can only compute a state's value once you already know the value of everything downstream of it, so you always start at the terminal cell (where value is known to be ) and work backward against the arrows.

Figure — State-value and action-value functions
  1. From the policy moves right to (the blue-to-orange red arrow), collecting . Why this step? under a deterministic policy is just the value of the single path it forces — no averaging needed, so we walk the path.
  2. From the policy moves up to the goal (the orange-to-green red arrow), collecting ; the goal is terminal so . Why this step? Terminal states have no future, so their value is the recursion's floor (see C6).
  3. Back-substitute from the goal outward (exactly the backward flow the figure highlights): Why this step? The Bellman equation propagates value backward from where reward is known.

Verify: two steps, each , second one discounted once: . Sign is negative (every move is a cost) and magnitude (discount shrinks the far cost). Matches the forecast if you guessed "a bit less than ." ✓


[!example] Example 2 — Cell C2: risky vs safe action, mixed signs

State , two actions. (undiscounted).

  • (safe): , , with .
  • (risky): , , ; , , .

Forecast: which action has the higher ? The safe one feels comfortable — trust that feeling?

  1. Safe action collapses to one term (probability 1): Why this step? When the sum has a single surviving term.
  2. Risky action weights each branch by its : Why this step? Expectation = probability-weighted sum of outcomes; the reward is real but rare, so it barely dents the total.

Verify: . The risky action wins on expectation even though it can go badly. Sanity: sits between the good branch total () and the bad branch total (), closer to because that branch is more likely. ✓ This is exactly the seed of policy improvement.


[!example] Example 3 — Cell C3: from under a stochastic policy

State with , , . Policy .

Forecast: is best at . Will equal ? Above it? Below it?

  1. Apply : Why this step? is what you actually get by following , and is a lottery over actions — so average the s with the policy's odds.
  2. Add up: .

Verify: the weights sum to (a valid distribution), and lies strictly between the worst () and the best (). A weighted average must fall inside that range — it does. ✓


[!example] Example 4 — Cell C4: the myopic limit

Same setup as Example 2, but now . Recompute both s.

Forecast: with the future term vanishes. What survives?

  1. Plug into the formula — the term dies: Why this step? literally means "ignore everything after the next reward."
  2. Risky action, immediate rewards only:

Verify: now the risky action still wins () but the margin shrank because the landing values (10, 15, 2) no longer count. A myopic agent judges by immediate reward alone. Sanity: is exactly the expected immediate reward . ✓


[!example] Example 5 — Cell C5: the far-sighted limit on an infinite reward stream

An agent sits in a chain and receives a constant reward forever (no terminal state). The return is . Compute for , and describe .

Forecast: an infinite sum of s — does it blow up, or settle to a finite number?

  1. Recognise a geometric series: Why this step? This is why we discount: makes the tail shrink fast enough that the infinite sum converges to a finite value.
  2. Evaluate at : .
  3. The limit : . A never-ending stream of positive reward has infinite value once we stop discounting. Why this step? It shows the boundary case: strictly less than is what keeps infinite-horizon values finite.

Verify: . Cross-check the first few terms: climbs toward and never passes it. ✓


[!example] Example 6 — Cell C6: terminal / degenerate state

The goal cell in Example 1. No actions lead anywhere; the episode ends.

Forecast: what is ?

  1. A terminal state has no future rewards, so the return from it is the empty sum: Why this step? Every recursion needs a base case. This is it — without it, the Bellman back-substitution in Example 1 would never bottom out.

Verify: substitute back into the goal's Bellman line: . Consistent. This zero is what made Example 1's come out clean. ✓


[!example] Example 7 — Cell C7: a self-loop (the agent can get stuck)

State has one action. With probability it stays in (self-loop, reward ); with probability it exits to a terminal state (, then value ). . Find .

Forecast: the loop keeps paying per stay. Since , will this diverge — or does the escape probability save us?

The figure draws the two-node world: the blue circle is (with its printed answer ), the curved orange arrow is the self-loop , and the straight red arrow is the escape into the green terminal node.

The key insight the figure carries: notice the orange arrow leaves and comes right back to . That returning arrow is the visual reason shows up on both sides of its own equation below — you cannot "walk the path" like in Example 1 because the path folds back on itself. The escape arrow (red, weight ) is the only way out, so intuitively the agent stays for about visits on average before leaving; the loop is finite precisely because that red exit always eventually fires. A picture with no red arrow would be a genuinely infinite loop with (for ) infinite value.

Figure — State-value and action-value functions
  1. Write the Bellman equation with on both sides (the orange self-loop term keeps on the right): Why this step? A self-loop puts inside its own equation — that is legal and we solve for it algebraically.
  2. Expand and collect : Why this step? We gather every onto one side so the single unknown can be isolated — the standard move for a linear equation where the unknown appears twice.
  3. Solve: . Why this step? Dividing by the coefficient isolates ; that escape probability acts like an effective discount, guaranteeing the loop ends eventually, so even with the value is finite.

Verify: plug back: . ✓ (Intuition: about expected stays plus the exit reward.)


[!example] Example 8 — Cell C8: the round trip

State with policy . We are told and . Compute , then reconstruct both action-values from a hypothetical one-step model to confirm nothing contradicts.

Forecast: will the round trip land back on the same number for every action, not just one?

  1. Down direction, from : Why this step? This is the averaging equation — the canonical way to collapse action-values into a state-value.
  2. Up direction for (consistency): suppose deterministically () leads to a state with , , . Then Why this step? We rebuild the given from the one-step formula.
  3. Up direction for (full check): suppose deterministically leads to a state with , , . Then Why this step? Reconstructing only would leave untested; the Bellman equation must hold for every action, so we verify too before trusting the model.

Verify: both reconstructed values match their givens ( and ), and lies between and as any convex combination must. ✓ This two-way traffic is exactly what policy evaluation iterates.


[!example] Example 9 — Cell C9: real-world word problem

A delivery drone in "Charging" state can, each minute: (i) keep charging — 90% it stays Charging (reward ), 10% it becomes Ready (reward ); or (ii) launch early — always goes to Flying, but reward for low battery. Once Ready, launching gives reward and ends the task (terminal). . The policy always charges until Ready, then launches. What is the value of the Charging state?

Forecast: patience (waiting to charge) should beat launching early — will the value be near or dragged down by the wait?

  1. Value of Ready under this policy: launch → terminal, so Why this step? Ready leads straight to the big reward; it's the anchor we back up from.
  2. Bellman for Charging (self-loop with escape to Ready): Why this step? We translate the English probabilities into : the stay-loop and the escape, each carrying the discount .
  3. Solve for (write for short): Why this step? The self-loop again puts on both sides; we isolate it. The exact value is the fraction ; we round to only for reporting, and we keep for the check so no rounding error creeps in.

Verify: plug the exact back into step 2: The value is well below : the drone must survive many discounted, reward-less charging minutes before cashing in.


[!example] Example 10 — Cell C10: the exam trap "best "

Given , , and a policy (same numbers as Example 3). A student writes " because is the best action." Diagnose and correct.

Forecast: what is the one word in the policy that breaks the student's claim?

  1. Spot the error: the student used , but averages over the policy's actual action probabilities, not the best one. Why this step? holds only for the optimal/greedy policy — see optimal value functions. Here is stochastic, so it isn't greedy.
  2. Correct computation (from Example 3): . Why this step? We follow the actual policy weights; 's only counts of the time.
  3. State the general fact: with equality iff puts all its probability on a maximising action. Why this step? A weighted average of numbers can never exceed the largest of them — that inequality is the whole lesson.

Verify: , and would require — which it isn't. The gap is the price of a policy that sometimes chooses or . ✓


Recall Quick self-test

How does under a stochastic policy relate to ? ::: , with equality only when is greedy on a maximiser. Why does a self-loop still give a finite value in Ex 7? ::: The escape probability guarantees eventual exit, acting like an effective discount. What is of any terminal state? ::: — the base case of the Bellman recursion. Sum of a constant reward forever with discount ? ::: , which diverges as . What does stand for? ::: The world's rulebook: probability of landing in with reward after doing in .

Next stops that use these values: estimating them from samples in Monte-Carlo prediction and temporal-difference learning.