Chapter 3: Dynamic Programming
Solve known MDPs by planning: iterative policy evaluation, policy improvement and the policy improvement theorem, policy iteration, value iteration, and the generalized policy iteration pattern that underlies every later algorithm.
- Topics
- 5
- Videos
- 5
- Min read
- 19
Chapter Overview
Dynamic programming (DP) answers the question Chapter 2 set up: given the transition dynamics, how do we actually compute the optimal policy? The answer is to turn the Bellman equations into assignment statements and apply them repeatedly until the values stop changing. Because the Bellman operators are contractions, this is guaranteed to converge.
DP has a strong assumption — you need a complete model of the environment — and a fatal weakness: it sweeps over every state, so it is unusable when the state space is large. It is nonetheless the most important chapter in the course. Every model-free method that follows is best understood as a way of approximating a DP update using sampled experience instead of a known model, and the alternation between evaluation and improvement introduced here is the skeleton of Q-learning, actor-critic and PPO alike.
Chapter Roadmap
Click any topic to jump in
Policy Evaluation
Repeatedly apply the Bellman expectation update to compute how good a fixed policy is.
Policy Improvement
Acting greedily with respect to a policy's own value function is guaranteed never to make it worse.
Full evaluation or a single sweep are just two points on a spectrum.
Policy Iteration
Alternate full evaluation and greedy improvement until the policy stops changing — converges in surprisingly few iterations.
Value Iteration
Collapse evaluation to a single sweep by folding the max directly into the update.
Generalized Policy Iteration
The unifying pattern: any interleaving of partial evaluation and partial improvement converges to the optimum.
Topics
5 topics in this chapter
Policy evaluation, also called the prediction problem, computes for a given policy. Chapter 2 showed this is a linear system solvable by matrix inversion, but at cubic cost in the number of states, which rules it out for anything real. The practical alternative is to treat the Bellman expectation equation as an assignment rather than an identity: initialize the values arbitrarily, apply the equation to every state, and repeat. The sequence converges to from any starting point, and it does so geometrically. The reason it works is worth stating precisely, because the same argument underwrites every algorithm in this chapter. The Bellman expectation operator is a contraction in the max norm: applying it to any two value functions brings them closer together by at least a factor . Since is a fixed point of that operator, repeated application drives any initial guess toward it, with the error shrinking by per sweep and the number of sweeps needed for a given accuracy scaling like .
Definition
Policy evaluation is the problem of computing the value function for a fixed policy. Solved iteratively, it repeatedly applies the Bellman expectation equation as an update rule until the values stop changing, converging geometrically from any initialization because the operator is a contraction.
In this topic
- 1Iterative Policy Evaluation
- 2When to Stop
Iterative Policy Evaluation
Each sweep replaces the value of every state with the one-step lookahead computed from the previous sweep's values. Successive approximation works because the Bellman expectation operator is a -contraction in the max norm: every application shrinks the distance to the true by at least a factor . Convergence is therefore geometric with a rate set entirely by the discount factor, and it is guaranteed regardless of initialization — a useful practical property, since it means a warm start from a previous policy's values is always safe and usually much faster than starting from zero.
The contraction property is . Applying it times gives error times the initial error, so the number of sweeps needed for a fixed accuracy grows like .
Should the sweep use the previous iteration's values throughout (synchronous), or the freshest values available (in-place)?
When to Stop
In theory convergence takes infinitely many sweeps; in practice you stop when the largest value change across a sweep falls below a threshold. The genuinely useful insight is that you rarely need accurate values at all — you need values accurate enough that the greedy policy derived from them is correct. Because the greedy step depends only on the ordering of action values rather than their magnitudes, that policy typically stabilizes long before the numbers do. The gap between good enough to act on and numerically converged is the single most exploited fact in dynamic programming, and value iteration is built directly on it.
If the current estimate is within of the true values everywhere, every one-step action value computed from it is within of the truth, because the estimate enters only through the term . So whenever the best action at a state beats the runner-up by more than , the greedy choice there is already correct. Orderings settle once drops below half the smallest action gap, which is usually long before itself is small.
In the 4x4 gridworld from Sutton & Barto, how many sweeps does the greedy policy need to become optimal, versus how many the values need to converge?
Related Problems on PixelBank
Having evaluated a policy, the natural next question is whether it can be beaten. The policy improvement theorem gives a strikingly strong answer: construct a new policy by acting greedily with respect to the old policy's value function, and the new policy is at least as good in every state, strictly better in at least one unless the old policy was already optimal. The subtlety worth pausing on is the gap between what is assumed and what is concluded. The premise compares only a single-step deviation — what happens if you act greedily once and then revert to the old policy. The conclusion is about following the new policy forever. The proof closes that gap by unrolling the inequality, replacing one step at a time and showing the bound holds at each substitution. That is why a purely local improvement yields a global guarantee, and it is the reason greedy improvement is safe rather than merely plausible.
Definition
Policy improvement constructs a new policy that acts greedily with respect to the current policy's value function. The policy improvement theorem guarantees the result is at least as good in every state, and strictly better somewhere unless the original policy was already optimal.
In this topic
- 1The Policy Improvement Theorem
- 2When Improvement Stops
The Policy Improvement Theorem
The theorem states that if for all states, then for all states. The subtle part is that the premise only compares a single-step deviation while the conclusion is about following forever. The proof unrolls the inequality repeatedly, replacing one step at a time, which is why a purely local improvement yields a global guarantee. This matters practically because it means greedy improvement can never make things worse — an unusual property in optimization, where a locally attractive step routinely degrades the global objective.
The proof is one inequality applied over and over: . The inside is again at most , so substitute and expand. After substitutions the first actions follow and only the tail still uses ; that tail is weighted by , so in the limit the right-hand side is exactly . TRPO's monotonic-improvement bound is the same argument with an extra penalty for how far moves from .
The greedy step only looks one action ahead. Why doesn't that produce a short-sighted policy?
When Improvement Stops
If a greedy step leaves the policy unchanged, then satisfies the Bellman optimality equation, and since that equation has a unique solution, must be optimal. This gives a clean termination test — stop when the policy is stable — and it also explains why the algorithm cannot stall at a suboptimal fixed point: the only policy that is greedy with respect to its own value function is the optimal one. That is a much stronger guarantee than gradient methods offer, where a zero gradient signals only a local optimum and says nothing about the global one.
If the greedy step returns itself, then for every state . That is the Bellman optimality equation, whose unique solution is , so . Termination is guaranteed because values never decrease and a finite MDP has at most deterministic policies; a strictly improving sequence can never revisit one.
Could policy improvement cycle forever between two policies of equal value?
Related Problems on PixelBank
Policy iteration simply alternates the two previous topics: evaluate the current policy to convergence, act greedily to obtain a better one, and repeat. Because each improvement strictly increases value unless the policy is already optimal, and there are finitely many deterministic policies in a finite MDP, the algorithm terminates at the exact optimum after a finite number of steps. What surprises people is how few steps that is. Policy iteration typically converges in fewer than ten improvements even on problems with hundreds of states, because a single greedy sweep changes the action at many states simultaneously rather than editing one decision at a time. Each iteration is therefore an enormous jump through policy space. The price is that every iteration requires a full policy evaluation, itself many sweeps over the entire state space. So policy iteration performs very few, very expensive iterations — exactly the tradeoff that value iteration inverts, and the reason both algorithms remain worth knowing.
Definition
Policy iteration alternates full policy evaluation with greedy policy improvement until the policy stops changing. It terminates at the exact optimum in finitely many iterations, typically very few, because each greedy sweep changes the action at many states at once.
In this topic
- 1The Algorithm and Its Cost
- 2Modified (Truncated) Policy Iteration
The Algorithm and Its Cost
Policy iteration converges in remarkably few iterations — often fewer than ten even on large problems — because each greedy step changes the policy at many states simultaneously. The cost is that every iteration requires a full policy evaluation, itself many sweeps over the state space. So it performs few, very expensive iterations, which is exactly the tradeoff value iteration inverts. The empirical observation that the iteration count is roughly independent of state-space size is what keeps the algorithm competitive: the per-iteration cost grows with the problem, but the number of iterations largely does not.
Each iteration costs one evaluation, which is a linear solve of order or a run of sweeps of order each, plus one greedy pass. The iteration count is what stays small. Ye (2011) proved that for a fixed discount it is bounded by a polynomial in the numbers of states and actions, growing like times a logarithm, and in practice it is far below even that bound. On Jack's Car Rental the evaluations took 126, 91, 80, 45 and 14 sweeps, shrinking as the policies stopped changing.
Jack's Car Rental has about 441 states. Policy iteration solves it in four policy improvements. Why so few?
Modified (Truncated) Policy Iteration
Nothing requires evaluation to run to convergence before improving. Truncating it after a fixed number of sweeps gives modified policy iteration, which still converges to the optimum. Setting the budget to a single sweep recovers value iteration exactly; setting it to infinity recovers classic policy iteration. The whole family is therefore parameterized by one quantity — how much evaluation you are willing to pay for per improvement — and recognizing that the two named algorithms are endpoints of a spectrum rather than rival methods makes the design space much easier to reason about.
Write the update as followed by , where is one sweep of policy evaluation. With this is , value iteration. As the inner loop converges to , which is policy iteration. Puterman and Shin proved convergence for every fixed , so the budget changes only the cost, never the answer.
How would you choose the evaluation budget in practice?
Value iteration takes the truncation idea to its limit: perform exactly one sweep of evaluation between improvements, and fold the greedy maximization directly into the update. The result is a single elegant assignment derived from the Bellman optimality equation, with no explicit policy maintained at all until the very end. Like its expectation counterpart, the optimality operator is a -contraction, so the values converge geometrically to from any initialization, and the optimal policy is extracted once at the end by a single greedy pass. There is also a genuine stopping guarantee attached, which is unusual and worth knowing: if the largest change in a sweep falls below , the greedy policy's value is within of optimal. In practice that bound is conservative — the greedy policy typically becomes optimal far earlier than the values converge — but it means value iteration can be stopped on a numerical criterion with a real correctness statement behind it.
Definition
Value iteration performs a single evaluation sweep per improvement by folding the max over actions directly into the Bellman update. It converges geometrically to from any initialization, and the optimal policy is extracted at the end with one greedy pass.
In this topic
- 1The Optimality Update
- 2Asynchronous Variants
The Optimality Update
Each sweep replaces every state's value with the best one-step lookahead rather than the policy-weighted average. Like its expectation counterpart, the Bellman optimality operator is a -contraction, so the values converge geometrically to from any initialization. The optimal policy is extracted once at the end by a single greedy pass, which is why no policy is stored during the loop at all. That absence is the algorithm's real economy: it removes the bookkeeping of maintaining and comparing policies, leaving a single array of numbers and one update rule.
Because is a -contraction, . Cutting the initial error by a factor of 1,000 takes 66 sweeps at and 688 at , which is why long horizons make value iteration slow. Undiscounted shortest-path problems can finish much sooner: on the 4x4 gridworld with per step, value iteration reaches the exact after 3 sweeps, one per step of the longest shortest path.
Value iteration converges to only in the limit. When can you safely stop and extract the policy?
Asynchronous Variants
Nothing requires sweeping states in a fixed order or even updating them equally often. Asynchronous dynamic programming updates states one at a time in any order and still converges, provided every state continues to be selected eventually. This matters because it severs the link between making progress and completing a full sweep: computation can be concentrated where it is most useful. On a large problem a full sweep may never finish in reasonable time, yet a well-ordered asynchronous method can produce a good policy over the states that actually matter long before that point.
The max-norm contraction is what makes any order safe. Updating a single state moves its error to at most and leaves the others untouched, so the worst-case error never grows. Bertsekas showed that convergence needs only that every state keeps being updated. Prioritized sweeping ranks states by the size of their pending change, , and updates the largest first.
How does asynchronous DP let you make progress on a problem far too large to sweep even once?
Generalized policy iteration is the name for the pattern every algorithm in this chapter shares: maintain a value function and a policy, make the value function more consistent with the policy, and make the policy more greedy with respect to the value function. Almost every reinforcement learning algorithm, including the deep ones in later chapters, is an instance of it. The two processes pull against each other. Making the value function consistent with the policy usually breaks the policy's greediness; making the policy greedy invalidates the value function. Yet together they settle, for two different reasons. Evaluation is a contraction toward the current policy's value, and improvement never lowers the value of any state, so the pair can only climb until it reaches a policy that is greedy with respect to its own value function, which is by definition optimal. What makes this abstraction worth carrying forward is that none of it depends on dynamic programming. Nothing requires evaluation to be exact, improvement to be fully greedy, or either to use a known model, and every algorithm ahead relaxes one of those.
Definition
Generalized policy iteration is the pattern of maintaining a value function and a policy while making each more consistent with the other. Any interleaving converges to the optimal pair, and the pattern survives when evaluation is approximate, improvement is partial, or the model is unknown.
In this topic
- 1Competing and Cooperating Processes
- 2Why This Pattern Survives Without a Model
Competing and Cooperating Processes
Evaluation and improvement pull in opposite directions. Making the value function consistent with the policy usually breaks the policy's greediness; making the policy greedy invalidates the value function. The alternation still settles, but not because both steps are contractions. Evaluation is one: the Bellman expectation operator pulls toward . Improvement is monotone instead: by the policy improvement theorem it never lowers the value of any state. Values can therefore only rise, and in a finite MDP they stop rising exactly at a policy that is greedy with respect to its own value function, which is optimal. The competition is real locally but harmless globally.
Policy iteration produces a chain , holding state by state, with strict improvement somewhere at each step until the policy stops changing. Every link is a policy, and a finite MDP has finitely many, so the chain must end; it can only end at a policy for which the greedy step changes nothing, which the previous topic showed is optimal.
In what sense do evaluation and improvement 'compete'?
Why This Pattern Survives Without a Model
Generalized policy iteration never actually requires that evaluation be exact, that improvement be fully greedy, or that either use a known model. Those are properties of the dynamic-programming implementation, not of the pattern. Replace exact evaluation with sampled returns and you get Monte Carlo control; replace it with bootstrapped one-step estimates and you get SARSA and Q-learning; replace greedy improvement with a gradient step on a parameterized policy and you get actor-critic and PPO. Recognizing this makes the remaining chapters far easier, because each is a substitution into a structure you already understand.
Approximate policy iteration has a clean error bound. If each evaluation is within of the true and each improvement is exactly greedy, then eventually . Sampled or learned evaluation therefore does not break the pattern; it only limits how close to optimal the final policy can get, and the shows why long horizons make that limit loose.
Given that GPI is so general, what actually distinguishes the algorithms in Chapters 4 through 8?