Chapter 4 · Sutton & Barto

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:

Iterative policy evaluationvk+1(s) = Σa π(a|s) Σs′, r p(s′, r | s, a) [ r + γ vk(s′) ]

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:

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:

Greedy policy improvementπ′(s) = argmaxa Σs′, r p(s′, r | s, a) [ r + γ vπ(s′) ]

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.

Policy iterationπ0 →E vπ0 →I π1 →E vπ1 →I π2 → … →I π* →E v*

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:

Value iterationvk+1(s) = maxa Σs′, r p(s′, r | s, a) [ r + γ vk(s′) ]

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

sweep 0 · Δ this sweep – · policy iterations 0
1.00

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

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.