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 , the action value of taking in and following afterwards is , a one-step lookahead that needs the model. A greedy policy picks in every state. Policy improvement replaces by such a , and the next theorem says this can never hurt.
Start from the hypothesis in state and apply it again in every state you reach, each time following for one more step before falling back on :
After steps the remainder is at most , and what is left is the expected discounted return of :
For strictness, write the difference exactly. Since , adding and subtracting gives . The last vector is the one-step advantage , so
The middle inequality holds because 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).
Two-state world, (in : stay pays 1, go pays 0 and moves to ; in : stay pays 2, go pays 0 and moves to ). The policy (stay, go) has values . What does one improvement step produce?
- (go, stay), the optimal policy
- (stay, stay)
- (go, go)
- No change: (stay, go) is already greedy
Show the answer
B. (stay, stay)
In : stay gives , go gives , so stay. In : stay gives , go gives , so switch to stay. The new policy (stay, stay) is worth , and the next improvement reaches (go, stay). Improvement uses the current values, and is too low for going there to look attractive yet.
Policy iteration
Policy iteration alternates the two steps until the policy stops changing: evaluate exactly, then let be greedy for . 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.
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_piEach iteration costs one linear solve, , plus the lookahead, . The tolerance in the tie test absorbs rounding errors in .
No repeats. If , the tie rule means the new action is strictly better in some state, so by the improvement theorem with strict inequality somewhere. Values never decrease and strictly increase somewhere at every change, so no policy can occur twice, and there are only deterministic policies.
Optimal on stopping. If , then is greedy for its own values, so . Since , the values are a fixed point of the Bellman optimality operator, whose only fixed point is :
| Round | Policy | Values | Lookahead in : stay / go | Lookahead in : stay / go |
|---|---|---|---|---|
| 0 | (go, go) | 1 / 0 | 2 / 0 | |
| 1 | (stay, stay) | 10 / 18 | 20 / 9 | |
| 2 | (go, stay) | 17.2 / 18 | 20 / 16.2 |
How many iterations?
The bound 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 , policy iteration is never behind value iteration, , because each evaluation goes all the way to .
In Sutton and Barto's 4×4 gridworld (two exit corners, 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 ( next to an exit, 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 its number of iterations is bounded by a polynomial in and alone. Without a fixed , 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.
| Algorithm | Evaluation | Improvement |
|---|---|---|
| Policy iteration | Exact solve of | Fully greedy |
| Value iteration | A single backup | Greedy (the max inside the backup) |
| Modified policy iteration | backups of the current policy | Fully greedy |
| Monte Carlo control | Averages of sampled returns | -greedy |
| SARSA, Q-learning | One temporal-difference update per step | -greedy, or greedy inside the target |
| Actor–critic | A critic learned by TD | A small policy-gradient step |
| AlphaZero | A value network trained on self-play outcomes | Tree search produces a better policy, which the network imitates |
In generalized policy iteration, suppose the values equal and is greedy with respect to them. What follows?
- Nothing yet: more improvement steps are needed
- is optimal only if it was reached by exact evaluation
- is optimal, because then satisfies the Bellman optimality equation
- is optimal only for the states visited so far
Show the answer
C. is optimal, because then satisfies the Bellman optimality equation
, and greediness gives , so is the unique fixed point . How the process got there does not matter.
Key takeaways
- Acting greedily on never hurts: , 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 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 and ), 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.
A deterministic policy satisfies in every state, with strict inequality in state only. What follows?
- everywhere, and
- in every state
- , but other states may get worse
- Nothing, unless is greedy for
Show the answer
A. everywhere, and
with a nonnegative advantage vector, so every state weakly improves, and strictly. Other states improve strictly too if they can reach under , but the theorem guarantees only weak improvement there.
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?
- 30
- No finite bound
Show the answer
C.
There are deterministic policies, and none can repeat. Real runs take far fewer rounds, and for a fixed much better bounds are proved in How Fast Is Policy Iteration?
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?
- It returns a suboptimal policy
- It raises an error in the linear solve
- It converges faster than with the tie rule
- 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.
Which algorithm is generalized policy iteration with one backup of evaluation per greedy improvement?
- Policy iteration
- Value iteration
- Monte Carlo evaluation
- Linear programming
Show the answer
B. Value iteration
A value-iteration backup, , is one backup of the policy that is greedy for the current : the improvement is hidden inside the max.
Go deeper
- Policy Iteration as Newton’s Method · Why policy iteration converges in so few steps.
- How Fast Is Policy Iteration? · Strongly polynomial bounds for a fixed discount, and exponential worst cases.
- Modified & Generalized Policy Iteration · Truncated evaluation, optimistic policy iteration and the spectrum between PI and VI.
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).