Simple Algorithm
Reinforcement Learning · Bootstrapping · 22 min

Policy Iteration

Evaluate, improve, repeat: why greedy improvement always helps.

Evaluate a policy, then act greedily on its values: the new policy is never worse, and usually strictly better. Repeat, and after a handful of rounds no state can improve, which certifies that the policy is optimal. Policy iteration (Howard, 1960) is one of the oldest algorithms for MDPs and still one of the fastest, and its evaluate-and-improve rhythm is the skeleton of almost every reinforcement learning method.

What you’ll learn

  • State and prove the policy improvement theorem, including when the improvement is strict, and connect it to the performance difference identity.
  • Implement policy iteration, prove that it stops at an optimal policy after finitely many iterations, and handle ties correctly.
  • Run policy iteration by hand on the two-state world, and compare its iteration counts with value iteration's in gridworlds.
  • Describe generalized policy iteration and place value iteration, sampling-based and policy-gradient methods in it.

Acting greedily on a policy’s values

Given VπV^\pi, the action value of taking aa in ss and following π\pi afterwards is Qπ(s,a)=r(s,a)+γ∑s′P(s′∣s,a)Vπ(s′)Q^\pi(s,a)=r(s,a)+\gamma\sum_{s'}P(s'\mid s,a)V^\pi(s'), a one-step lookahead that needs the model. A greedy policy picks π′(s)∈arg max⁡aQπ(s,a)\pi'(s)\in\argmax_aQ^\pi(s,a) in every state. Policy improvement replaces π\pi by such a π′\pi', and the next theorem says this can never hurt.

DerivationProof by unrolling, and an exact identity
  1. Vπ(s)≤Qπ(s,π′(s))=Eπ′[Rt+1+γVπ(St+1)∣St=s]≤Eπ′[Rt+1+γRt+2+γ2Vπ(St+2)∣St=s]≤⋯V^\pi(s)\le Q^\pi\big(s,\pi'(s)\big)=\E_{\pi'}\big[R_{t+1}+\gamma V^\pi(S_{t+1})\mid S_t=s\big]\le\E_{\pi'}\big[R_{t+1}+\gamma R_{t+2}+\gamma^2V^\pi(S_{t+2})\mid S_t=s\big]\le\cdots

    Start from the hypothesis in state ss and apply it again in every state you reach, each time following π′\pi' for one more step before falling back on VπV^\pi:

  2. Vπ(s)≤lim⁡n→∞Eπ′[∑k<nγkRt+k+1 ∣ St=s]=Vπ′(s)V^\pi(s)\le\lim_{n\to\infty}\E_{\pi'}\Big[\sum_{k<n}\gamma^kR_{t+k+1}\ \Big|\ S_t=s\Big]=V^{\pi'}(s)

    After nn steps the remainder γnE[Vπ(St+n)]\gamma^n\E[V^\pi(S_{t+n})] is at most γn∥Vπ∥∞→0\gamma^n\norm{V^\pi}_\infty\to0, and what is left is the expected discounted return of π′\pi':

  3. Vπ′−Vπ=(I−γPπ′)−1Aπ(⋅,π′(⋅)) ≥ Aπ(⋅,π′(⋅)) ≥ 0V^{\pi'}-V^\pi=\big(I-\gamma P^{\pi'}\big)^{-1}A^\pi\big(\cdot,\pi'(\cdot)\big)\ \ge\ A^\pi\big(\cdot,\pi'(\cdot)\big)\ \ge\ 0

    For strictness, write the difference exactly. Since Vπ′=Tπ′Vπ′V^{\pi'}=\mathcal T^{\pi'}V^{\pi'}, adding and subtracting Tπ′Vπ\mathcal T^{\pi'}V^\pi gives Vπ′−Vπ=γPπ′(Vπ′−Vπ)+(Tπ′Vπ−Vπ)V^{\pi'}-V^\pi=\gamma P^{\pi'}(V^{\pi'}-V^\pi)+(\mathcal T^{\pi'}V^\pi-V^\pi). The last vector is the one-step advantage Aπ(s,π′(s))=Qπ(s,π′(s))−Vπ(s)A^\pi(s,\pi'(s))=Q^\pi(s,\pi'(s))-V^\pi(s), so

  4. The middle inequality holds because (I−γPπ′)−1=∑tγt(Pπ′)t≥I(I-\gamma P^{\pi'})^{-1}=\sum_t\gamma^t(P^{\pi'})^t\ge I entrywise and the advantage vector is nonnegative. So the improvement at each state is at least its own advantage, which is strict wherever the advantage is positive. This identity is the performance difference lemma in matrix form (Advantages & the Performance Difference Lemma).

Check your understanding

Two-state world, γ=0.9\gamma=0.9 (in s1s_1: stay pays 1, go pays 0 and moves to s2s_2; in s2s_2: stay pays 2, go pays 0 and moves to s1s_1). The policy (stay, go) has values V=(10,9)V=(10,9). What does one improvement step produce?

  1. (go, stay), the optimal policy
  2. (stay, stay)
  3. (go, go)
  4. No change: (stay, go) is already greedy
Show the answer

B. (stay, stay)

In s1s_1: stay gives 1+0.9×10=101+0.9\times10=10, go gives 0.9×9=8.10.9\times9=8.1, so stay. In s2s_2: stay gives 2+0.9×9=10.12+0.9\times9=10.1, go gives 0.9×10=90.9\times10=9, so switch to stay. The new policy (stay, stay) is worth (10,20)(10,20), and the next improvement reaches (go, stay). Improvement uses the current values, and V(s2)=9V(s_2)=9 is too low for going there to look attractive yet.

Policy iteration

Policy iteration alternates the two steps until the policy stops changing: evaluate πk\pi_k exactly, then let πk+1\pi_{k+1} be greedy for VπkV^{\pi_k}. One detail matters: when several actions tie for the maximum, keep the current action if it is among them. Otherwise the algorithm can switch forever between equally good policies.

python
import numpy as np

def policy_iteration(P, r, gamma):
    """P[s, a, s2] transition probabilities, r[s, a] expected rewards.
    Returns an optimal deterministic policy and its values."""
    S, A = r.shape
    pi = np.zeros(S, dtype=int)                   # any initial policy
    while True:
        P_pi, r_pi = P[np.arange(S), pi], r[np.arange(S), pi]
        V = np.linalg.solve(np.eye(S) - gamma * P_pi, r_pi)   # evaluate exactly
        Q = r + gamma * P @ V                     # S x A one-step lookahead
        keep = Q[np.arange(S), pi] >= Q.max(axis=1) - 1e-12   # ties keep pi
        new_pi = np.where(keep, pi, Q.argmax(axis=1))
        if np.array_equal(new_pi, pi):
            return pi, V                          # no state can improve
        pi = new_pi

Each iteration costs one linear solve, O(S3)\order{S^3}, plus the lookahead, O(S2A)\order{S^2A}. The tolerance in the tie test absorbs rounding errors in VV.

DerivationProof
  1. No repeats. If πk+1≠πk\pi_{k+1}\ne\pi_k, the tie rule means the new action is strictly better in some state, so by the improvement theorem Vπk+1≥VπkV^{\pi_{k+1}}\ge V^{\pi_k} with strict inequality somewhere. Values never decrease and strictly increase somewhere at every change, so no policy can occur twice, and there are only ASA^S deterministic policies.

  2. Vπ=TπVπ=T⋆Vπ⟹Vπ=V⋆V^\pi=\mathcal T^\pi V^\pi=\mathcal T^\star V^\pi\quad\Longrightarrow\quad V^\pi=V^\star

    Optimal on stopping. If πk+1=πk=π\pi_{k+1}=\pi_k=\pi, then π\pi is greedy for its own values, so TπVπ=T⋆Vπ\mathcal T^\pi V^\pi=\mathcal T^\star V^\pi. Since Vπ=TπVπV^\pi=\mathcal T^\pi V^\pi, the values are a fixed point of the Bellman optimality operator, whose only fixed point is V⋆V^\star:

RoundPolicy (s1,s2)(s_1,s_2)Values VπV^{\pi}Lookahead in s1s_1: stay / goLookahead in s2s_2: stay / go
0(go, go)(0,0)(0,0)1 / 02 / 0
1(stay, stay)(10,20)(10,20)10 / 1820 / 9
2(go, stay)(18,20)(18,20)17.2 / 1820 / 16.2
Policy iteration on the two-state world (γ=0.9\gamma=0.9) from the worst policy. Bold entries are where the lookahead switches or confirms go. Round 2's lookahead changes nothing, so (go, stay) is optimal, with V⋆=(18,20)V^\star=(18,20): two policy changes, three evaluations.

How many iterations?

The bound ASA^S is astronomically pessimistic. In practice policy iteration needs few rounds: in Sutton and Barto's car-rental example (Example 4.2) it reaches the optimal policy after a few improvements, starting from a policy that never moves any cars. Part of the reason is a simple comparison: started from the same values Vπ0V^{\pi_0}, policy iteration is never behind value iteration, Vπk≥(T⋆)kVπ0V^{\pi_k}\ge(\mathcal T^\star)^kV^{\pi_0}, because each evaluation goes all the way to Vπk+1≥T⋆VπkV^{\pi_{k+1}}\ge\mathcal T^\star V^{\pi_k}.

Predict before you look

In Sutton and Barto's 4×4 gridworld (two exit corners, −1-1 per step), start policy iteration from the random policy, whose values you computed in Policy Evaluation. How many improvement steps until the policy is optimal?

Show the answer

1

One. Acting greedily on the random policy's values (−14-14 next to an exit, −22-22 in the far corners) already heads straight for the nearest exit from every cell. A second evaluation and lookahead are needed only to confirm that nothing changes.

Why so few rounds, and is that a theorem? Policy iteration is Newton's method on the Bellman equation, and for a fixed γ\gamma its number of iterations is bounded by a polynomial in SS and AA alone. Without a fixed γ\gamma, carefully built MDPs force exponentially many iterations. The Fixed Point lessons develop both stories.

Generalized policy iteration

Sutton and Barto call the general pattern generalized policy iteration (GPI): an evaluation process pulls the values toward those of the current policy, an improvement process makes the policy greedier with respect to the current values, and the two interleave at any granularity. They pull in different directions, since improving the policy makes the values wrong, but the only point where both are satisfied is a policy that is greedy for its own values, and that is an optimal policy by the stopping argument above.

Almost every algorithm in this course is GPI with a particular choice of how much evaluation to do and how greedy to be.

AlgorithmEvaluationImprovement
Policy iterationExact solve of (I−γPπ)V=rπ(I-\gamma P^\pi)V=r^\piFully greedy
Value iterationA single backupGreedy (the max inside the backup)
Modified policy iterationmm backups of the current policyFully greedy
Monte Carlo controlAverages of sampled returnsε\varepsilon-greedy
SARSA, Q-learningOne temporal-difference update per stepε\varepsilon-greedy, or greedy inside the target
Actor–criticA critic learned by TDA small policy-gradient step
AlphaZeroA value network trained on self-play outcomesTree search produces a better policy, which the network imitates
Less evaluation per improvement makes each round cheaper but noisier. With exact models the extremes both converge; with samples and function approximation, how much to evaluate before improving becomes a real design choice.
Check your understanding

In generalized policy iteration, suppose the values equal VπV^\pi and π\pi is greedy with respect to them. What follows?

  1. Nothing yet: more improvement steps are needed
  2. π\pi is optimal only if it was reached by exact evaluation
  3. π\pi is optimal, because VπV^\pi then satisfies the Bellman optimality equation
  4. π\pi is optimal only for the states visited so far
Show the answer

C. π\pi is optimal, because VπV^\pi then satisfies the Bellman optimality equation

Vπ=TπVπV^\pi=\mathcal T^\pi V^\pi, and greediness gives TπVπ=T⋆Vπ\mathcal T^\pi V^\pi=\mathcal T^\star V^\pi, so VπV^\pi is the unique fixed point V⋆V^\star. How the process got there does not matter.

Key takeaways

  • Acting greedily on VπV^\pi never hurts: Vπ′−Vπ=(I−γPπ′)−1Aπ(⋅,π′(⋅))≥0V^{\pi'}-V^\pi=(I-\gamma P^{\pi'})^{-1}A^\pi(\cdot,\pi'(\cdot))\ge0, with strict gains wherever the advantage is positive.
  • Policy iteration alternates exact evaluation and greedy improvement. With ties broken in favor of the current action it never repeats a policy, stops within ASA^S rounds, and stops only at an optimal policy.
  • In practice it needs very few rounds (one improvement from the random policy in the 4×4 gridworld, five rounds on the cliff walk at both γ=0.9\gamma=0.9 and γ=0.99\gamma=0.99), and from a common starting point its values always keep up with value iteration's.
  • Generalized policy iteration, evaluation and improvement interleaved at any granularity, is the template behind value iteration, Monte Carlo control, Q-learning, actor–critic and AlphaZero.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A deterministic policy π′\pi' satisfies Qπ(s,π′(s))≥Vπ(s)Q^\pi(s,\pi'(s))\ge V^\pi(s) in every state, with strict inequality in state s0s_0 only. What follows?

  1. Vπ′≥VπV^{\pi'}\ge V^\pi everywhere, and Vπ′(s0)>Vπ(s0)V^{\pi'}(s_0)>V^\pi(s_0)
  2. Vπ′>VπV^{\pi'}>V^\pi in every state
  3. Vπ′(s0)>Vπ(s0)V^{\pi'}(s_0)>V^\pi(s_0), but other states may get worse
  4. Nothing, unless π′\pi' is greedy for VπV^\pi
Show the answer

A. Vπ′≥VπV^{\pi'}\ge V^\pi everywhere, and Vπ′(s0)>Vπ(s0)V^{\pi'}(s_0)>V^\pi(s_0)

Vπ′−Vπ=(I−γPπ′)−1Aπ(⋅,π′(⋅))V^{\pi'}-V^\pi=(I-\gamma P^{\pi'})^{-1}A^\pi(\cdot,\pi'(\cdot)) with a nonnegative advantage vector, so every state weakly improves, and s0s_0 strictly. Other states improve strictly too if they can reach s0s_0 under π′\pi', but the theorem guarantees only weak improvement there.

Question 2

An MDP has 10 states and 3 actions in each. What upper bound on the number of policy iteration rounds does the termination proof give?

  1. 30
  2. 103=1,00010^3=1{,}000
  3. 310=59,0493^{10}=59{,}049
  4. No finite bound
Show the answer

C. 310=59,0493^{10}=59{,}049

There are AS=310A^S=3^{10} deterministic policies, and none can repeat. Real runs take far fewer rounds, and for a fixed γ\gamma much better bounds are proved in How Fast Is Policy Iteration?

Question 3

A programmer implements policy iteration with argmax, which breaks ties arbitrarily, and stops only when the policy does not change. On an MDP where some state has two equally good optimal actions, what can happen?

  1. It returns a suboptimal policy
  2. It raises an error in the linear solve
  3. It converges faster than with the tie rule
  4. It can switch forever between equally good optimal policies
Show the answer

D. It can switch forever between equally good optimal policies

If the two tied actions are picked alternately, the policy keeps changing while the values do not. Keeping the current action on ties, or stopping when the values stop changing, fixes it.

Question 4

Which algorithm is generalized policy iteration with one backup of evaluation per greedy improvement?

  1. Policy iteration
  2. Value iteration
  3. Monte Carlo evaluation
  4. Linear programming
Show the answer

B. Value iteration

A value-iteration backup, max⁡a[r(s,a)+γ∑s′P(s′∣s,a)V(s′)]\max_a[r(s,a)+\gamma\sum_{s'}P(s'\mid s,a)V(s')], is one backup of the policy that is greedy for the current VV: the improvement is hidden inside the max.

Go deeper

Further reading

  • R. A. Howard, Dynamic Programming and Markov Processes (MIT Press, 1960).
  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 4 (MIT Press, 2nd ed., 2018).
  • M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 6 (Wiley, 1994).