Unit 7 of 11 · about 4 hours · derive, implement

Policy improvement, policy iteration and generalized policy iteration

Policy iteration finds the optimal policy in few outer loops. If improvement uses stale values, it can stall short of optimal.

You'll be able to

  • Prove improvement from to .
  • Implement policy iteration that evaluates to tolerance and then improves greedily until the policy is stable.
  • Explain generalized policy iteration as evaluation and improvement truncated anywhere in between.

Covers

Policy improvement and greedy policy constructionPolicy iterationGeneralized policy iteration
1

Read

In this order, only the parts listed

0 of 3 done

  1. Improvement, iteration and GPIReinforcement Learning: An Introduction (2nd ed.) - Chapters 3 and 4

    Chapter 4, Section 4.2 Policy Improvement with (4.6) to (4.9) and ties note, Section 4.3 Policy Iteration with the boxed algorithm, Section 4.6 Generalized Policy Iteration, printed pp. 76 to 86. The authors post the full text free at incompleteideas.net.

  2. Policy iteration and Bellman fixed pointsStanford CS234 Winter 2026 - Foundations and Planning

    MDP Policy Iteration (slide 19, repeated on slide 22) and Policy Iteration as Bellman Operations with fixed point (slides 35 to 36)

  3. Greedy policy from action valuesHands-on Modern RL - MDPs, Values and Bellman Equations

    From Q to a Policy with the greedy rule (page read 2026-10-14)

2

Focus on

  • Improvement needs , not alone. Greedy means .
  • If the greedy policy equals the old policy everywhere, values satisfy optimality and both are optimal.
  • Generalized policy iteration truncates evaluation. Value iteration truncates to one sweep.
Common mistakes · 3
  • Stopping evaluation after fixed sweeps and calling it policy iteration. Run to tolerance or track the GPI error.
  • Breaking ties differently each sweep and reporting non-convergence. Exercise 4.4 warns that switching among equally good policies never terminates. Fix the tie rule.
  • Improving the policy in place during the sweep. Use of the old policy for every state in that improvement.
3

Try it yourself

Closed-book, before checking anything

From a blank file, run policy iteration on RiverSwim WEAK with and tolerance 0.001 to get left-most 30.328 and right-most 36.859 with an all-R policy, matching value iteration within 0.001.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What condition triggers the improvement theorem?Answer

    When for all , then for all , with strict gain where the first inequality is strict.

  2. 2.Why must policy iteration converge in finite MDPs?Answer

    There are finitely many deterministic policies. Each outer loop strictly improves unless already optimal, so it cannot cycle forever.

  3. 3.What is the Exercise 4.4 bug?Answer

    The boxed loop can switch forever among equally good policies. Stop only when the policy is stable under a fixed tie rule.

  4. 4.Where does generalized policy iteration sit?Answer

    Anywhere between full evaluation plus greedy improvement and one-sweep value iteration. Both processes compete and constrain each other.

5

Practice

  • Deep-ML 467: Policy Iteration for MDPs

    Two tests with : 2-state MDP expects ([1, 1], [11.0, 10.0]); 3-state chain expects ([1, 1, 1], [18.1, 19.0, 20.0])

    Implement from a blank file first and use Deep-ML as a closed-book check. The section mini-project covers RiverSwim policy versus value agreement.

You're done when

Your policy iteration matches the RiverSwim sanity values and your greedy policy stops changing under a fixed tie rule.

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.pdf and lecture1post.pdf) and Lecture 2 on tabular MDP planning (lecture2pre.pdf and lecture2post.pdf under https://web.stanford.edu/class/cs234/slides/). Work Assignment 1 independently: the written MDP, horizon and Bellman-operator questions and the vi_and_pi.py planning 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.py and gridworld_q_learning.py run 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; its code/chapterNN folders 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.