Policy Iteration

Imagine a robot navigating a warehouse grid. At every cell, it decides whether to move north, south, east, or west. It has a full map and knows every reward. It also knows exactly how each action changes its position. The question is not exploration — it already knows everything about the world. The question is computation. How does it turn a complete model of the environment into the single best behavior? That behavior must work forever. Policy iteration answers this question with a strikingly simple loop. Start with any policy, however bad. Compute exactly how good that policy is, state by state. Then improve it by acting greedily with respect to that evaluation. Repeat. This alternation between measuring and improving is one of the oldest ideas in sequential decision-making. It predates reinforcement learning as a field. It originated in operations-research work on Markov decision processes in the 1950s and 1960s. Policy iteration matters because it is the conceptual skeleton on which nearly all of reinforcement learning is built. Modern deep-RL algorithms include actor-critic methods, deep Q-learning, and proximal policy optimization. All of these are, at some level of abstraction, approximate versions of the same loop. They are sampled, function-approximated versions of evaluate-then-improve. Understanding the exact, tabular version matters for this reason. Every step in it is provably correct. This gives you the vocabulary and proof techniques needed. With them, you can understand why the approximate versions are reasonable at all. This article gives the precise algorithm for finite Markov decision processes with a known model. It proves that each iteration strictly improves the policy, until convergence. It derives its computational cost. It also works a full numeric example by hand. It closes by connecting policy iteration to Value Iteration, its faster but less exact cousin.

Key takeaways
  • Policy iteration alternates two subroutines: policy evaluation (compute vπv_\pi exactly) and policy improvement (act greedily with respect to vπv_\pi).
  • Each improvement step produces a policy that is strictly better, unless the current policy is already optimal — this is the policy improvement theorem.
  • Because there are only finitely many deterministic policies, the loop terminates after a finite number of iterations, always at an optimal policy.
  • Policy evaluation itself is normally solved iteratively (or exactly via a linear system), coupling two nested loops.
  • The tradeoff versus Value Iteration is fewer, more expensive outer iterations versus more, cheaper ones.
  • Policy iteration requires a full model (transition probabilities and rewards) and is a cornerstone of Dynamic Programming approaches to reinforcement learning.
The core insight

Alternately computing exact state values for the current policy and then switching to the greedy action at every state converges, in finitely many strict improvements, to an optimal policy.

The idea

A finite Markov decision process (MDP) is specified by a state set S\mathcal{S}, an action set A\mathcal{A}, transition probabilities p(s′∣s,a)p(s' \mid s, a), a reward function r(s,a,s′)r(s, a, s'), and a discount factor γ∈[0,1)\gamma \in [0, 1). A policy π\pi maps states to (distributions over) actions. The state-value function vπ(s)v_\pi(s) is the expected discounted return starting from ss and following π\pi thereafter, satisfying the Bellman Optimality Equation's cousin, the Bellman expectation equation:

vπ(s)=∑aπ(a∣s)∑s′p(s′∣s,a)[r(s,a,s′)+γ vπ(s′)].v_\pi(s) = \sum_a \pi(a\mid s) \sum_{s'} p(s'\mid s,a)\big[r(s,a,s') + \gamma\, v_\pi(s')\big].

Policy evaluation is the task of computing vπv_\pi for a fixed policy π\pi. Assuming Iterative Policy Evaluation as a known primitive, this is solved by repeatedly applying the Bellman expectation backup as an update rule until the value function stops changing (or changes by less than a tolerance θ\theta).

Policy improvement takes a value function vπv_\pi and produces a new, deterministic policy π′\pi' by acting greedily:

π′(s)=arg⁡max⁡a∑s′p(s′∣s,a)[r(s,a,s′)+γ vπ(s′)]=arg⁡max⁡aqπ(s,a).\pi'(s) = \arg\max_a \sum_{s'} p(s'\mid s,a)\big[r(s,a,s') + \gamma\, v_\pi(s')\big] = \arg\max_a q_\pi(s,a).

Policy iteration is the composition of these two steps, repeated: evaluate π0\pi_0 to get vπ0v_{\pi_0}, improve to get π1\pi_1, evaluate π1\pi_1 to get vπ1v_{\pi_1}, improve to get π2\pi_2, and so on. The sequence of policies is monotonically non-decreasing in value at every state, and because the number of deterministic policies over a finite state–action space is finite, the sequence must reach a fixed point — a policy that is greedy with respect to its own value function. Such a fixed point satisfies the Bellman optimality equation exactly, so it is an optimal policy.

How it works

The algorithm has an outer loop (policy improvement) wrapping an inner loop (policy evaluation). The inner loop can be run to full convergence (classical policy iteration) or truncated after a fixed number of sweeps (a variant that shades continuously into value iteration). ``` Algorithm: Policy Iteration Input: MDP (S, A, p, r, γ), evaluation tolerance θ > 0 Output: optimal policy π, optimal value function v 1. Initialize: V(s) ← 0 for all s in S # or any initial guess π(s) ← arbitrary action in A(s) # for all s in S 2. repeat (outer loop: "policy iteration")

# ---- Step A: Policy Evaluation ---- repeat Δ ← 0 for each s in S: v_old ← V(s) V(s) ← Σ_{s'} p(s' | s, π(s)) · [ r(s, π(s), s') + γ · V(s') ] Δ ← max(Δ, |v_old − V(s)|) until Δ < θ

# ---- Step B: Policy Improvement ---- policy_stable ← true for each s in 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 3. until policy_stable = true 4. return π, V ``` Two implementation notes matter in practice. First, ties in the `argmax` should be broken consistently (e.g. keep the old action if it is still optimal) — this avoids policies oscillating between equally-good actions without ever satisfying `policy_stable`. Second, evaluation does not need to restart from zero each outer iteration: warm-starting VV from the previous policy's values converges the inner loop faster, since πk+1\pi_{k+1} is typically close to πk\pi_k.

Figure
The evaluate–improve cycle, and why it must terminate
Policy Evaluation solve v_π = Bellman-expect(π) (iterate sweeps until Δ < θ) produces exact v_π Policy Improvement π'(s) = argmax_a q_π(s,a) greedy w.r.t. v_π produces new π' v_π π' (feeds back as current policy) Why it must stop |A|^|S| deterministic policies exist (finite). Each improvement step strictly increases value, unless already optimal — no policy repeats.

Complexity & correctness

Per-iteration cost. Let n=∣S∣n = |\mathcal{S}| and k=∣A∣k = |\mathcal{A}| (assume a bounded action set per state). One sweep of policy evaluation costs O(n⋅b)O(n \cdot b), where bb is the branching factor of the transition function (number of possible next states per state–action pair, often treated as O(n)O(n) in the worst case for a dense MDP, but typically much smaller for structured problems). A single evaluation phase runs some number of sweeps EE until convergence to tolerance θ\theta; empirically EE is small (tens of sweeps) and is bounded theoretically because the Bellman expectation backup is a γ\gamma-contraction in the max-norm, giving E=O ⁣(log⁡(1/θ)log⁡(1/γ))E = O\!\left(\frac{\log(1/\theta)}{\log(1/\gamma)}\right). Policy improvement costs O(n⋅k⋅b)O(n \cdot k \cdot b), one QQ-value computation per action per state.

Outer loop cost. The number of outer iterations is bounded by the number of deterministic policies, knk^n, since no policy can repeat (proved below) — but this bound is astronomically loose. In practice the number of outer iterations observed is typically small, often logarithmic-feeling in problem size, though no tight polynomial bound is known in general for arbitrary discount factors; for fixed discount factor γ\gamma, policy iteration is known to run in strongly polynomial time in nn, kk, and 1/(1−γ)1/(1-\gamma) (a result following from connections to linear programming and simplex-type analyses of MDPs). This is a much sharper guarantee than the naive knk^n bound suggests.

Space complexity. O(n)O(n) for the value function, O(n)O(n) for the policy, plus the model itself which may require O(n2k)O(n^2 k) to store dense transition probabilities (often sparse in practice).

Correctness — the Policy Improvement Theorem. This is the crux of why the algorithm works, so it deserves a full argument rather than an assertion. Claim: If π′\pi' is greedy with respect to vπv_\pi, i.e. qπ(s,π′(s))≥vπ(s)q_\pi(s, \pi'(s)) \geq v_\pi(s) for all ss, then vπ′(s)≥vπ(s)v_{\pi'}(s) \geq v_\pi(s) for all ss. Proof sketch. Start from the hypothesis and unroll one step at a time:

vπ(s)≤qπ(s,π′(s))=E[Rt+1+γ vπ(St+1)∣St=s,At=π′(s)].v_\pi(s) \le q_\pi(s, \pi'(s)) = \mathbb{E}\big[R_{t+1} + \gamma\, v_\pi(S_{t+1}) \mid S_t = s, A_t = \pi'(s)\big].

Now substitute the same inequality for vπ(St+1)≤qπ(St+1,π′(St+1))v_\pi(S_{t+1}) \le q_\pi(S_{t+1}, \pi'(S_{t+1})) inside the expectation, and repeat this substitution indefinitely:

vπ(s)≤Eπ′[Rt+1+γRt+2+γ2Rt+3+⋯∣St=s]=vπ′(s).v_\pi(s) \le \mathbb{E}_{\pi'}\big[R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots \mid S_t = s\big] = v_{\pi'}(s).

Each substitution is valid because the bound qπ(s,a)≥vπ(s)q_\pi(s,a) \ge v_\pi(s) holds at every state, not just the starting one, so it can be chained along the entire trajectory generated by following π′\pi'. Because rewards are bounded and γ<1\gamma < 1, this infinite telescoping sum converges and the interchange of limit and expectation is justified. ■\blacksquare

Why iteration terminates at optimality. The theorem shows the value never decreases across an improvement step. Suppose the loop reaches a policy π\pi that is not improved, meaning π\pi is already greedy with respect to its own vπv_\pi:

vπ(s)=max⁡a∑s′p(s′∣s,a)[r(s,a,s′)+γ vπ(s′)]for all s.v_\pi(s) = \max_a \sum_{s'} p(s'\mid s,a)\big[r(s,a,s') + \gamma\, v_\pi(s')\big] \quad \text{for all } s.

This is exactly the Bellman Optimality Equation. Since that equation has a unique solution v∗v_* (a consequence of the Bellman optimality operator being a γ\gamma-contraction in max-norm, hence having a unique fixed point by the Banach fixed-point theorem), vπ=v∗v_\pi = v_* and π\pi is optimal. Finally, termination in finitely many steps follows because: (1) each improvement step either leaves the policy unchanged (in which case we have just shown it is optimal and we stop) or strictly increases v(s)v(s) at some state without decreasing it anywhere; (2) there are only knk^n distinct deterministic policies; (3) a policy that strictly improves can never recur, since its value is strictly larger than at any earlier visit. Hence the sequence of distinct policies visited is finite, and the loop must halt at an optimal policy.

Tradeoffs. Policy iteration requires a full model — it is not directly applicable when pp and rr are unknown (that setting motivates temporal-difference and Monte Carlo control methods). Running policy evaluation to full convergence at every outer step is often wasteful: most of the benefit of evaluation is captured in the first few sweeps, which is precisely the insight exploited by Value Iteration, where evaluation is truncated to a single sweep.

Worked example

Consider a tiny 4-state corridor MDP: states {1,2,3,4}\{1, 2, 3, 4\} laid out in a line, with state 4 being terminal (goal, reward 0 on entering, episode ends). From non-terminal states, two actions are available: Right (move toward state 4) and Left (move toward state 1, or stay at state 1 if already there). Every non-terminal transition gives reward −1-1. Discount γ=1\gamma = 1 for this episodic example (standard for finite-horizon-like gridworld/corridor tasks). Transitions are deterministic: Right moves s→s+1s \to s+1 (clamped at 4, terminal), Left moves s→max⁡(s−1,1)s \to \max(s-1, 1).

Initialization. π0(s)=Left\pi_0(s) = \text{Left} for all s∈{1,2,3}s \in \{1,2,3\}, V0(s)=0V_0(s) = 0 for all ss.

Iteration 1 — Evaluate π0\pi_0 (all-Left). Under all-Left, state 1 loops on itself forever incurring −1-1 each step and never reaches the terminal — its true value is −∞-\infty (or, with the iterative sweeps used in practice, a very large negative number after enough sweeps; conceptually this is a bad policy). States 2 and 3 eventually get pushed to state 1 and get stuck there too. Sweeps of iterative policy evaluation drive all three values toward −∞-\infty (or, truncated at some large negative cutoff for illustration), confirming numerically that this policy is bad everywhere.

Improve. For each state, compare q(s,Left)q(s,\text{Left}) against q(s,Right)q(s,\text{Right}) using the (very negative) VV just computed.

stateq(s,Left)=−1+V(left-neighbor)q(s,\text{Left}) = -1+V(\text{left-neighbor})q(s,Right)=−1+V(right-neighbor)q(s,\text{Right}) = -1+V(\text{right-neighbor})new greedy action
1−1+V(1)-1 + V(1) (very negative)−1+V(2)-1 + V(2) (very negative, but less so — closer to goal)Right
2−1+V(1)-1 + V(1)−1+V(3)-1 + V(3)Right
3−1+V(2)-1 + V(2)−1+V(4)=−1+0=−1-1 + V(4) = -1 + 0 = -1Right

Because state 4 is terminal with V(4)=0V(4) = 0 fixed, q(3,Right)=−1q(3, \text{Right}) = -1 is finite and strictly better than looping indefinitely, so Right wins at every state. New policy π1(s)=Right\pi_1(s) = \text{Right} for all ss.

Iteration 2 — Evaluate π1\pi_1 (all-Right). This policy is now deterministic and acyclic (every state marches straight to the terminal), so exact values are computable directly:

V(3)=−1+V(4)=−1,V(2)=−1+V(3)=−2,V(1)=−1+V(2)=−3.V(3) = -1 + V(4) = -1, \quad V(2) = -1 + V(3) = -2, \quad V(1) = -1 + V(2) = -3.

Improve again.

stateq(s,Left)q(s,\text{Left})q(s,Right)q(s,\text{Right})greedy action
1−1+V(1)=−1+(−3)=−4-1 + V(1) = -1 + (-3) = -4−1+V(2)=−1+(−2)=−3-1 + V(2) = -1+(-2) = -3Right (unchanged)
2−1+V(1)=−4-1 + V(1) = -4−1+V(3)=−2-1 + V(3) = -2Right (unchanged)
3−1+V(2)=−3-1 + V(2) = -3−1+V(4)=−1-1 + V(4) = -1Right (unchanged)

`policy_stable = true` — no action changed. The loop halts. π1\pi_1 is optimal, with v∗(1,2,3)=(−3,−2,−1)v_* (1,2,3) = (-3,-2,-1), matching the obvious ground truth: the shortest path to the goal from state ss takes 4−s4-s steps. This example is deliberately non-symmetric: the starting policy (all-Left) is close to worst-case, forcing a real improvement step rather than a trivial no-op, and the terminal is off-center enough that the value gradient is easy to sanity-check by hand.

Common pitfalls & traps

PitfallWhy it happensFix
Treating truncated evaluation as exactStopping the inner loop early (as in value iteration) gives an approximate vv, and applying the improvement theorem to it without care can, in rare edge cases, seem to violate monotonic improvementEither run evaluation to convergence, or recognize you're doing a generalized policy iteration and rely on the separate convergence theory for that variant
Tie-breaking oscillationIf `argmax` is not tie-broken deterministically, the greedy action can flip between two equally good choices every outer step, never satisfying `policy_stable`Break ties by preferring the previous action, or by a fixed lexicographic rule
Ignoring non-terminating (transient) policiesAn initial policy that never reaches any terminal or absorbing state (like all-Left in the example) can make policy evaluation diverge or need very many sweepsCap the number of evaluation sweeps, or detect divergence and treat the value as −∞-\infty conceptually
Forgetting the model requirementPolicy iteration assumes known p(s′∣s,a)p(s'\mid s,a) and r(s,a,s′)r(s,a,s'); applying it directly to a black-box environment silently produces nonsenseUse model-free analogues (SARSA, Q-learning) or first fit a model
Confusing vπv_\pi with v∗v_* mid-loopEvery intermediate VV in the loop is the value of the current policy, not yet the optimal value function, until the very last iterationOnly trust VV as v∗v_* after `policy_stable` is confirmed

Pioneers & history

Origins · 1 minRonald A. Howard and how this was discovered
RA
Ronald A. Howard
1927–2023 · American operations researcher, MIT and Stanford

Ronald Howard formalized policy iteration in his 1960 book Dynamic Programming and Markov Processes, building on Richard Bellman's dynamic-programming framework. Howard's contribution was to show that alternating an exact evaluation step with a greedy improvement step yields a finitely terminating algorithm for solving Markov decision processes — a sharper computational claim than Bellman's original value-iteration-style successive approximation, which converges only in the limit. Howard went on to found the field of decision analysis, applying these ideas well beyond operations research, into medical and business decision-making.

History
1957
— Richard Bellman publishes Dynamic Programming, introducing the Bellman equation and value iteration for MDPs.
1960
— Ronald Howard formalizes policy iteration in Dynamic Programming and Markov Processes, proving finite termination via the policy improvement theorem.
1977
— Martin Puterman and collaborators refine convergence and complexity analyses of policy iteration, connecting it to linear programming formulations of MDPs.
1988
— Richard Sutton introduces temporal-difference learning, setting up the model-free analogues of policy evaluation used in modern reinforcement learning.
1998
— Sutton and Andrew Barto's Reinforcement Learning: An Introduction frames policy iteration as the organizing template — "generalized policy iteration" — underlying almost all RL algorithms.

Where this matters

Policy iteration is the backbone of classical operations-research tools for inventory control, queueing systems, and maintenance scheduling, where transition models are known from physical or statistical modeling and exact solutions are required. In robotics, it underlies motion-planning solvers that treat navigation as an MDP with a known map, computing provably shortest-expected-cost policies before deployment. In finance, portfolio-rebalancing and options-pricing models formulated as MDPs use policy-iteration-style solvers when transition dynamics can be estimated from historical data. Conceptually, its evaluate-then-improve template — termed generalized policy iteration by Sutton and Barto — is the schema instantiated by nearly every practical reinforcement-learning algorithm: SARSA and Q-learning approximate the evaluation and improvement steps with sampled transitions instead of a known model; actor-critic architectures make the split explicit, with the critic performing (approximate) policy evaluation and the actor performing (approximate) policy improvement; and even large-scale deep-RL systems like AlphaGo Zero's self-play loop can be read as policy iteration where evaluation is done via Monte Carlo tree search and improvement is distilled back into a neural network policy. Anyone building on Value Iteration, actor-critic methods, or model-based RL is, at bottom, building on this same alternation.

Ask the tutor: How does modified policy iteration (truncating evaluation to kk sweeps) formally interpolate between policy iteration and value iteration?

Does policy iteration always converge faster than value iteration in wall-clock time?

Not necessarily. Policy iteration usually needs far fewer outer iterations because each one fully solves the current policy's values, giving a bigger jump toward optimality per step. But each outer iteration is expensive, since policy evaluation itself is an inner loop run to convergence. Value iteration instead does one cheap sweep per outer iteration but needs many more of them. Whether policy iteration or value iteration is faster in practice depends on the MDP's structure: policy iteration tends to win when evaluation converges quickly (small effective mixing time), while value iteration tends to win on very large state spaces where even one full evaluation phase is costly. Modified policy iteration, which truncates evaluation to a fixed number of sweeps, interpolates between the two and is often the fastest in practice.

What happens if the initial policy is deterministic but very bad, like a policy with cycles that never reach a goal?

Policy evaluation is still well-defined as long as the value function stays finite, which requires either a discount factor γ<1\gamma < 1 or, in the undiscounted episodic case, a guarantee that all policies eventually terminate. If a policy can cycle forever without reaching a terminal state under γ=1\gamma = 1, its value at looping states is formally −∞-\infty (for a cost-per-step of −1-1), which iterative evaluation approaches asymptotically rather than reaching exactly. In practice this is handled either by using γ<1\gamma < 1 to guarantee convergence, by capping the number of evaluation sweeps, or by adding an artificial upper bound on episode length. The worked example in this article deliberately starts from such a bad, looping policy to show the algorithm correctly escapes it.

Is the policy produced by policy iteration unique?

The optimal value function v∗v_* is always unique, since it is the unique fixed point of the Bellman optimality operator, a contraction mapping. The optimal policy, however, need not be unique: if two actions achieve the same maximum in the Bellman optimality equation at some state, either is optimal, and different tie-breaking rules in the `argmax` step can lead policy iteration to output different (but equally optimal) policies. This is common in symmetric MDPs, such as a gridworld where multiple shortest paths exist to the same goal.

Can policy iteration be used when the environment is stochastic, not deterministic?

Yes — the algorithm as stated already handles stochastic transitions via the general form p(s′∣s,a)p(s' \mid s, a), and the worked example's deterministic corridor was chosen only for hand-computability. In a stochastic MDP, the evaluation backup becomes an expectation over possible next states, and the improvement step computes qπ(s,a)q_\pi(s,a) as an expectation as well. Nothing in the correctness proof relies on determinism; the policy improvement theorem and the contraction argument for the Bellman operators hold for arbitrary transition kernels, as long as rewards are bounded and γ<1\gamma < 1 (or the episodic analogue holds).

How does policy iteration relate to generalized policy iteration (GPI)?

Generalized policy iteration is the broader pattern of any interacting evaluation and improvement processes, not necessarily run to convergence at each step and not necessarily alternating in strict lock-step. Classical policy iteration is the special case where evaluation is run to full convergence before every improvement step. Value iteration is the opposite extreme, where evaluation is truncated to exactly one sweep. Nearly every reinforcement-learning algorithm — SARSA, Q-learning, actor-critic methods — can be seen as an instance of GPI with approximate, sampled, or asynchronous evaluation and improvement steps, which is why Sutton and Barto treat GPI as the organizing concept of the entire field.

What is the actual computational bottleneck in policy iteration for large state spaces?

The dominant cost is usually the policy evaluation phase, since each sweep touches every state and, for each, sums over all possible next states — a cost of O(n⋅b)O(n \cdot b) per sweep where bb is the branching factor, repeated for many sweeps until convergence. For large or continuous state spaces this becomes infeasible, which motivates two directions: truncating evaluation (yielding modified policy iteration or value iteration), and replacing exact tabular evaluation with function approximation (yielding fitted value iteration and, ultimately, deep reinforcement learning methods that never store VV or π\pi as explicit tables at all).

Why does the improvement step use q_π rather than just re-checking v_π directly?

The value function vπ(s)v_\pi(s) only tells you how good it is to follow π\pi from state ss; it does not, by itself, compare alternative actions at ss. The action-value function qπ(s,a)=∑s′p(s′∣s,a)[r(s,a,s′)+γvπ(s′)]q_\pi(s,a) = \sum_{s'} p(s'|s,a)[r(s,a,s') + \gamma v_\pi(s')] explicitly evaluates the consequence of trying a different action aa once at ss and then reverting to π\pi afterward. Taking the argmax over aa of qπ(s,a)q_\pi(s,a) is exactly what asks "is there a one-step deviation from π\pi that does better?" — which is the operational meaning of policy improvement.

Before this

Where this leads