5.1.9 · D4Reinforcement Learning Foundations

Exercises — Dynamic programming (value - policy iteration)

3,958 words18 min readBack to topic

This page is a self-test ladder for Dynamic Programming. Every problem states its difficulty level (L1 Recognition → L5 Mastery). Try each one before opening its Solution callout. Every symbol you need is recalled inline right here, so you can work the whole page without leaving it; the Bellman-equations and Contraction-mapping-theorem pages carry the fuller proofs.

Before we start, one tiny toy world we reuse. It is the smallest MDP that still shows everything.

Figure — Dynamic programming (value - policy iteration)
Figure s01 — the 3-state chain. Three circles left to right: A (magenta, "start") → B (violet) → G (orange, "terminal", labelled V=0). Two navy arrows, each labelled r = −1, point A→B and B→G. It is a deterministic MDP with one forced action per non-terminal state and a per-step cost of 1.


Level 1 — Recognition

Exercise 1.1 (L1)

State in words what means, and what the discount factor does. No formulas.

Recall Solution 1.1

is the expected total future reward you will collect if you start in state and act forever according to policy . "Expected" = the average over all the random things that could happen.

(gamma), a number with , shrinks rewards that arrive later. A reward that is steps away counts as only . Picture each future step's reward passing through a "dimmer switch" that multiplies it by once per step. This keeps the infinite sum finite and expresses "sooner is better."

Exercise 1.2 (L1)

Match each name to its one-line role: (a) policy evaluation, (b) policy improvement, (c) Bellman backup.

Recall Solution 1.2
  • (a) Policy evaluationgiven a fixed policy , compute (how good is this behaviour?) by repeating the expectation backup .
  • (b) Policy improvementgiven values , pick the greedy action in each state to build a better policy.
  • (c) Bellman backup — one update step ( or ) that replaces a state's value estimate with "immediate reward + discounted value of where you land."

Exercise 1.3 (L1)

In the Bellman optimality equation, which symbol turns "average over what the policy does" into "pick the best action"?

Recall Solution 1.3

The operator. The expectation backup weights actions by (an average); the optimality backup replaces that whole weighted sum by (choose the single best). That swap is the entire difference between "evaluate a policy" and "find the optimal one."


Level 2 — Application

Exercise 2.1 (L2)

On the 3-state chain with per step and , compute , , by backward substitution. (On this forced-policy chain .)

Recall Solution 2.1

Terminal first: .

One step from goal — apply the (deterministic) backup : Two steps from goal: What it looks like — read figure s02 below. The two curved coloured arrows show value flowing backward from : the orange arrow carries into , then the violet arrow carries into . Each circle is labelled with its final value, and you can see value gets more negative the farther you sit from — the cost of the remaining steps, discounted.

Figure — Dynamic programming (value - policy iteration)
Figure s02 — backward value propagation on the chain (γ = 0.9). The same three circles A, B, G now carry their converged values V = −1.9, V = −1, V = 0. Curved arrows point from G back to B (labelled −1+0.9(0)) and from B back to A (labelled −1+0.9(−1)), visualising how each backup pushes value one hop backward, discounted by γ.

Exercise 2.2 (L2)

Redo Exercise 2.1 with . Which state's value changes more, or ? Why?

Recall Solution 2.2

is unchanged () because it is one step from the terminal, so only multiplies . changes from to : a smaller discounts 's future cost more heavily, so far-away costs matter less. Rule of thumb: smaller ⇒ values of distant states get pulled toward the immediate reward.

Exercise 2.3 (L2)

A single synchronous value-iteration sweep starts from the running estimate everywhere on the chain. Compute after one sweep (). Have they converged? (Recall: a sweep applies the optimality backup to every state; synchronous means every backup reads the previous array of zeros.)

Recall Solution 2.3

With all values before the sweep, each backup uses the old zeros: Both become . Not converged: the true . One sweep only lets value travel one hop back from where it was known. It takes another sweep for to "hear about" 's updated .


Level 3 — Analysis

Exercise 3.1 (L3)

Now give state two actions: go (deterministic to , ) and stay (deterministic self-loop to , ). With and the correct values , , is stay ever optimal? Compute and set up the equation for .

Recall Solution 3.1

Recall from the symbol box that = "reward for now of where you land." For go you land on whose value is known: For stay, you land back on , so — and since , the unknown appears on both sides: Suppose stay were optimal: then Since , stay gives a worse value than go. So and stay is never optimal — looping just piles up costs forever.

Exercise 3.2 (L3)

The Policy Improvement Theorem says the greedy policy satisfies for all . Explain, using the chain, why greedy w.r.t. can only help, never hurt.

Recall Solution 3.2

Greedy picks, in each state, the action with the largest . By construction: That inequality says: "acting greedily for one step, then reverting to , is at least as good as ." Chaining this across every state (each step you swap in the greedy choice and never lose value) gives everywhere. On the chain: if the random policy sometimes chose stay, greedy always chooses go, strictly raising 's value from a mixture toward . You cannot do worse than the max, and the max is what greedy takes.

Exercise 3.3 (L3)

Why does value iteration converge to a unique regardless of the initial ? Name the property and the role of , and explain each step of the argument in words.

Recall Solution 3.3

The Bellman optimality operator (one full sweep of "reward + discounted best next value" over all states) is a -contraction in max-norm (see Contraction-mapping-theorem): for any two value arrays , Reading each piece in plain words:

  1. What is — the biggest disagreement between two value guesses across all states (the worst state). We track the worst case because if the worst shrinks, everything shrinks.
  2. Why the update shrinks it — a backup replaces each value by . The is identical for both guesses (same model), so it cancels; only the part differs, and it carries a factor out front. So any gap the input had comes out multiplied by at most . That single is doing all the work.
  3. Why uniqueness and convergence follow — start from any guess; each sweep multiplies the worst-case error by , so after sweeps the error is , which because . Two different fixed points would have to sit zero distance apart (a contraction can't hold them apart), so there is exactly one (Banach fixed-point theorem).

What it looks like: no matter where you start, all initial guesses funnel to the same value, and the funnelling is geometrically fast (error killed by each step). This is exactly why is not optional — it is the spring that pulls every guess to .


Level 4 — Synthesis

Exercise 4.1 (L4)

Run one full iteration of policy iteration on a 2-state MDP. States , terminal (). From : action L (self-loop, ); action R (). Start with policy . Use . (a) Evaluate . (b) Improve. (c) Did the policy change?

Recall Solution 4.1

(a) Policy evaluation of (run to its fixed point). L self-loops, so (b) Policy improvement. Compute both values using , : Greedy picks the larger: , so . (c) Yes, the policy changed from L to R. Re-evaluating gives — a huge jump up from . This is the policy-improvement theorem in action: escaping the costly loop.

Exercise 4.2 (L4)

For the same MDP, run synchronous value iteration from the running estimate . Show sweeps 1, 2 and 3, and give the converged . ( always, since is terminal.)

Recall Solution 4.2

Each sweep applies the optimality backup reading the previous array. Sweep 1 (old ): Sweep 2 (old ): Sweep 3 (old ): The value is stable at (sweep 3 reproduces sweep 2's output), so we stop. Converged , matching policy iteration's answer in 4.1 — as it must, since both solve the same Bellman optimality equation. The greedy action extracted from this is R.


Level 5 — Mastery

Exercise 5.1 (L5)

Stochastic action. State has one action try that with probability reaches terminal () and with probability stays at (). , . Find by solving the fixed-point equation. (Here the from the backup has two terms.)

Recall Solution 5.1

The expectation backup with a stochastic outcome averages over both branches (the ): Expand: Solve: What it looks like: the value sits just below the "lucky" branch's , dragged down by the chance of a stall that also delays the payoff.

Exercise 5.2 (L5)

Truncated evaluation trade-off. Explain precisely why value iteration equals policy iteration with policy-evaluation truncated to a single sweep, giving the intuitive "why" behind the fused-update identity. Then give one situation where full evaluation (policy iteration) wins and one where truncation (value iteration) wins.

Recall Solution 5.2

Why identical structure (step by step). Policy iteration alternates two phases: (i) many evaluation sweeps of the expectation backup until settles, then (ii) a greedy improvement .

  • Step 1 — truncate evaluation to one sweep. Instead of running phase (i) to convergence, do a single backup and move on. The values are now only roughly right, but they are directional — good enough to pick a better action.
  • Step 2 — see that "one greedy sweep of evaluation" already contains the improvement. If, in that single sweep, you evaluate under the greedy policy, then choosing and immediately plugging it in makes the sweep read . The (improvement) and the (evaluation) have fused into a single — that fused line is the optimality backup , i.e. the value-iteration update.
  • Why this is legal: picking the greedy action and then evaluating that one action is the same arithmetic as taking the directly, because "evaluate the best action" and "take the maximum action value" are literally the same number. So value iteration = policy iteration whose evaluation phase is cut to a single greedy sweep. Nothing is lost in correctness (both converge to the same ); only the bookkeeping of an explicit policy is dropped.

Full evaluation wins when the environment has long chains where value must propagate far and the same policy stays greedy across many states (e.g. our long corridor): converging fully means each improvement is a big, well-informed jump, so total outer iterations are few.

Truncation wins when the greedy policy changes frequently as values shift: spending many sweeps to perfectly evaluate a policy you are about to discard is wasted work. Value iteration improves the policy implicitly every sweep, avoiding that waste. This same "don't fully evaluate before improving" idea seeds TD learning, 5.1.11-Q-learning, and 5.1.12-SARSA.

Exercise 5.3 (L5)

Edge cases in . Discuss what happens to the DP methods as (a) and (b) , and (c) whether is ever allowed in the infinite-horizon setting.

Recall Solution 5.3

(a) (myopic agent). The backup collapses to — only the immediate reward survives, all future terms are multiplied by (nearly) zero. The agent becomes greedy-for-now, blind to consequences. Convergence is instant (the contraction factor is , so one sweep essentially finishes), but the resulting policy can be terrible on tasks needing foresight. On our chain, makes every non-terminal value exactly ; the agent cannot "see" the goal beyond one step.

(b) (far-sighted agent). The future is barely discounted, so distant rewards matter almost as much as near ones — good for long-horizon planning. But the contraction factor approaches , so each sweep shrinks the error only slightly ( very slowly). Practically: convergence gets dramatically slower as , and value magnitudes blow up (on the chain, ; on a long corridor of length , values scale like , which grows without bound as ).

(c) Is allowed? In the infinite-horizon setting, is generally not allowed unless the problem is guaranteed to terminate. Reason: with the return can be an infinite sum that does not settle to a finite number (e.g. a self-loop with gives ), and the operator is no longer a contraction ( removes the shrinking factor), so the fixed-point/uniqueness guarantee of Contraction-mapping-theorem fails. is safe for episodic tasks with a reachable terminal state (a "proper" policy), where every trajectory ends and the sum is finite — that is the undiscounted episodic case, common in shortest-path grid worlds.


Recall Quick self-check ledger (answers)

chain, ::: chain, ::: after one synchronous VI sweep ::: in the loop-vs-exit MDP ::: Loop-forever value ::: Stochastic in 5.1 :::


See also