Part I · Foundations of Sequential Decision-Making
5.Dynamic Programming: Planning with a Known Model
“An optimal policy has the property that whatever the initial state and initial decision are, the remaining decisions must constitute an optimal policy with regard to the state resulting from the first decision.”
Suppose Rusty is handed the warehouse blueprint: the exact transition probabilities and rewards from Chapter 4. No sensors needed, no trial and error — finding the best policy becomes pure computation. But the naive computation, enumerating all 4^80 policies, is beyond astronomical. This chapter shows how Bellman structure replaces enumeration with iteration, proves that greedy improvement can never make a policy worse, and extracts the pattern — generalized policy iteration — that every remaining algorithm in this book is a variation on.
Foundation
Iterative policy evaluation, the policy improvement theorem with proof, policy and value iteration, convergence rates, the Bellman-error bound, and asynchronous DP.
Conceptual
The book's signature dashboard: value rippling outward from the dock sweep by sweep, with Δ and the suboptimality bound streaming beside it.
Practical
rl-tabular's DP module operating on the Chapter 4 Mdp trait, with the sweep-by-sweep generator that drives the dashboard.
After this chapter you can
- Implement iterative policy evaluation and explain why in-place sweeps converge faster than two-array ones
- Prove the policy improvement theorem and understand why it makes greedy improvement safe
- Explain the difference between policy iteration and value iteration as truncation of the same process
- Use the Bellman-error stopping rule to bound how suboptimal your current policy is
- State generalized policy iteration and identify it in every later algorithm in the book
- Quantify why dynamic programming cannot scale to a real robot
5.1 The blueprint changes everything
Overnight, Rusty receives the warehouse blueprint — the exact MDP from Chapter 4. The grid, , rewards of at the dock, per step, per shelf bump, and .
With and in hand, "what should Rusty do?" is no longer an experimental question. It is arithmetic. But which arithmetic?
The direct approach is to enumerate every deterministic policy, evaluate each, and keep the best. With roughly 80 states and four actions that is policies. At a billion evaluations per second you would finish some times after the heat death of the universe.
Dynamic programming is the family of methods that exploits Bellman structure to replace that enumeration with iteration. The key insight is Bellman's own principle of optimality, quoted above: an optimal policy's tail must itself be optimal. That means we can build the answer up from local consistency conditions rather than searching the space of whole policies.
5.2 Iterative policy evaluation
Start with the easier problem: given a policy , what is ?
Chapter 4 showed this is a linear system solvable by matrix inversion, at cost . Iteration is cheaper. Turn the Bellman expectation equation into an assignment:
which is exactly applying the operator . Theorem 4.3 proved is a -contraction, so by Banach the sequence converges to the unique fixed point from any initialization — including all zeros.
When do we stop? Track and halt when it falls below a threshold . Banach's bound converts into a guarantee: if the greedy policy with respect to is , then
This is not decoration. It is the difference between "the numbers stopped changing much" and "my policy is provably within of optimal", and the dashboard below displays it live.
5.3 The policy improvement theorem
Now the result that makes everything work. Suppose we have and construct a new policy that acts greedily with respect to it:
Is better? It is locally better by construction — but each state's improvement changes which states you visit, so the argument is not obvious. It is, however, true.
Theorem 5.1— Policy improvement theorem
Let and be deterministic policies such that for all ,
Then is at least as good as : for all . If the first inequality is strict at any state, the second is strict at that state too.
▸Proof
Start from the hypothesis and expand repeatedly, each time replacing by the hypothesis applied one step further along:
Each inequality applies the hypothesis at the newly-exposed state; each equality is the tower property. The remainder term vanishes as because is bounded and — the same geometric argument as everywhere else in this book.
5.4 Policy iteration and value iteration
Policy iteration alternates the two operations to convergence:
Since each improvement strictly increases the value of at least one state unless the policy is already optimal, and there are finitely many deterministic policies, policy iteration terminates in finitely many iterations at an exactly optimal policy. In practice it converges in a handful — often fewer than ten, even on large problems.
The cost is that each evaluation runs to convergence, which is many sweeps of work to answer a question ("which action is best here?") that may not need that precision.
Value iteration takes the opposite extreme: truncate evaluation after a single sweep, folding the improvement into it by using a max instead of a policy average:
This is just repeated application of , which Theorem 4.3 proved is a -contraction. It converges geometrically, though — unlike policy iteration — it approaches optimality asymptotically rather than hitting it exactly.
Generalized policy iteration, sweep by sweep
ch05-gpi-dashboardRusty's warehouse: value heatmap + greedy policy arrows, with the convergence measure the theory names.
Sweep
1
policy evaluation
Δ = max|Vₖ₊₁ − Vₖ|
21.1
still changing
Suboptimality bound
802.1
2γΔ/(1−γ) ≥ ‖v_π − v*‖∞
States swept
76.0
9×12 grid minus shelves
Watch what happens when you press play on value iteration. Value appears first at the dock, then bleeds outward one cell per sweep. That is not an artifact of the visualization: with and a one-step backup, information about the goal can travel exactly one cell per sweep. Cells far from the dock cannot know anything about it until the wave arrives.
Then switch to policy iteration and watch a different rhythm: long stretches of evaluation where the arrows barely move, punctuated by improvement steps where large regions of the policy flip at once.
use rl_core::Mdp;
pub struct Sweep {
pub index: usize,
pub values: Vec<f64>,
pub delta: f64,
/// Bellman-error bound on ‖v_π − v*‖∞ implied by this Δ.
pub suboptimality: f64,
}
/// Value iteration with in-place (Gauss–Seidel) sweeps.
pub fn value_iteration<M: Mdp>(mdp: &M, theta: f64, max_sweeps: usize) -> Vec<Sweep> {
let gamma = mdp.gamma();
let mut v = vec![0.0; mdp.states().len()];
let mut history = Vec::new();
for index in 1..=max_sweeps {
let mut delta: f64 = 0.0;
for (i, state) in mdp.states().iter().enumerate() {
if mdp.is_terminal(state) { continue; }
let old = v[i];
// max_a Σ p(s′,r|s,a)[r + γ v(s′)] — the Bellman optimality backup.
v[i] = mdp
.actions(state)
.iter()
.map(|&a| {
mdp.transitions(state, a)
.iter()
.map(|&(next, reward, prob)| prob * (reward + gamma * v[next]))
.sum::<f64>()
})
.fold(f64::NEG_INFINITY, f64::max);
delta = delta.max((old - v[i]).abs());
}
history.push(Sweep {
index,
values: v.clone(),
delta,
suboptimality: 2.0 * gamma * delta / (1.0 - gamma),
});
if delta < theta { break; }
}
history
}5.5 Generalized policy iteration
Step back and notice that policy iteration and value iteration are the same algorithm with a dial set differently.
Both maintain a value estimate and a policy. Both push the value toward consistency with the policy (evaluation) and push the policy toward greediness with respect to the value (improvement). They differ only in how much evaluation happens between improvements: everything, one sweep, or — in modified policy iteration — some number in between.
Generalized policy iteration (GPI) is the name for this pattern in the abstract: any interleaving of evaluation and improvement, at any granularity, in any order, including asynchronously and on subsets of states.
5.6 Asynchronous DP, and the wall
Nothing requires sweeping states in order, or sweeping all of them equally. Asynchronous DP updates states in any order, as long as every state is updated infinitely often in the limit. Convergence still holds.
This licenses genuinely useful strategies: back up states along a trajectory the robot actually visits, prioritize states whose values just changed a lot (Chapter 7's prioritized sweeping), or focus computation near the goal where it does most good.
But no ordering saves dynamic programming for a real robot. The problem is not the sweep order — it is that a sweep must touch every state, and the number of states is exponential in the robot's degrees of freedom.
The exponential wall
ch14-dimensionality-wallDiscretize a robot's joint space and count the cells. One sweep of tabular DP must touch every one of them.
State dimensions
14.0
7 positions + 7 velocities
Discrete states
1.00e+14
Time for one sweep
0.00 million years
tabular methods are not an option
Set the widget to 7 degrees of freedom with 10 bins per dimension — a deliberately crude discretization of a modest research arm — and read the time for a single sweep. No faster computer rescues this; the curve is exponential in the exponent.
This is the first of Kober's four curses, and Chapter 14 develops all four properly. Its consequence for us is immediate and structural: we must stop enumerating states and start generalizing across them. That is Part II.
5.7 What DP still buys you
Before leaving, it is worth being clear that dynamic programming is not merely a pedagogical stepping stone.
It remains the correct tool whenever the state space is genuinely small — inventory control, discrete task planning, the high-level layer of a hierarchical robot controller. It is the ground truth against which approximate methods are measured: every algorithm in Chapters 6 through 12 is trying to approximate what DP computes exactly. And its theory transfers wholesale: the contraction argument, the improvement theorem, and GPI survive into the approximate setting, sometimes weakened but always recognizable.
Chapter 12 will bring DP back in a new guise, planning against a learned model rather than a given one — Bellman backups over dynamics the robot discovered for itself.
5.8 Chapter bridge
We assumed the blueprint. Rusty does not have one.
Real robots are handed no transition probabilities. Friction is unmeasured, payloads change, and the only access to the dynamics is to act and observe what happens. Chapter 6 removes the model entirely and asks whether anything survives. The answer is that a great deal does — Monte Carlo methods learn from complete episodes, temporal-difference methods learn from single transitions by bootstrapping from their own estimates, and the GPI pattern from this chapter carries over intact with sampling in place of expectation.
- 01Foundation●●●Jacobi versus Gauss–Seidel
Prove that the in-place policy-evaluation sweep still converges to v_π. Then construct a small MDP and a state ordering where in-place converges in strictly fewer sweeps, and one where the ordering makes almost no difference.
- 02Foundation●●●The improvement theorem, carefully
Reproduce the proof of Theorem 5.1, justifying every equality and inequality. In particular, state precisely why the remainder term γ^k v_π(S_{t+k}) vanishes and what would break if γ = 1.
- 03Foundation●●●Termination in finite time
Prove that policy iteration terminates after finitely many improvement steps. Then give an upper bound on the number of steps in terms of |S| and |A|, and explain why the observed number is usually far smaller.
- 04Foundation●●●The stopping bound
Derive the bound ‖v_π − v*‖∞ ≤ 2γΔ/(1−γ) where π is greedy with respect to a value function whose latest sweep changed by at most Δ. Where does the factor of 2 come from?
- 05Conceptual●●●The propagation wave
In the GPI dashboard, run value iteration and count how many sweeps pass before the cell farthest from the dock has a non-zero value. Relate this to the grid’s diameter, and explain why one-step backups make this inevitable.
- 06Conceptual●●●Two rhythms
Compare policy iteration and value iteration on the same problem. Record total sweeps to convergence for each at γ = 0.9 and γ = 0.99. Which degrades faster as γ → 1, and does the contraction bound predict it?
- 07Practical●●●Modified policy iteration
Implement modified policy iteration with a configurable number k of evaluation sweeps per improvement. Plot total backups to convergence as a function of k. There should be an interior optimum — find it and explain its existence.
- 08Practical●●●Measure the wall
Benchmark value iteration on grids from 10×10 up to as large as your machine tolerates, and fit the scaling. Then extrapolate to the 14-dimensional state of a 7-DoF arm and report the number honestly.
References
Baseline references
- Sutton, R. S. & Barto, A. G. (2018). Reinforcement Learning: An Introduction. MIT Press, 2nd editionChapter 4 in full: §4.1 policy evaluation, §4.2 policy improvement, §4.3 policy iteration, §4.4 value iteration, §4.5 asynchronous DP, §4.6 generalized policy iteration.
- Kober, J., Bagnell, J. A. & Peters, J. (2013). Reinforcement Learning in Robotics: A Survey. International Journal of Robotics Research 32(11)§3.1 the curse of dimensionality — the argument §5.6 turns into arithmetic.
Further reading & modern sources
- Bellman, R. (1957). Dynamic Programming. Princeton University PressThe principle of optimality, and the coining of "curse of dimensionality" by the person who had the most right to complain about it.
- Howard, R. A. (1960). Dynamic Programming and Markov Processes. MIT PressThe origin of policy iteration.
- Puterman, M. L. (1994). Markov Decision Processes: Discrete Stochastic Dynamic Programming. WileyModified policy iteration, convergence rates, and the complete theory of the methods sketched here.
- Bertsekas, D. P. (2012). Dynamic Programming and Optimal Control, Vol. II. Athena Scientific, 4th editionAsynchronous DP, and the approximate dynamic programming that bridges to Part II of this book.
