When the model is known, the Bellman equation becomes an assignment: a state's value is the reward now plus the discounted value of where you land, averaged with the model's probabilities. Back up every state from its successors, sweep after sweep, and the error shrinks by a factor γ per sweep onto the exact values; the optimal policy is then read off greedily.
One backup, then the same backup everywhere: reward spreads out one step per sweep
You need: expectations and a linear solve, nothing more. The parent Topic: rl places DP among the families (every RL method estimates the same expected return; DP computes it exactly from the model). Learning the same values from samples, without the model, is Model-free prediction and control.
| Bellman equation | Turned into an update, it is | Solves | Stops when |
|---|---|---|---|
| Expectation, for Vπ | Policy evaluation (section 4); also solvable directly as a linear system | Prediction | The largest change in a sweep is below θ |
| Expectation, then a greedy step | Policy iteration (section 6): evaluate, improve, repeat | Control | The policy stops changing: exact and finite |
| Optimality, for V* | Value iteration (section 7): one sweep of evaluation per improvement | Control | The largest change is below θ: approximate |
| Either, in any order | Asynchronous DP (section 12): in place, prioritised sweeping, real-time DP | Either | Every state keeps being backed up |
Why it matters beyond small tables: DP is the ideal every model-free method approximates (they replace its exact expectation over successors with one sampled successor), and its backup is the template for every learning target in RL (section 13). DP is model-based planning, not learning: nothing is learned from interaction (Sutton and Barto, chapter 4; Silver, Lecture 3). The Sweep lab runs every method here side by side on four worlds.
The loop. At each step t the agent receives an observation Ot and a reward Rt, then chooses an action At; the environment answers with Ot+1 and Rt+1 (Silver, Lecture 1). So the reward that follows action At is Rt+1, which is why every return below starts at Rt+1; texts that write rt for the reward of the action at t shift only the subscript.
The Markov property is what makes DP's one-step recursion valid: the backup reads only the successor's value, never the path that led there. A single Atari frame is not Markov (it shows where the ball is, not where it is going); stacking the last four frames, as DQN does, recovers the velocity (Mnih et al. 2015, Methods). When the agent observes the environment's state directly the problem is fully observable, formally an MDP; all of DP on this page lives there. When it sees the state only indirectly (a robot not told its absolute location, a poker agent that sees only the public cards) it is a partially observable MDP (POMDP), and the agent must build its own state. The language-model case: the state is the prompt plus the tokens so far and appending a token is deterministic, so generation is a fully observable MDP with a trivial, known transition (section 13 says why DP still cannot solve it).
A state is any function of the history, St = f(Ht). The environment state Set is the environment's private representation, whatever it uses to pick the next observation and reward; it is usually invisible to the agent and may hold irrelevant detail. The agent state Sat = f(Ht) is the agent's own summary, the state RL algorithms work with. The environment state is Markov, and so, trivially, is Ht; the design problem is a compact agent state that is still Markov. Fully observable means Ot = Sat = Set. In a POMDP (also a trading agent that sees only current prices) the three standard agent states are:
Notation. Sutton and Barto write the dynamics as one joint p(s′, r | s, a) rather than P and an expected R; they note the MDP literature usually uses the pair, and for DP the two are equivalent because only the expected reward enters the backups (Sutton and Barto, chapter 3 notes).
DP extends to POMDPs by planning over beliefs, which turns a finite problem into one over a continuous belief space (Sutton and Barto, chapter 1 history cites Lovejoy's 1991 survey).
Three objects, each adding one ingredient, on David Silver's student example (Silver, Lecture 2; the diagram in section 3 draws all three):
A policy is a distribution over actions given states, π(a | s); a deterministic policy picks one action, π(s). Fixing a policy turns an MDP back into an MRP, Pπss′ = Σa π(a | s) Pass′ and Rπs = Σa π(a | s) Ras: averaging over the policy's choices removes the agency, which is why evaluating a fixed policy is an MRP problem, and linear (section 4).
A concrete return first. Start in Class 1 with γ = ½ and sample the episode Class 1, Class 2, Class 3, Pass, Sleep: the rewards for leaving each state are −2, −2, −2 and +10, so the return is −2 + ½(−2) + ¼(−2) + ⅛(10) = −2 − 1 − 0.5 + 1.25 = −2.25. Another episode from the same start, Class 1, Facebook, Facebook, Class 1, Class 2, Sleep, gives −2 − ½ − ¼ − 2/8 − 2/16 = −3.125. The return is a random variable; the value is its average. Averaging samples is what model-free methods do; DP computes the same average exactly from the model.
The agent maximises cumulative reward, so it can pay to sacrifice reward now (refuelling a helicopter costs time now but may prevent a crash hours later; Silver, Lecture 1). Silver's four reasons to discount: cycles (it keeps returns finite when the chain can loop on Facebook indefinitely); uncertainty (the future, and the model of it, is less certain); convenience (with γ < 1 the Bellman operators are contractions, the reason DP converges, section 9); preference (people and animals prefer reward sooner, and immediate money can earn interest). Undiscounted returns (γ = 1) are fine when every sequence is guaranteed to terminate; LLM RL typically uses γ = 1, because every response ends and the reward arrives at the end.
The line is the running mean of the sampled returns, the dashed line the exact value from the Bellman equation (next section). Episodes of the student MRP from Class 1 are drawn with the chain's own probabilities from a seeded stream (seed 42), so each reset replays the same draws. The mean creeps toward the exact value slowly, because one episode can loop on Facebook for dozens of steps: that slowness is what the model buys you out of.
The value is the expected return, which removes the randomness of single episodes. In an MRP, V(s) = 𝔼[Gt | St = s]. In an MDP the return depends on how you act, so values are defined for a policy π, and the two kinds are one step apart:
That asymmetry is why Q is what you want for model-free control: acting greedily on Q is argmaxa Q(s, a), a lookup, while acting greedily on V needs the lookahead and so the dynamics (a model-free agent that stores only V cannot pick actions from it). DP has the model, so it works with V and computes Q when it needs it. Optimal values: V*(s) = maxπ Vπ(s) and Q*(s, a) = maxπ Qπ(s, a). An MDP is solved once Q* is known: picking argmaxa Q*(s, a) everywhere is optimal. For any finite MDP an optimal policy π* is at least as good as every other policy in every state, all optimal policies share V* and Q*, and there is always a deterministic one (Silver, Lecture 2).
The Bellman equation cuts a long-horizon problem into one-step pieces, and follows from the return's own recursion: V(s) = 𝔼[Rt+1 + γGt+1 | St = s] = 𝔼[Rt+1 + γV(St+1) | St = s], where the last step replaces the random tail by its expectation, which the Markov property allows. The value of a state is the immediate reward plus the discounted value of the successor. For a known MRP it reads V(s) = Rs + γ Σs′ Pss′ V(s′). Four value functions give four equations:
The expectation equations are linear: one equation per state, as many unknowns as states, so Vπ = Rπ + γPπVπ has the direct solution Vπ = (I − γPπ)−1 Rπ, with column vectors over states and Pπ the MRP's matrix from section 1. Inversion costs O(n³) for n states, so it is only for small problems. The max makes the optimality equations nonlinear: matrix inversion does not solve them, there is no closed form in general, and they are solved iteratively, by value and policy iteration when the model is known (this page) and from samples by Q-learning and SARSA when it is not (Model-free prediction and control). Everything downstream, from value iteration to DQN's TD target to the value model in PPO-based RLHF, is a way of approximately solving one of these equations; the DP algorithms are "little more than these equations turned into assignment statements" (Sutton and Barto §4.8).
| State | γ = 0 | γ = 0.9 | γ = 1 |
|---|---|---|---|
| Class 1 | −2.0 | −5.0 | −12.5 |
| Class 2 | −2.0 | 0.9 | 1.5 |
| Class 3 | −2.0 | 4.1 | 4.3 |
| Pass | 10.0 | 10.0 | 10.0 |
| Pub | 1.0 | 1.9 | 0.8 |
| −1.0 | −7.6 | −22.5 |
The student MRP solved as V = (I − γP)−1R, Sleep terminal at 0, to one decimal (checked in src/recompute.py).
Reading the numbers. At γ = 0 every value is the immediate reward. At γ = 1 Facebook is the worst state: it loops back with probability 0.9, so the agent stays 10 steps on average at −1 each, then lands in Class 1, worth −12.5, for −22.5 in total. Check Class 3 at γ = 1: V(Class 3) = −2 + 1 × (0.6 × 10 + 0.4 × 0.8) = −2 + 6 + 0.32 = 4.32, the solved 4.3. And reward is not value: Class 2 has reward −2 and value +1.5. In the MDP at γ = 1:
Each action with probability 0.5, γ = 1: Vπ = −1.3 (Class 1), 2.7 (Class 2), 7.4 (Class 3), −2.3 (Facebook). At Class 3, half the time the Pub and half the time Study: 0.5 × (1 + 0.2 × (−1.3) + 0.4 × 2.7 + 0.4 × 7.4) + 0.5 × 10 = 7.4. The widget's middle mode shows every state's equation.
Prediction and control are the two problems these equations pose. Prediction: given a fixed π, compute Vπ or Qπ. Control: find π*, equivalently V* or Q*. You usually solve prediction to solve control: evaluate, improve greedily, repeat. With a model, prediction is policy evaluation (section 4) and control is policy iteration (section 6) or value iteration (section 7).
Values solve the Bellman expectation equation exactly (a 6- or 4-state linear system) or, for the optimal policy, iterate the optimality equation to convergence. At γ = 0, 0.9 and 1 the Markov reward process reproduces the table in Silver's Lecture 2 independently; the slide rounds Class 1 and Facebook at γ = 1 to −13 and −23 (exactly −12.54 and −22.54).
Four words recur (Sutton and Barto §4.1). A backup (Sutton and Barto: an expected update) is one application of the right-hand side of a Bellman equation to one state, as an assignment; "expected" because it averages over every possible successor with its probability rather than one sampled successor, which is what a full-width backup means and why it needs the model. A sweep backs up every state once. A synchronous sweep computes every new value from the previous sweep's values, keeping two arrays (Jacobi-style, after Jacobi's method for linear systems). An in-place sweep overwrites as it goes, so later states already see this sweep's new values (Gauss–Seidel-style): one array, usually faster, and then the order in which states are swept matters (section 8); Sutton and Barto "usually have the in-place version in mind" (chapter 4 notes).
Policy evaluation estimates Vπ of a given, fixed policy by turning the expectation equation into that assignment:
Why it converges. Vπ is a fixed point of the update and the backup is a γ-contraction (section 9), so the iteration reaches the unique Vπ from any start. Sutton and Barto's condition is wider: Vπ exists, is unique and is the limit "as long as either γ < 1 or eventual termination is guaranteed from all states under the policy π" (§4.1). Or solve it directly: (I − γPπ)−1Rπ is one linear solve of order n³. Iterative evaluation is "a classical successive approximation algorithm for solving a system of linear equations" (Sutton and Barto, chapter 4 notes); it wins when n is large, when transitions are sparse (a sweep then costs about n times the number of successors), and when a good starting guess exists, as inside policy iteration.
Figure 4.1 in depth: the random policy's values sink sweep by sweep, while the greedy arrows settle long before
The standard example (drawn on the right) is Sutton and Barto's 4 × 4 gridworld (Example 4.1): two shaded corners end the episode, every move costs −1, a move off the grid leaves you in place, γ = 1, and the policy picks each move with probability ¼, so Vπ is minus the expected number of steps to a corner. The Sweep lab opens on it, and the greedy policy on its values is optimal from k = 3 on, as Figure 4.1's caption says (book p. 77), long before the values converge.
The lab's computed values reproduce Figure 4.1 independently, 92 of the 96 printed values at two significant digits. The other four are the exact −1.75 at k = 2, printed −1.7 while the same figure prints −2.875 as −2.9: a rounding slip in the figure, not a different computation. Figure 4.1's numbers also require synchronous sweeps; an in-place sweep gives different intermediate values.
Once you know Vπ, act greedily on it: compute every action's lookahead value and pick the best, π′(s) = argmaxa [ R(s, a) + γ Σs′ P(s′ | s, a) Vπ(s′) ] = argmaxa Qπ(s, a), a new deterministic policy. The student MDP did exactly this in section 3 and got the optimal policy in one step.
The policy improvement theorem (Sutton and Barto §4.2): if deterministic π and π′ satisfy Qπ(s, π′(s)) ≥ Vπ(s) in every state, then Vπ′(s) ≥ Vπ(s) in every state, strictly wherever the premise is strict. In words: taking the better action once and then following π is at least as good as following π, and doing so at every later step only adds improvement. The greedy policy satisfies the premise automatically (the best action's value is at least the policy-weighted average, which is Vπ(s)). And if greedy improvement changes nothing, then Vπ(s) = maxa Qπ(s, a) everywhere, which is the Bellman optimality equation: π is already optimal. So improvement "must give us a strictly better policy except when the original policy is already optimal".
Keep expanding the Q side and reapplying the premise:
Stochastic policies and ties. The theorem carries over to stochastic policies. When several actions tie for the max, the new policy may split its probability among them in any way, as long as every non-maximising action gets zero; Figure 4.1's right column draws an arrow for every tied action for this reason.
Alternate the two steps until the policy stops changing: evaluate the current policy (sweeps until the values settle, or the linear solve), then improve it greedily. The chain π0 → Vπ0 → π1 → Vπ1 → ... → π* → V* has each policy strictly better than the last unless already optimal (Sutton and Barto §4.3). Start each evaluation from the previous policy's values: this "typically results in a great increase in the speed of convergence of policy evaluation (presumably because the value function changes little from one policy to the next)".
Why it terminates. A finite MDP has mn deterministic policies for m actions and n states; each improvement is at least as good everywhere and strictly better somewhere unless optimal, so no policy repeats and the loop ends at a policy that satisfies the optimality equation. In practice it needs very few outer iterations, because the policy changes in whole jumps and often becomes optimal long before its values converge: on Figure 4.1's gridworld it "would find the optimal policy after just one iteration"; on Jack's car rental (Example 4.2: two locations with up to 20 cars each, Poisson rentals and returns, γ = 0.9) π0, ..., π4 reach the optimum in four improvements (Figure 4.2). On the Sweep lab's worlds it stops after 2 improvements on both 4 × 4 grids, 3 on the Dyna maze and 5 on Russell and Norvig's 4 × 3 world.
Evaluation does not have to converge. Stopping evaluation after k sweeps and improving anyway still reaches the optimal policy: modified policy iteration (Puterman and Shin 1978), which contains policy iteration (k → ∞) and value iteration (k = 1) as special cases. In Silver's 4 × 4 gridworld (Figure 4.1's) k = 3 evaluation sweeps were already enough for the greedy policy to be optimal (Silver, Lecture 3). One catch, found while building the old version of this page: with truncated evaluation, "the policy did not change" no longer proves optimality, because the values the policy was greedy on were not yet its own. Stop only when the policy is unchanged and the values have stopped changing.
Generalised policy iteration (GPI) is the general pattern: any interleaving of some evaluation with some improvement, at any granularity, down to a single state. "As long as both processes continue to update all states, the ultimate result is typically the same, convergence to the optimal value function and an optimal policy" (Sutton and Barto §4.6). The two compete (making the policy greedy makes the values wrong for it; making the values right makes the policy no longer greedy) and cooperate: both stabilise only when the policy is greedy on its own value function, which is the optimality equation. "Almost all reinforcement learning methods are well described as GPI", PPO included (section 13).
Turn the Bellman optimality equation itself into the assignment, with no policy stored anywhere, and watch it on Silver's shortest-path grid: a 4 × 4 grid, one goal in the corner, −1 per move, γ = 1, values from zeros.
Values sweep out from the goal one cell per sweep: within k moves they are exact, beyond they all say −k
Predict before it plays: after 2 sweeps, what does the far corner (6 moves from the goal) hold, and which way does its greedy arrow point?
Synchronous value iteration computed live by the page's engine (checked against src/recompute.py). Bar height is −V; orange bars changed in the sweep just shown, and the ringed cell is the backup worked out above, fed by streams from the four cells it reads; during each step a glass plane crosses the grid, and each column takes its new value as the plane reaches it (all computed from the previous sweep's values); arrows are greedy on the current values, every tied action drawn. The frames reproduce Silver's slide independently: his V1 is the all-zero start, V2 = −1 everywhere but the goal, V3 = −2 in most cells, and V7 holds the true distances, 0 to −6 (his Vk+1 is the frame after k sweeps here).
What the sweeps show. It looks like policy evaluation with one difference: each sweep takes a max over actions rather than an average under some π. Equivalently, each sweep is one evaluation sweep of the policy that is greedy on the current values, so value iteration is policy iteration with evaluation cut to a single sweep (Sutton and Barto §4.4). Silver's intuition: "start with final rewards and work backwards"; it "still works with loopy, stochastic MDPs". As step 3 shows, the intermediate value functions need not be the value of any policy; only the limit is, and it is the optimal policy's (Silver, Lecture 3). Formally value iteration converges only in the limit, but the greedy policy becomes optimal after finitely many sweeps (Bertsekas 1987, cited in Sutton and Barto's notes). Between the extremes, "faster convergence is often achieved by interposing multiple policy evaluation sweeps between each policy improvement sweep" (§4.4): modified policy iteration again.
Sutton and Barto's Example 4.3 (book p. 84): a gambler with capital s from 1 to 99 stakes any whole number from 1 to min(s, 100 − s) on a coin that comes up heads with probability ph; heads doubles the stake, reaching 100 pays +1, everything else 0, γ = 1, so the value is the probability of winning. In-place value iteration from zeros, as in the book's box, until no value moves by more than 10−12. With ph = 0.4 the optimal policy is famously curious: at capital 50 bet everything, at 51 bet a little. At ph = 0.4 it converges after 22 in-place sweeps (checked in src/recompute.py). The policy is optimal but not unique: a whole family of optimal policies comes from ties in the argmax (Exercise 4.8 asks why).
Reproduces Figure 4.3 in shape only: the book gives the curves as an image, which this page never reads numbers from. What the text states does hold: at ph = 0.4 the policy bets everything at 50 but not at 51, and many optimal policies exist. At ph = 0.55 the smallest stake is optimal everywhere: when the coin favours you, play many small rounds (at exactly 0.5 every stake ties) derived.
Three states in a row, A, B and C, with a terminal goal G to the right of C. Two deterministic actions, left and right. Moving right from C enters G, earns 10 and ends the episode; every other move earns 0; left from A bumps the wall and stays. γ = 0.9 and V(G) = 0. Synchronous value iteration backs up V(A) ← max(0.9 V(A), 0.9 V(B)), V(B) ← max(0.9 V(A), 0.9 V(C)) and V(C) ← max(0.9 V(B), 10 + 0.9 × 0).
| Sweep | V(A) | V(B) | V(C) | What happened |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | Initial guess |
| 1 | 0 | 0 | 10 | C: right gives 10 + 0.9 × 0 = 10, left gives 0.9 × 0 = 0 |
| 2 | 0 | 9 | 10 | B: right gives 0.9 × 10 = 9 |
| 3 | 8.1 | 9 | 10 | A: right gives 0.9 × 9 = 8.1 |
| 4 | 8.1 | 9 | 10 | No change: converged |
So V* = (8.1, 9, 10), right in every state; after k synchronous sweeps only rewards up to k steps away have been seen. Predict: if the sweep goes in place in the order C, B, A, how many sweeps does it take? And in the order A, B, C?
Every number is computed live by the page's engine and checked against src/recompute.py; the defaults reproduce the table above, which comes from the old version of this page, independently. The line under the cells gives the values the backup reads: the previous sweep's (synchronous) or the current array (in place). The random-policy evaluation steps through its first three sweeps backup by backup, then jumps to sweep 40.
The answer. In place in the order C, B, A converges in a single sweep (C becomes 10, then B sees the new C and becomes 9, then A becomes 8.1); in the order A, B, C it is no faster than synchronous here. That is why sweep order matters for in-place DP. Policy iteration on the same corridor, from the uniform random policy: its values converge to Vπ ≈ (4.29, 5.24, 7.36); greedy on them, right wins everywhere (in A, left 0.9 × 4.29 = 3.86 against right 0.9 × 5.24 = 4.72; in C, right 10 against 4.72), so one improvement gives the optimal policy, evaluating it gives (8.1, 9, 10), and policy iteration stops after two outer iterations. Evaluation did not need to converge: after only three sweeps, with values (1.0125, 2.25, 6.0125), the greedy comparison is already right everywhere. That is modified policy iteration paying off.
Evaluation backups for the random policy: V(A) ← 0.45 V(A) + 0.45 V(B), V(B) ← 0.45 V(A) + 0.45 V(C) and V(C) ← 0.45 V(B) + 5, where 0.45 is 0.5 × 0.9 and 5 is 0.5 × 10. From zeros the sweeps give (0, 0, 5), then (0, 2.25, 5), then (1.0125, 2.25, 6.0125), then (1.4681, 3.1612, 6.0125), converging to the exact solution of the three linear equations. Improvement on the converged values: in B, left 3.86 and right 0.9 × 7.36 = 6.62; in C, left 0.9 × 5.24 = 4.72. After three sweeps: in A, right is 0.9 × 2.25 = 2.025 against left 0.9 × 1.0125 = 0.91; in B, 5.41 against 0.91; in C, 10 against 2.025.
Measure the distance between two value functions by the largest difference in any state, ‖u − v‖∞ = maxs |u(s) − v(s)|, and write T for one synchronous sweep (expectation backup for a fixed π, or optimality backup). Both are γ-contractions (Silver, Lecture 3):
How many sweeps. With γ = 0.9, cutting the error by a factor of 1,000 takes 66 sweeps (0.966 ≈ 0.00096); with γ = 0.99 it takes 688 derived. Since ln(1/γ) ≈ 1 − γ, the sweep count grows like the effective horizon 1/(1 − γ): rewards that far away still matter, and each sweep reaches one step further.
A small change is not a small error. If one sweep changes no value by more than θ, the contraction gives ‖Vk+1 − V*‖∞ ≤ γθ/(1 − γ): with γ = 0.99 and θ = 0.001 the values can still be off by up to 0.099, about a hundred times the last change. A small error is a small loss, though: acting greedily on any V with ‖V − V*‖∞ ≤ ε loses at most 2γε/(1 − γ) of return in any state (Singh and Yee 1994, in the form given by RL Theory, lecture 3).
Without discounting. At γ = 1 there is no contraction, yet Figure 4.1, Silver's shortest path and the 4 × 3 world all use γ = 1 and converge. What saves them is termination: if every policy evaluated eventually reaches a terminal state (a "proper" policy), the sweeps still converge, a stochastic-shortest-path result outside this page's scope (Bertsekas and Tsitsiklis 1996). Drop that and an undiscounted evaluation of a policy that loops forever on a −1 reward runs off to −∞. γ = 1 is not wrong in itself: it is standard when every episode terminates, as in LLM RL.
Szepesvári's lecture notes give the usual form: k ≥ ln(1/(ε(1 − γ)))/(1 − γ) iterations bring value iteration within ε of V* (RL Theory, lecture 3). It follows from the bound above: with rewards in [0, 1] and V0 = 0 the starting error is at most 1/(1 − γ), and ln(1/γ) ≥ 1 − γ derived.
Predict, then drag γ to 0.99 and to 1: what happens to the grey bound, and do the runs still converge? Russell and Norvig's 4 × 3 world (section 13 and the Sweep lab), synchronous value iteration, log scale. The gap between the runs (purple) never rises above the bound (grey), checked sweep by sweep in src/recompute.py at γ = 0.5, 0.9 and 0.99. The errors fall faster than the bound because the bound is a worst case and the 4 × 3 world's paths end within a few steps. At γ = 1 the bound is flat and useless, yet both runs still converge: termination, not contraction.
| Policy iteration | Value iteration | |
|---|---|---|
| What is stored | Policy-based: an explicit policy is stored and improved | Value-based: no explicit policy until the end |
| Update rule | Bellman expectation equation, plus greedy improvement | Bellman optimality equation directly |
| Intermediate values | At every step, V corresponds to some actual policy | Intermediate V may correspond to no policy; only the final V is (the optimal) one's |
| Per-sweep cost | An evaluation sweep under a deterministic policy reads one action per state, cheaper than a value-iteration sweep by a factor of the number of actions; only the improvement step reads all actions. But evaluation is a loop inside the outer loop | Every sweep maxes over all actions; a single loop |
| Typical behaviour | Few outer iterations; the policy often stabilises before the values fully converge | More sweeps, each simple |
| Stops when | The policy does not change: an exact, finite stopping point | The largest change in a sweep is below a threshold: approximate |
Both need the model and converge to the same V* and π*. "Both policy iteration and value iteration are widely used, and it is not clear which, if either, is better in general" (Sutton and Barto §4.7). Policy iteration wins when evaluation is cheap (few actions, or an affordable linear solve), value iteration when each state has many actions or the policy would change many times; modified policy iteration, with a handful of evaluation sweeps per improvement, sits between them and is often the practical choice. Counting model look-ups in the Sweep lab makes the trade concrete: to reach an error of 10−4 on the 4 × 4 grids, policy iteration needs about 60 to 80 times as many look-ups as value iteration, most of them spent evaluating the starting random policy at γ = 1, while modified policy iteration with three sweeps comes within a factor of 1.6 of value iteration; on the 4 × 3 world it beats value iteration (1,581 look-ups against 2,112).
One sweep backs up every state, and each backup reads every action and every possible successor. With n states and m actions a sweep over state values costs O(mn²); a sweep over action values Q(s, a) costs O(m²n²), because there are mn entries and each backup reads mn successor pairs (Silver, Lecture 3). Those are dense worst cases: in a gridworld each action has at most three successors, so a sweep costs about 3mn look-ups, which is what the Sweep lab counts derived.
DP is exponentially faster than searching policies. "In the worst case, the time that DP methods take to find an optimal policy is polynomial in the number of states and actions", although there are mn deterministic policies, each of which direct search would have to examine for the same guarantee (Sutton and Barto §4.7). In practice DP handles millions of states (Silver; Sutton and Barto) and "usually converge[s] much faster than [its] theoretical worst-case run times, particularly if [started] with good initial value functions or policies".
The curse of dimensionality (Bellman's phrase) is the catch: states multiply with every state variable. Predict before moving the sliders: ten variables with ten values each make how many states? Then try twenty.
Ten variables with ten values each already make ten billion states, and at that size even one backup can be too expensive. Backgammon has over 1020 states; Sutton and Barto note that at a million backups a second one sweep would take "over a thousand years" (§4.5), an understatement: 1020/106 seconds is about 3.2 million years derived. Large state sets are "inherent difficulties of the problem, not of DP as a solution method": DP copes with them better than direct search or linear programming.
V* is the smallest function that satisfies every one-step inequality, so it solves a linear program: minimise Σs c(s) V(s) subject to V(s) ≥ R(s, a) + γ Σs′ P(s′ | s, a) V(s′) for every state and action, with any positive weights c; n variables and nm constraints (one inequality per action is how the max of the optimality equation becomes linear). Its dual has one variable per state-action pair, the discounted occupancy measure (how often the policy visits each pair), and the optimal policy read from it does not depend on the weights (Mahajan, Stochastic control notes, linear programming, written for costs, so the inequalities flip). An off-the-shelf solver returns exactly the V* that value iteration converges to on the 4 × 3 world (9 variables, 36 constraints), Figure 4.1's grid and the Dyna maze derived, checked in src/recompute.py. Linear programming can have better worst-case guarantees than DP, but "become[s] impractical at a much smaller number of states than do DP methods (by a factor of about 100)" (§4.7). The dual view returns in constrained MDPs and offline RL, where occupancy measures are the natural variable, and de Farias and Van Roy built approximate DP on it (de Farias and Van Roy 2003).
States n = (values)variables; look-ups per value-iteration sweep: mn² if every state can lead to every state, mnb with b = 3 successors per action as in the grids here; deterministic policies mn. The time assumes 109 look-ups a second, an illustrative rate.
Asynchronous DP backs up states in any order rather than in systematic sweeps, "using whatever values of other states happen to be available"; some states may be updated several times before others once. It converges "given only that all states occur in the sequence {sk} an infinite number of times" for 0 ≤ γ < 1; the order can even be random (Sutton and Barto §4.5). It does not necessarily mean less computation: it means never getting "locked into any hopelessly long sweep before it can make progress", and freedom to choose which states to back up (Silver, Lecture 3; Bertsekas 1982, 1983). The Sweep lab races these on the same world with the same budget of model look-ups.
Prioritised sweeping was developed independently by Moore and Atkeson and by Peng and Williams in 1993 (Sutton and Barto §8.4 and notes); it solved a rod-manoeuvring task with 14,400 potential states that is "probably too large to be solved with unprioritized methods".
On Sutton and Barto's racetrack (9,115 reachable states, only 599 relevant), Gauss–Seidel value iteration converged in 28 sweeps and 252,784 updates, about twice as fast as the Jacobi version; RTDP needed 127,600 updates over 4,000 episodes, about half, and left about 290 states never updated, with both policies averaging 14 to 15 steps (Sutton and Barto §8.7, Example 8.6; Barto, Bradtke and Singh 1995).
Past millions of states, either the model has to go or the table has to go. Sample backups replace the full sum over successors with one sampled transition ⟨S, A, R, S′⟩: they need no model, and the cost of a backup becomes "constant, independent of n", which "breaks the curse of dimensionality through sampling" (Silver, Lecture 3). Predict: on the same 9-cell world, how many sampled transitions does Q-learning need to get within a few hundredths of what value iteration reaches in about 20 sweeps?
One world, two ways to get its values: sweep the model, or learn from samples
Russell and Norvig's 4 × 3 world (AIMA, Figure 17.1; the book's code): each move goes where intended with probability 0.8 and sideways with 0.1 each; bumping a wall stays put; every step costs 0.04; the two exits pay +1 and −1; γ = 1. Value iteration converges on exactly the utilities printed in AIMA's Figure 17.3 (0.812, 0.868, 0.918 on the top row; 0.762 and 0.660 in the middle; 0.705, 0.655, 0.611, 0.388 on the bottom), reproduced independently. The Q-learning agent never sees the 0.8 and 0.1: it starts each episode in a random cell, behaves ε-greedily (ε = 0.3), updates Q(s, a) toward −0.04 + maxa′ Q(s′, a′) with step 1/N(s, a)0.8, and shows maxa Q. Its run is one seeded draw; the step-size and start settings are illustrative.
The answer. Value iteration reaches AIMA's values in about 20 sweeps, each touching all 9 states with 4 actions and 3 outcomes, and it never moves the agent. Q-learning gets within a few hundredths of the same numbers only after thousands of episodes and more than ten thousand transitions, and its rarely visited cells (bottom right, next to the −1) lag longest. The model buys exactness and speed; samples buy independence from the model. Step 7 of the value-iteration run shows what optimal means under risk.
Where DP lives on. Replace the expectation by one sample and the backup is the TD target (Q-learning is sampled value iteration); replace the table by a network and it is approximate DP, DQN; keep evaluate-then-improve with both approximated and it is GPI, the shape of PPO. Token generation even has the known model DP needs, but every possible prefix is a state, so LLM RL samples instead.
Every DP method on this page, run live on the same world with the same budget, side by side. One frame is one value-iteration sweep's worth of model look-ups (one look-up reads one action's successor, its probability and reward), so a method that does cheaper work per step gets more steps per frame: a policy-evaluation sweep under a stored deterministic policy reads one action per state, a quarter of a value-iteration sweep.
Try: the default runs policy iteration (left) against value iteration (right) on Sutton and Barto's 4 × 4 grid, opened on frame 3; press Play to run both from frame 0, or drag the frame slider. The left panel's first frames are Figure 4.1's left column exactly (policy iteration starts by evaluating the random policy); its arrows are the figure's right column, optimal from frame 3 while the values are still far off. Then switch the world to the Dyna maze and race in-place or prioritised sweeping against synchronous sweeps. A changed setting opens on its last frame, both runs finished, with the whole race in the chart. Click any cell for its backup with the current numbers.
Methods. Policy evaluation runs on the uniform random policy (Figure 4.1's). Policy iteration starts from the uniform random policy, evaluates it until no value moves by more than θ, then improves greedily, keeping the current action when it is among the tied best (so it cannot flip between equally good policies), and stops when no action changes. Modified policy iteration does exactly 3 evaluation sweeps per improvement and stops when the policy is unchanged and the last sweep moved nothing by more than θ. Value iteration and policy evaluation run synchronously (two arrays, as Figure 4.1 requires) or in place, in reading order or reversed. Prioritised sweeping is the model-known form: every state's Bellman error is computed once, then the state with the largest priority is backed up, and γ × P(s | p, a) × |change| (the largest over p's actions) is added to each predecessor p's priority, an upper bound on how much its Bellman error can have grown; it stops when no priority exceeds 10−12.
Counting. Each backup costs one look-up per (action, successor) pair it reads; an improvement step reads every action of every state; prioritised sweeping pays one more look-up per predecessor update. Values start at 0; terminal states hold their value (0, or +1 and −1 for Russell and Norvig's exits). The target is V* for control methods and Vπ of the random policy for evaluation, both computed exactly. "Policy optimal" means every greedy action (ties within 10−9) is an optimal action.
Checked. src/check_engine.mjs runs this tab's engine in Node and compares it with src/recompute.py, an independent Python implementation: every Figure 4.1 value, the Russell and Norvig utilities, and for all four worlds and eight methods the look-ups and backups needed to reach an error of 10−4 (no mismatch).
Links open in a new tab. Times are estimates: for knowledge base pages, the page's own reading time where it states one.
Policy evaluation, policy iteration, modified policy iteration, value iteration (synchronous and in place) and prioritised sweeping on four worlds, side by side on the same budget of model look-ups; reproduces Figure 4.1, Silver's shortest path and AIMA Figure 17.3.
In the Reading tab, section 8: synchronous against in-place value iteration and random-policy evaluation, one backup at a time; section 7 steps value iteration sweep by sweep, and its closed block runs the gambler's problem.
In the approved reading order.
The parent: an intuitive overview of RL on one through-line (every method estimates the expected return and differs in what stands in for the expectation), with the Taxonomy, Method atlas, and Milestones and benchmarks tabs. (17 min, plus 2 in closed details)
The one-state MDP: greedy, ε-greedy, optimistic starts, UCB, Thompson sampling and regret.
The same evaluate-and-improve loop from samples: Monte Carlo, TD, n-step and TD(λ), SARSA and Q-learning. (34 min, plus 9 in closed details)
Approximate value iteration with neural networks: the deadly triad, DQN, Double DQN, dueling, Rainbow.
GPI with a gradient step for improvement: REINFORCE, actor-critic and GAE, TRPO, PPO, SAC.
Planning with a learned or given model at scale: Dyna, Monte Carlo tree search, AlphaZero, MuZero.
Learning from a fixed dataset: behaviour cloning, DAgger, inverse RL, distribution shift.
Token generation as an MDP with a known transition, and why it is sampled rather than solved. (53 min, plus 13 in closed details · +20h 55m resources)
The post-training pipeline around the RL step. (33 min, plus 4 in closed details)
When the reward in the Bellman equation is a proxy, and the optimiser exploits it. (33 min, plus 12 in closed details · +4h 57m resources)
The canonical RLHF-with-PPO recipe: GPI with a learned critic on a language model. (26 min read)
GRPO, which drops the critic (approximate policy evaluation) for group statistics. (25 min read)
The MDP as a discrete stochastic control problem. (about 30 min)
Bellman's own account of the name and the early years. (about 15 min)
Policy iteration and simplex for fixed discount. (about 1h)
The 2γε/(1 − γ) bound on acting greedily on an approximate V. (about 20 min)
Linear programming with a function approximator. (about 1h)
The primal and dual LPs and occupancy measures, written for costs. (about 20 min)
The clearest walkthrough of policy iteration against value iteration, with the gridworld and the contraction proof (slides: PDF). (1h 30m)
Markov process to MRP to MDP and the student example used on this page. (1h 30m)
Policy evaluation to GPI, with the proof sketch of the policy improvement theorem, Jack's car rental and the gambler. (45 min)
The canonical treatment of MDPs, returns and the Bellman equations. (1h)
Value and policy iteration with exact iteration counts, error bounds and lower bounds. (1h 30m)
Interactive; watch values propagate and edit rewards. (about 10 min)
A one-page compression of the foundations, MDPs to DP. (about 35 min)
A crisp notation reference for states, policies, returns and Bellman equations. (about 15 min)