Unit 8 of 11 · about 4 hours · implement
Value iteration and finite-horizon backward induction
Value iteration is the exact baseline for later sampling. Backward induction shows finite-horizon optima depend on steps remaining.
You'll be able to
- Implement value iteration with the maximum backup and extract the greedy policy.
- Implement finite-horizon backward induction with time-dependent values and explain why a stationary policy fails there.
- Reproduce the gambler Figure 4.3 shape and the Hands-on GridWorld 7-sweep convergence.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Value iteration and the gamblerReinforcement Learning: An Introduction (2nd ed.) - Chapters 3 and 4
Chapter 4, Section 4.4 Value Iteration with the boxed algorithm, Example 4.3 Gambler's Problem with Figure 4.3 for and Exercises 4.8 to 4.10, printed pp. 82 to 84. The authors post the full text free at incompleteideas.net.
- Value iteration and finite-horizon policiesStanford CS234 Winter 2026 - Foundations and Planning
Value Iteration with (slide 34) and Value Iteration for Finite Horizon H with and (slide 43) plus Question: Finite Horizon Policies on stationary failure (slides 47 to 48)
- GridWorld value iteration experimentHands-on Modern RL - MDPs, Values and Bellman Equations
Value Iteration: Reading the Environment Rules with synchronous
valuesversusupdatedcode, 7-sweep convergence and , plus Terminal Rewards Are Computed Upon Entry (page read 2026-10-14)
Focus on
- Value iteration uses maximum over actions, not an average under . One synchronous sweep spreads the goal reward by one layer.
- Finite-horizon values depend on steps to go. An infinite-horizon stationary policy need not be optimal for finite .
- Gambler ties mean the optimal policy is not unique. Any tie split among maximum actions stays optimal.
Common mistakes · 3
- Extracting a policy from before convergence and expecting infinite-horizon optimal behavior. Lecture 2 asks whether intermediate policies improve monotonically. They need not.
- Treating finite-horizon as stationary. means optimal with decisions left.
- Ignoring gambler ties and claiming one optimal policy. The book asks for the whole family in Example 4.3.
Try it yourself
Closed-book, before checking anything
python code/chapter03_mdp/gridworld_q_learning.py --output-dir output/value-experiment on CPU to print 7 sweeps and 0.728537, then show the 6-step shortest path with and step penalty 0.01 gives the same number.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does the GridWorld need 7 sweeps for a 6-step path?Answer
Six sweeps carry the goal reward to the start. The seventh sweep finds no change and proves convergence.
2.What are the gambler values at capital 50 for ?Answer
with stake 50. Betting everything wins with probability 0.4 and reaches 100 at once.
3.Is the finite-horizon optimal policy stationary?Answer
No. With one step left the best action maximizes immediate reward. With many steps left it can invest for later. The policy depends on steps remaining.
4.What update separates value iteration from policy evaluation?Answer
Value iteration applies maximum over actions in every sweep. Policy evaluation averages under the fixed policy.
Practice
- Deep-ML 157: Bellman update for value iteration
Two tests on small MDPs: first expects
[1., 1.]withgamma=0.9; second expects[6., 0.]withgamma=0.5 - Deep-ML 164: Gambler value iteration
Two tests:
ph=0.4expectsV[50]=0.4and stake 50;ph=0.25expectsV[80]=0.4534and stake 5Implement both from a blank file first and use Deep-ML as closed-book checks. The section mini-project covers RiverSwim value versus policy agreement.
You're done when
Your value iteration prints the 7-sweep GridWorld result and your gambler run matches the two Deep-ML spot values.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 4 more in the section
- Stanford CS234 Winter 2026 - Foundations and PlanningStanford University · primary
Study Lecture 1 (
lecture1pre.pdfandlecture1post.pdf) and Lecture 2 on tabular MDP planning (lecture2pre.pdfandlecture2post.pdfunder https://web.stanford.edu/class/cs234/slides/). Work Assignment 1 independently: the written MDP, horizon and Bellman-operator questions and thevi_and_pi.pyplanning code on RiverSwim (NumPy only). Do not copy or publish course solutions. - Reinforcement Learning: An Introduction (2nd ed.) - Chapters 3 and 4Richard Sutton and Andrew Barto · required
The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Chapter 3 (Sections 3.1 to 3.7, finite MDPs, returns, Bellman equations and optimality) and Chapter 4 (Sections 4.1 to 4.7, policy evaluation, improvement, policy and value iteration, asynchronous DP and GPI). Reproduce Example 4.1 and Figure 4.1 exactly, then Example 4.3 (the gambler), and write exact tests for stochastic transitions, terminals, ties and stopping error.
- Hands-on Modern RL - MDPs, Values and Bellman EquationsWalking Labs · support
Use the value and Bellman page and the value experiment page (
code/chapter03_mdp/bellman_equation_verify.pyandgridworld_q_learning.pyrun on CPU in seconds) as a code companion. Check every equation against Sutton and Barto before adopting it. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; itscode/chapterNNfolders do not follow the site chapter numbers.
Also in this section
- Gymnasium - Time Limits and Episode SemanticsFarama Foundation · required
Read the termination, truncation and "importance in learning code" sections, then the vector autoreset note at https://farama.org/Vector-Autoreset-Mode (next-step autoreset is the default in Gymnasium 1.x). Build a wrapper test that distinguishes termination from truncation and proves which cases bootstrap before using any third-party environment.
- Deep-ML - Planning ProblemsDeep-ML · support
Use named problems as closed-book checks after your own implementation passes: 157 (Bellman equation for value iteration), 164 (the gambler's problem by value iteration) and 467 (policy iteration). Read each problem's tests before trusting its statement.
- Reinforcement Learning Control Laboratory - Planning CoreRoadmap-Final · required
Complete Milestone A planning requirements: an explicit finite MDP, exact policy evaluation, value iteration, policy iteration, simulator agreement and seeded reward, transition, terminal and tie defects.
- Reinforcement Learning: An Introduction (2nd ed.) - Chapter 2 banditsRichard Sutton and Andrew Barto · reference
Read Section 2.9, Associative Search (Contextual Bandits), printed p. 41, with Exercise 2.10 as the context versus no-context check. Use only this section from Chapter 2.