If you know how the world works, you can plan. The Bellman operators from the previous note are things you can compute: for a given value vector, run one backup over every state and you have applied or . Dynamic programming is the family of algorithms that apply those backups repeatedly and use the contraction property to argue that they get where they are going.
There are two basic moves. Evaluation takes a policy and computes its value function. Improvement takes a value function and produces a better policy by acting greedily with respect to it. Policy iteration alternates the two moves to completion. Value iteration collapses them into a single backup. Almost every practical reinforcement learning algorithm is some approximation of this alternation, which is why Sutton and Barto call the general pattern generalized policy iteration.
Policy evaluation
Given , the value function is the unique fixed point of . There are two ways to compute it.
Direct solve. . This is exact and costs a linear solve in unknowns, roughly , which is fine for thousands of states and hopeless beyond that.
Iterative policy evaluation. Start from any and iterate
Each sweep costs in the dense case. Since is a contraction, , and to reach accuracy from an initial error takes
sweeps. The dependence on inside the log is mild; the in front is the real cost of a long horizon.
In place sweeps. The update above uses on the right hand side for every state, which requires two arrays. Using the newest available values instead, so that a state updated early in the sweep is already seen by states updated later, is the Gauss Seidel variant. It converges too, typically faster, and needs one array. The proof is a small modification: each in place update is still a backup of some vector that is componentwise between and , and monotonicity carries the contraction argument through.
The policy improvement theorem
This is the theorem that makes policy iteration work, and it is the one result in this note that is not simply the contraction property again.
Theorem. Let and be two deterministic policies such that, for every state ,
Then for every . Moreover, if the hypothesis holds with strict inequality at some state, the conclusion holds with strict inequality at that state.
In words: if switching to for one step and then following is at least as good as following throughout, then following throughout is at least as good as following throughout. One step of improvement, made everywhere, compounds into improvement of the whole policy.
Proof. Start from the hypothesis and expand one step:
So componentwise. Apply to both sides. By monotonicity of the operator,
The right hand side converges to the fixed point of , which is , as . Componentwise inequalities survive limits, so .
For strictness, if at some , then the chain of inequalities is strict at that state from the first step on, and .
The proof is three lines once the operator language is available, and it is the reason for setting up that language. The same argument written out with nested expectations over trajectories, which is how Sutton and Barto present it, is a page long and harder to check.
Corollary (greedy improvement). Let be greedy with respect to , so . Then , the hypothesis holds, and is at least as good as . If is no better than anywhere, then , so is the fixed point of the optimality operator and is already optimal.
Policy iteration
Alternate evaluation and greedy improvement:
- Initialize arbitrarily.
- Evaluate. Compute exactly, by a linear solve or by iterating to convergence.
- Improve. Set , breaking ties in favor of the current action.
- If , stop. Otherwise return to step 2.
Theorem. Policy iteration terminates after finitely many iterations, and the policy it terminates with is optimal.
Proof. By the corollary, each improvement step produces a policy at least as good as the last, componentwise. If the policy changes at step 3, then the greedy action is strictly better than the current action at some state (the tie breaking rule ensures a change only happens on strict improvement), so by the strict part of the theorem . The sequence of value functions is therefore strictly increasing in the componentwise partial order as long as the policy keeps changing, and no policy can appear twice. There are only deterministic policies, so the loop terminates. When it terminates, means the greedy policy with respect to is itself, which by the corollary means is optimal.
The bound is astronomically pessimistic. In practice policy iteration converges in a handful of iterations, and there are results (Ye 2011) showing the number of iterations is polynomial in , and for a fixed discount. The per iteration cost is dominated by the evaluation step.
Value iteration
Instead of evaluating each policy to convergence before improving, do one backup and improve immediately. Since the greedy improvement of followed by one step of evaluation is exactly , this is
which is simply iterating the optimality operator. No explicit policy is kept during the iteration; one is read off at the end by acting greedily with respect to the final .
Convergence is Corollary 2 of the previous note: from any .
Stopping rule. Run until . Then
Proof. For any , by the triangle inequality and the contraction,
Rearranging, , and dividing through gives the bound.
How good is the greedy policy from an approximate value?
Value iteration stops with an approximate , not , and the policy actually used is the greedy one with respect to . The final question is how much that costs.
Theorem. Let be any value vector and a greedy policy with respect to . Then
Proof. Since is greedy for , . Then
For the first term, , so it equals . For the second, , so it is . Substituting and collecting the terms on the left,
Combined with the stopping rule, stopping value iteration at tolerance produces a policy whose value is within of optimal. The is the price of extracting a policy from an approximate value function, and it is the reason the discount factor is so sensitive in practice: at the factor is ten thousand.
There is a second, more encouraging fact hiding here. Because there are finitely many deterministic policies and is fixed, once is close enough to the greedy policy is exactly optimal, not just approximately. Value iteration typically identifies the optimal policy long before the value estimates have converged.
Asynchronous and prioritized variants
Nothing in the convergence proofs requires that every state be updated in each sweep. As long as every state continues to be updated infinitely often, the iterates still converge to . This licenses asynchronous dynamic programming, where states are updated in any order, possibly focusing on the ones whose values are changing most (prioritized sweeping) or the ones the agent is actually visiting (real time dynamic programming). The proof of convergence for the asynchronous case is a small extension of the contraction argument: after enough updates that every state has been touched at least once, the sup norm error has shrunk by at least a factor .
The cost of the model
Every backup in this note sums over with weights . That requires the transition kernel as a table, or at least as something you can evaluate, and it makes every sweep scale with the number of states. Two things go wrong at scale. The state space becomes too large to enumerate, which is the curse of dimensionality, and the model is unavailable because the environment is a physical system, a game engine or a market, which is the more common problem. Both push toward the same idea: replace the expectation over with a sample of drawn by actually taking the action. That is Temporal difference learning.
References
- Sutton, Barto. Reinforcement Learning: An Introduction, 2nd ed. MIT Press, 2018, chapter 4.
- Puterman. Markov Decision Processes. Wiley, 1994, chapter 6.
- Bertsekas, Tsitsiklis. Neuro-Dynamic Programming. Athena Scientific, 1996, chapter 2.
- Ye. The Simplex and Policy-Iteration Methods Are Strongly Polynomial for the Markov Decision Problem with a Fixed Discount Rate. Mathematics of Operations Research, 2011.