Policy Iteration
Prerequisites
- MDPs — Bellman expectation equations, value functions
- Value Iteration — Bellman optimality operator, convergence
🎯 Learning Objectives
- Understand the two phases: policy evaluation and policy improvement
- Solve the linear system for policy evaluation
- Prove the policy improvement theorem
- Compare policy iteration vs value iteration in terms of iterations and cost
- Explain modified policy iteration
1. Overview
Policy iteration is an alternative DP method that maintains an explicit policy and alternates between two steps:
- Policy Evaluation: Compute Vπ for the current policy π
- Policy Improvement: Compute a new policy π′ that is greedy w.r.t. Vπ
This continues until the policy no longer changes (π′ = π), at which point we have the optimal policy.
💡 Intuition
Value iteration combines evaluation and improvement into a single step (each Bellman backup does both). Policy iteration separates them: it fully evaluates the current policy, then improves it. This is more expensive per iteration but typically requires far fewer iterations.
2. Policy Evaluation
📚 Policy Evaluation Problem
Given a policy π, compute Vπ(s) for all s ∈ S. This requires solving the Bellman expectation equation:
Vπ(s) = ∑a π(a|s) ∑s′ P(s′|s,a) [R(s,a,s′) + γ Vπ(s′)]
2.1 Direct Solution (Linear System)
For a deterministic policy π(s) = a, the Bellman equation for each state gives one linear equation in |S| unknowns. The system can be written in matrix form:
Vπ = Rπ + γ Pπ Vπ
Solving: Vπ = (I − γ Pπ)−1 Rπ
⚠ Computational Cost of Direct Solution
Matrix inversion costs O(|S|³), which is feasible for small state spaces but prohibitive for large MDPs. In practice, iterative policy evaluation is preferred.
2.2 Iterative Policy Evaluation
Algorithm: Iterative Policy Evaluation
function POLICY-EVALUATION(π, MDP, ε):
Initialize V(s) = 0 for all s ∈ S
repeat:
Δ ← 0
for each state s ∈ S:
v ← V(s)
V(s) ← ∑_a π(a|s) ∑_{s'} P(s'|s,a) [R(s,a,s') + γ V(s')]
Δ ← max(Δ, |v - V(s)|)
until Δ < ε
return V
Notice this is similar to value iteration but uses the policy π to select actions rather than taking the max. The update is linear (no max), so convergence is guaranteed.
📝 Example: Policy Evaluation for a 2-State MDP
States: {s1, s2}. γ = 0.9. Policy: π(s1) = a, π(s2) = a.
Transitions: P(s1|s1,a)=0.5, P(s2|s1,a)=0.5, R=+5
P(s1|s2,a)=0.8, P(s2|s2,a)=0.2, R=+2
Bellman equations:
V(s1) = 5 + 0.9[0.5·V(s1) + 0.5·V(s2)]
V(s2) = 2 + 0.9[0.8·V(s1) + 0.2·V(s2)]
Simplifying: V(s1)(1 − 0.45) = 5 + 0.45·V(s2) ⇒ 0.55·V(s1) = 5 + 0.45·V(s2)
V(s2)(1 − 0.18) = 2 + 0.72·V(s1) ⇒ 0.82·V(s2) = 2 + 0.72·V(s1)
From the second equation: V(s2) = (2 + 0.72·V(s1)) / 0.82
Substituting: 0.55·V(s1) = 5 + 0.45 × (2 + 0.72·V(s1)) / 0.82
0.55·V(s1) = 5 + (0.9 + 0.324·V(s1)) / 0.82
0.55·V(s1) = 5 + 1.098 + 0.395·V(s1)
0.155·V(s1) = 6.098 ⇒ V(s1) ≈ 39.34
V(s2) ≈ 37.01
3. Policy Improvement
🔧 Policy Improvement Theorem
Let π and π′ be two deterministic policies such that for all s ∈ S:
Qπ(s, π′(s)) ≥ Vπ(s)
Then π′ is at least as good as π: Vπ′(s) ≥ Vπ(s) for all s ∈ S.
If there is strict inequality for at least one state, then π′ is strictly better.
The improvement step computes the greedy policy with respect to Vπ:
π′(s) = argmaxa ∑s′ P(s′|s,a) [R(s,a,s′) + γ Vπ(s′)]
💡 Why Does Greedy Improvement Work?
For the greedy policy π′, we have Qπ(s, π′(s)) = maxa Qπ(s,a) ≥ Qπ(s, π(s)) = Vπ(s). So the condition of the policy improvement theorem is automatically satisfied.
4. Policy Iteration Algorithm
Algorithm: Policy Iteration
function POLICY-ITERATION(MDP):
Initialize π(s) arbitrarily for all s ∈ S
repeat:
// Step 1: Policy Evaluation
V ← POLICY-EVALUATION(π, MDP, ε)
// Step 2: Policy Improvement
policy_stable ← true
for each state s ∈ S:
old_action ← π(s)
π(s) ← argmax_a ∑_{s'} P(s'|s,a) [R(s,a,s') + γ V(s')]
if old_action ≠ π(s):
policy_stable ← false
until policy_stable
return V, π
🔧 Convergence Guarantee
Policy iteration converges to the optimal policy π* in a finite number of iterations. This is because:
- Each improvement step produces a strictly better policy (or the same policy if already optimal)
- There are only finitely many deterministic policies (|A||S|)
- No policy can be revisited (monotonic improvement)
Therefore, the algorithm must terminate.
5. Value Iteration vs Policy Iteration
| Property | Value Iteration | Policy Iteration |
|---|---|---|
| Approach | Iterative Bellman optimality backup | Alternate evaluation & improvement |
| Update rule | V(s) ← maxa […] | V(s) ← ∑a π(a|s)[…] then improve π |
| Iterations to converge | Many (can be 100s) | Few (often 5–10) |
| Cost per iteration | O(|S|²|A|) per sweep | O(|S|³) for exact evaluation + O(|S|²|A|) for improvement |
| Total cost | Often lower for small |A| | Often lower for large |A| |
| When policy converges | Values may not have converged yet | Exactly when Vπ satisfies optimality |
| Memory | O(|S|) | O(|S| + |S|) (values + policy) |
| Simplicity | Simpler to implement | Slightly more complex |
📝 When to Choose Which?
- Value iteration is preferred when |S| is very large (avoiding O(|S|³) evaluation) or when an approximate solution suffices
- Policy iteration is preferred when |A| is large (fewer iterations offset the evaluation cost) or when exact solutions are needed
- In practice, modified policy iteration (below) often works best
6. Modified Policy Iteration
Instead of evaluating the policy to convergence (or solving the linear system exactly), perform only k sweeps of iterative evaluation before improving the policy.
Algorithm: Modified Policy Iteration (k sweeps)
function MODIFIED-POLICY-ITERATION(MDP, k):
Initialize π arbitrarily, V(s) = 0 for all s
repeat:
// Partial Policy Evaluation (k sweeps instead of full convergence)
for i = 1 to k:
for each s ∈ S:
V(s) ← ∑_{s'} P(s'|s,π(s)) [R(s,π(s),s') + γ V(s')]
// Policy Improvement
policy_stable ← true
for each s ∈ S:
old_action ← π(s)
π(s) ← argmax_a ∑_{s'} P(s'|s,a) [R(s,a,s') + γ V(s')]
if old_action ≠ π(s): policy_stable ← false
until policy_stable
return V, π
💡 Special Cases
- k = ∞ (full evaluation): Standard policy iteration
- k = 1: Equivalent to value iteration!
- k = 3–5: Often the sweet spot in practice
7. Worked Example: Policy Iteration on 3-State MDP
📝 Setup
States: {A, B, C}. Actions: {go, stay}. γ = 0.9. C is terminal (V(C)=0).
| State | Action | Next State | P | R |
|---|---|---|---|---|
| A | go | B | 1.0 | +2 |
| A | stay | A | 1.0 | +1 |
| B | go | C | 1.0 | +10 |
| B | stay | B | 1.0 | +3 |
Iteration 1: Start with π0 = {A: stay, B: stay}
Policy Evaluation:
V(A) = 1 + 0.9·V(A) ⇒ V(A) = 1/(1−0.9) = 10
V(B) = 3 + 0.9·V(B) ⇒ V(B) = 3/(1−0.9) = 30
Policy Improvement:
State A: Q(A,go) = 2 + 0.9×30 = 29 vs Q(A,stay) = 1 + 0.9×10 = 10 ⇒ π′(A) = go
State B: Q(B,go) = 10 + 0.9×0 = 10 vs Q(B,stay) = 3 + 0.9×30 = 30 ⇒ π′(B) = stay
Policy changed! π1 = {A: go, B: stay}
Iteration 2: Evaluate π1 = {A: go, B: stay}
Policy Evaluation:
V(A) = 2 + 0.9·V(B) and V(B) = 3 + 0.9·V(B) ⇒ V(B) = 30
V(A) = 2 + 0.9×30 = 29
Policy Improvement:
State A: Q(A,go) = 2 + 0.9×30 = 29 vs Q(A,stay) = 1 + 0.9×29 = 27.1 ⇒ π′(A) = go ✅
State B: Q(B,go) = 10 + 0 = 10 vs Q(B,stay) = 3 + 0.9×30 = 30 ⇒ π′(B) = stay ✅
Policy unchanged! π* = {A: go, B: stay}. Converged in 2 iterations.
Summary
- Policy iteration alternates evaluation (compute Vπ) and improvement (greedy w.r.t. Vπ)
- Policy evaluation solves a linear system or uses iterative methods
- The policy improvement theorem guarantees monotonic progress
- Converges in finite iterations (typically very few: 5–10)
- Each iteration is more expensive than value iteration but fewer iterations are needed
- Modified policy iteration with k partial evaluation sweeps bridges the gap between VI (k=1) and PI (k=∞)
References
- Sutton & Barto, RL: An Introduction, Chapter 4.2–4.3
- Russell & Norvig, AIMA, Chapter 17.3