Dynamic programming
If you know the MDP's dynamics exactly, you don't need experience at all: you can compute value functions and optimal policies by turning the Bellman equations into update rules and sweeping them over every state. That is dynamic programming (DP). It is rarely usable as is, but every learning method that follows is an attempt to get DP's result without DP's model.
01What DP assumes
DP needs a perfect model: the full dynamics p(s′, r | s, a) of a finite MDP. With it, each update can average over every possible next state and reward instead of sampling one. This is called an expected update, in contrast to the sample updates of Monte Carlo and TD.
02Policy evaluation
To find vπ for a given policy, start from any guess v0 (zeros, say) and repeatedly apply the Bellman expectation equation as an assignment:
One pass over all states is a sweep. The sequence vk converges to vπ as k → ∞. In practice you stop when the largest change in a sweep, Δ, falls below a small threshold θ. There are two ways to run a sweep:
- Synchronous (two arrays): compute every new value from the old array, then replace it all at once. This matches the equation exactly.
- In place (one array): overwrite each value immediately, so later states in the same sweep already use new values. This also converges, usually faster, and the order of states then matters.
03Policy improvement
Knowing vπ, is there a better policy? Look one step ahead from each state: qπ(s, a) = Σ p(s′, r | s, a)[r + γvπ(s′)]. If some action has qπ(s, a) > vπ(s), taking it once and then following π is better than following π throughout. The policy improvement theorem says that switching to that action every time it applies is better still, or at least as good, in every state. Acting greedily on qπ everywhere gives the improved policy:
If the greedy policy is no better than π, then vπ already satisfies the Bellman optimality equation, so π is optimal.
04Policy iteration
Alternate the two: evaluate π0, improve it to π1, evaluate π1, improve again, and so on. Each policy is strictly better than the last until one is optimal. A finite MDP has finitely many deterministic policies, so this ends in a finite number of iterations, often very few.
05Value iteration
Policy evaluation can take many sweeps, but you don't need to wait for it to converge before improving. Value iteration cuts evaluation down to a single update per state and folds the improvement into it by taking the max instead of the policy average. It is the Bellman optimality equation turned into an update rule:
It converges to v*. When it stops, the greedy policy with respect to the final values is optimal.
06Worked example: the 4×4 gridworld
This is Sutton & Barto's Example 4.1. The non-terminal states are numbered 1–14. The two shaded corners are one terminal state. Actions up, down, left and right move one cell deterministically; an action that would leave the grid leaves the agent where it is. Every transition gives reward −1, so the value of a state is minus the expected number of steps to finish. Under the equiprobable random policy, each action has probability 1/4. Step through one cell at a time to see each backup.
Sweeping the gridworld
Blue outline: the cell being updated. Amber outlines: the neighbours its backup reads. Teal underline: already updated in this sweep.
Largest change per sweep, Δ (log scale)
What to try
- Click Update next cell through the first sweep in place, then reset and do it synchronously. In place, cell 2 already sees cell 1's new value −1.00, so its own value drops further in the same sweep.
- Choose Synchronous and press Finish sweep three times. The greedy arrows are already optimal, though the values are far from converged: a good policy can emerge long before evaluation finishes. Then run to convergence: the values approach −14, −18, −20 and −22, minus the expected number of random steps to reach a corner.
- For policy iteration: run evaluation to convergence, click Improve policy, then evaluate again. The new policy's values are the optimal ones (−1, −2, −3), and a second improvement changes nothing. Policy iteration finished in one improvement here.
- Switch to value iteration and press Finish sweep a few times. It reaches v* in 3 sweeps; the 4th sweep confirms it with Δ = 0.
07Generalised policy iteration
Policy iteration and value iteration are two points on a range. You can do any number of evaluation sweeps between improvements, and you need not even sweep every state: asynchronous DP updates states in any order, as long as none is neglected forever, which lets you focus computation on the states that matter. All of these share one structure, which Sutton & Barto call generalised policy iteration (GPI): one process makes the value function consistent with the policy, the other makes the policy greedy with respect to the value function, and they interact until both stop changing, which can only happen at the optimum.
Nearly every method in reinforcement learning is a form of GPI. Monte Carlo control and Sarsa evaluate from sampled experience and improve with ε-greedy; Q-learning interleaves the two at every step, like a sampled value iteration.
08Strengths and limits
DP is efficient compared with the alternative: searching over policies directly would cost on the order of kn for n states and k actions, while DP takes time polynomial in n and k. Methods like these are routinely used on MDPs with millions of states.
The limits are the assumptions. DP needs the full model p, which most real problems don't provide. And each sweep touches every state, which is impossible when the state space is astronomically large (the curse of dimensionality). Monte Carlo methods remove the need for a model by averaging sampled returns. TD methods remove it too while keeping DP's key trick of bootstrapping: updating an estimate from other estimates.