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.
- Policy iteration alternates two subroutines: policy evaluation (compute exactly) and policy improvement (act greedily with respect to ).
- 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.
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 , an action set , transition probabilities , a reward function , and a discount factor . A policy maps states to (distributions over) actions. The state-value function is the expected discounted return starting from and following thereafter, satisfying the Bellman Optimality Equation's cousin, the Bellman expectation equation:
Policy evaluation is the task of computing for a fixed policy . 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 ).
Policy improvement takes a value function and produces a new, deterministic policy by acting greedily:
Policy iteration is the composition of these two steps, repeated: evaluate to get , improve to get , evaluate to get , improve to get , 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 from the previous policy's values converges the inner loop faster, since is typically close to .
Complexity & correctness
Per-iteration cost. Let and (assume a bounded action set per state). One sweep of policy evaluation costs , where is the branching factor of the transition function (number of possible next states per state–action pair, often treated as in the worst case for a dense MDP, but typically much smaller for structured problems). A single evaluation phase runs some number of sweeps until convergence to tolerance ; empirically is small (tens of sweeps) and is bounded theoretically because the Bellman expectation backup is a -contraction in the max-norm, giving . Policy improvement costs , one -value computation per action per state.
Outer loop cost. The number of outer iterations is bounded by the number of deterministic policies, , 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 , policy iteration is known to run in strongly polynomial time in , , and (a result following from connections to linear programming and simplex-type analyses of MDPs). This is a much sharper guarantee than the naive bound suggests.
Space complexity. for the value function, for the policy, plus the model itself which may require 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 is greedy with respect to , i.e. for all , then for all . Proof sketch. Start from the hypothesis and unroll one step at a time:
Now substitute the same inequality for inside the expectation, and repeat this substitution indefinitely:
Each substitution is valid because the bound holds at every state, not just the starting one, so it can be chained along the entire trajectory generated by following . Because rewards are bounded and , this infinite telescoping sum converges and the interchange of limit and expectation is justified.
Why iteration terminates at optimality. The theorem shows the value never decreases across an improvement step. Suppose the loop reaches a policy that is not improved, meaning is already greedy with respect to its own :
This is exactly the Bellman Optimality Equation. Since that equation has a unique solution (a consequence of the Bellman optimality operator being a -contraction in max-norm, hence having a unique fixed point by the Banach fixed-point theorem), and 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 at some state without decreasing it anywhere; (2) there are only 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 and 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 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 . Discount for this episodic example (standard for finite-horizon-like gridworld/corridor tasks). Transitions are deterministic: Right moves (clamped at 4, terminal), Left moves .
Initialization. for all , for all .
Iteration 1 — Evaluate (all-Left). Under all-Left, state 1 loops on itself forever incurring each step and never reaches the terminal — its true value is (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 (or, truncated at some large negative cutoff for illustration), confirming numerically that this policy is bad everywhere.
Improve. For each state, compare against using the (very negative) just computed.
| state | new greedy action | ||
|---|---|---|---|
| 1 | (very negative) | (very negative, but less so — closer to goal) | Right |
| 2 | Right | ||
| 3 | Right |
Because state 4 is terminal with fixed, is finite and strictly better than looping indefinitely, so Right wins at every state. New policy for all .
Iteration 2 — Evaluate (all-Right). This policy is now deterministic and acyclic (every state marches straight to the terminal), so exact values are computable directly:
Improve again.
| state | greedy action | ||
|---|---|---|---|
| 1 | Right (unchanged) | ||
| 2 | Right (unchanged) | ||
| 3 | Right (unchanged) |
`policy_stable = true` — no action changed. The loop halts. is optimal, with , matching the obvious ground truth: the shortest path to the goal from state takes 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
| Pitfall | Why it happens | Fix |
|---|---|---|
| Treating truncated evaluation as exact | Stopping the inner loop early (as in value iteration) gives an approximate , and applying the improvement theorem to it without care can, in rare edge cases, seem to violate monotonic improvement | Either 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 oscillation | If `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) policies | An 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 sweeps | Cap the number of evaluation sweeps, or detect divergence and treat the value as conceptually |
| Forgetting the model requirement | Policy iteration assumes known and ; applying it directly to a black-box environment silently produces nonsense | Use model-free analogues (SARSA, Q-learning) or first fit a model |
| Confusing with mid-loop | Every intermediate in the loop is the value of the current policy, not yet the optimal value function, until the very last iteration | Only trust as after `policy_stable` is confirmed |
Pioneers & history
Origins · 1 minRonald A. Howard and how this was discovered
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.
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 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 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 , its value at looping states is formally (for a cost-per-step of ), which iterative evaluation approaches asymptotically rather than reaching exactly. In practice this is handled either by using 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 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 , 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 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 (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 per sweep where 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 or as explicit tables at all).
Why does the improvement step use q_π rather than just re-checking v_π directly?
The value function only tells you how good it is to follow from state ; it does not, by itself, compare alternative actions at . The action-value function explicitly evaluates the consequence of trying a different action once at and then reverting to afterward. Taking the argmax over of is exactly what asks "is there a one-step deviation from that does better?" — which is the operational meaning of policy improvement.