Value Iteration
Back up the best action until the values settle.
Policy iteration evaluates a whole policy before it changes anything. Value iteration never holds a policy at all: it backs up the best action in every state, sweep after sweep, and the values close in on by a factor per sweep. After sweeps it has solved the -step problem exactly, and its greedy policy is usually optimal long before the values settle, which is why the right question is not "have the values converged?" but "is the policy good enough?"
What you’ll learn
- Derive the Bellman optimality backup, implement value iteration in state-value and action-value form, and prove that it converges at rate in the max norm.
- Interpret the -th iterate as the optimal -step value, and solve finite-horizon problems exactly by backward induction.
- Run value iteration by hand on the two-state world, distinguishing the actions a backup used from the greedy policy of its result.
- Prove that stopping when makes the greedy policy -optimal, and estimate how many sweeps that takes.
Backing up the best action
The optimal values satisfy the Bellman optimality equation: a state is worth the best action's reward plus the discounted value of where it leads. Write the right-hand side as an operator on value tables, the optimality Bellman operator
so that is its fixed point, . Value iteration applies the operator over and over, , from any starting table . Each application is one sweep of backups, one per state. No policy is stored anywhere: the max inside every backup does the improving.
For any two functions of the action, the maxima differ by at most the largest difference: if peaks at , then , and symmetrically.
Apply it in state , with and the bracketed lookaheads under and . The rewards cancel, and what is left is times an average of differences:
Take the maximum over . Two fixed points would be at most times their own distance apart, so there is at most one, and the Banach fixed-point theorem supplies it (Bellman Operators & Contraction). Since , every sweep shrinks the error by at least :
import numpy as np
def value_iteration(P, r, gamma, eps=1e-3):
"""P[s, a, s2] transition probabilities, r[s, a] expected rewards.
Returns values and a greedy policy that is eps-optimal."""
V = np.zeros(r.shape[0])
while True:
Q = r + gamma * P @ V # S x A one-step lookaheads
V_new = Q.max(axis=1) # the backup: best action everywhere
if np.abs(V_new - V).max() < eps * (1 - gamma) / (2 * gamma):
return V_new, (r + gamma * P @ V_new).argmax(axis=1)
V = V_newA sweep costs with a dense model, or when each state–action pair has at most successors. The stopping test is derived in When to stop; the policy returned is greedy for the latest values.
Worked example: the two-state world
Take the two-state world of the earlier lessons with : in , stay pays 1 and stays, go pays 0 and moves to ; in , stay pays 2 and stays, go pays 0 and moves to . Start from . The first backup gives : with nothing known about the future, the immediate rewards decide.
| Backup | In : stay / go, using | Action used in | Greedy policy for | |
|---|---|---|---|---|
| 1 | 1 / 0 | stay | (stay, stay) | |
| 2 | 1.9 / 1.8 | stay | (go, stay) | |
| 3 | 2.71 / 3.42 | go | (go, stay) | |
| 4 | 4.078 / 4.878 | go | (go, stay) | |
| 17.2 / 18 | go | (go, stay) |
Think first Every backup comes with two policies: the actions that attained the max while computing , and the greedy policy for the new values . How are they related?
The actions used in backup are exactly the greedy policy for , one table behind. Backup 2 used stay in because stay is greedy for ; the greedy policy for is go, and backup 3 uses it. Stopping rules certify one of these two policies at a time, so it pays to say which one you mean (Error Bounds & Stopping Rules bounds both).
From the distance to is 20, and here it shrinks by exactly the factor 0.9 per backup. After how many backups is it below 0.01 for the first time?
Show the answer
73
Solve : , so 73 backups. In , stay is used throughout, so loses exactly the factor 0.9 per backup. The policy was right after 2.
Looking k steps ahead
What is ? Unrolling the backups answers it exactly: is the largest expected discounted reward obtainable in steps from , with paid as a terminal value after the last step. For this is the best immediate reward plus . If is the best -step value from every state, then the best -step plan chooses its first action well and continues optimally from wherever it lands, which is worth . Bellman's principle of optimality is this induction.
| Steps to go | Best first action in | ||
|---|---|---|---|
| 1 | 1 | 0 | stay |
| 2 | 1.9 | 1.8 | stay |
| 3 | 2.71 | 3.42 | go |
| 4 | 4.078 | 4.878 | go |
An agent in has exactly two steps left, with . Why is stay optimal, although pays twice as much per step?
- Going earns 0 now and a single 2 one step later, worth , against for staying twice
- With , rewards two steps away are worth nothing
- In finite-horizon problems the greedy action is always the one with the larger immediate reward
- It is not: go is optimal at every horizon, as in the discounted problem
Show the answer
A. Going earns 0 now and a single 2 one step later, worth , against for staying twice
With two steps left, going spends one of them earning nothing and leaves only one step in . The comparison flips at three steps left ( against ), which is why finite-horizon optimal policies depend on the time remaining.
When to stop
Values converge only in the limit, so value iteration needs a stopping rule, and the useful rule speaks about the policy you will act with. Write for the largest change in a sweep. Because the changes shrink geometrically, the one you just saw bounds all the changes still to come:
Greediness means , and . Compare , the fixed point of , with by inserting :
So the policy's true values lie within of , and by the a posteriori bound so does . The triangle inequality finishes the proof:
The rule is a certificate, not a detector. In the two-state world with the threshold is , first met at backup 57, while the greedy policy has been optimal since backup 2. The guarantee is worst-case, and it holds in every MDP.
How many sweeps does it take? From with , the starting error is at most , so the a priori bound gives an -accurate table after about sweeps, using . The effective horizon sets the price: needs roughly ten times as many sweeps as . Policy iteration needs far fewer iterations, each costing a linear solve (Policy Iteration).
Value iteration with stops after a sweep whose largest change is . What does the proof above guarantee about the greedy policy for the final values?
- It loses at most in any state
- It loses at most in any state
- It loses at most in any state
- Nothing, until a sweep changes no value at all
Show the answer
C. It loses at most in any state
: the factor covers the changes still to come, and it appears twice because and can lie on opposite sides of . A sharper analysis in Error Bounds & Stopping Rules improves this to for this policy.
Variations on the backup
Action values. Value iteration can store instead of :
Started from it produces , so it converges at the same rate, at the cost of numbers of memory instead of . What it buys is that acting greedily, , no longer needs the model. Replace the expectation by one sampled transition and you get Q-learning (SARSA & Q-Learning).
Other orders. Overwriting values in place, or backing up states in any order that visits each one infinitely often, also converges to . Asynchronous dynamic programming uses this freedom to put the backups where they matter (Asynchronous & Prioritized Dynamic Programming).
Between value and policy iteration. A value-iteration backup is one sweep of evaluation for the policy that is greedy right now, so value iteration is generalized policy iteration with the least possible evaluation; modified policy iteration does sweeps per improvement (Modified & Generalized Policy Iteration).
| Setting | What the iterates mean | Convergence |
|---|---|---|
| Finite horizon , any | is the optimal value with steps to go | Exact after backups; the optimal policy depends on |
| Discounted, | Optimal -step values with terminal value | from any start |
| Episodic, | Optimal values of reaching a terminal state | Converges if some policy terminates with probability 1 from every state and every policy that does not has value somewhere (Bertsekas & Tsitsiklis, 1996) |
| Average reward | grows like times the optimal gain | Relative value iteration subtracts a reference value each sweep (Average-Reward Dynamic Programming) |
Deeper: the Fixed Point lessons
Key takeaways
- Value iteration applies the optimality backup to every state, sweep after sweep. The max is non-expansive, so is a -contraction and .
- is the optimal -step value with terminal value : finite-horizon problems are solved exactly by backups (backward induction), with a policy that depends on the steps remaining.
- The greedy policy usually settles long before the values: after 2 backups against 73 in the two-state world. The actions used by backup are greedy for , one table behind.
- Stopping when makes the greedy policy -optimal; reaching that takes about sweeps.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Value iteration on an MDP with starts with . What does the contraction property guarantee after 20 sweeps?
- , because 20 sweeps reach the effective horizon
Show the answer
A.
Each sweep multiplies the max-norm error by at most . With close to 1, value iteration needs many multiples of the effective horizon : about sweeps for an error .
A problem ends after exactly steps. What does value iteration from deliver after 10 backups?
- An approximation whose error is about times the initial error
- One stationary policy that is optimal at every step
- The exact optimal values, and an optimal policy that may depend on how many steps remain
- Nothing reliable unless
Show the answer
C. The exact optimal values, and an optimal policy that may depend on how many steps remain
is the optimal value with steps to go, so 10 backups give the exact answer, and , greedy for , is the optimal action with steps left. In the two-state world it stays in with one or two steps left and goes with three or more.
Why run value iteration on action values instead of state values , at times the memory?
- It converges at a faster rate
- The greedy policy can then be read off without the model, which is also what lets sampled versions such as Q-learning work
- It avoids the max over actions
- It is required when
Show the answer
B. The greedy policy can then be read off without the model, which is also what lets sampled versions such as Q-learning work
With stored, needs no transition probabilities, so an agent can act, and learn from sampled transitions, without a model. The rate is the same , because .
Backup computes . Which policy do the actions attaining these maxima form?
- The greedy policy for
- An optimal policy, once
- The policy evaluated by the previous sweep
- The greedy policy for
Show the answer
D. The greedy policy for
The max compares actions using , so its maximizers are greedy for , one table behind the new values. In the two-state world, backup 2 used stay in (greedy for ), while the greedy policy for already goes.
Go deeper
- Error Bounds & Stopping Rules · From the size of the last update to a guarantee on the greedy policy.
- Linear Programming for MDPs · The primal over values, the dual over occupancy measures, and what each reveals.
- Planning in Huge MDPs: Sparse Sampling · Near-optimal planning in time independent of the number of states.
Further reading
- R. Bellman, Dynamic Programming (Princeton University Press, 1957).
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 4 (MIT Press, 2nd ed., 2018).
- M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 4 and 6 (Wiley, 1994).
- D. P. Bertsekas & J. N. Tsitsiklis, Neuro-Dynamic Programming, ch. 2 (Athena Scientific, 1996).