Chapter 8 · Sutton & Barto

Planning and learning

Dynamic programming plans with a model; Monte Carlo and TD learn from real experience. Both rest on the same operation, backing up values from successors, so they can be combined. An agent can learn a model from its experience and then rehearse that model to improve its values far faster than real experience alone allows.

01Models and planning

A model is anything the agent can use to predict how the environment responds to an action. A distribution model gives every possible next state and reward with its probability, which is what DP needs. A sample model produces one possible outcome at random, like a simulator. Sample models are usually far easier to obtain.

In this chapter planning means any computation that uses a model to improve a policy. The key observation: planning methods and learning methods differ only in where their experience comes from. Feed Q-learning simulated transitions drawn from a model instead of real ones and you have a planning method, random-sample one-step tabular Q-planning, which converges to the optimal policy for the model.

02Dyna: acting, learning and planning together

An online planning agent keeps interacting with the world while it plans. Each real transition can serve two purposes: improve the values and policy directly (direct RL), and improve the model, which then improves the values through planning (indirect RL). Dyna-Q does both on every step:

  1. In state S, choose A ε-greedily from Q; observe R and S′.
  2. Direct RL: Q(S, A) ← Q(S, A) + α [ R + γ maxa Q(S′, a) − Q(S, A) ].
  3. Model learning: remember Model(S, A) ← (R, S′). This assumes the world is deterministic.
  4. Planning, repeated n times: pick a previously seen state and an action previously taken there at random, ask the model for (R, S′), and apply the same Q-learning update.

With n = 0 this is plain Q-learning. Every planning step is cheap, so n can be large.

03Worked example: the Dyna maze

A 6×9 maze with a few walls (Sutton & Barto, Example 8.1). The agent starts at S and must reach G, which gives reward +1 and ends the episode; every other reward is 0. Moves into walls or the edge leave the agent in place. γ = 0.95, α = 0.1, ε = 0.1, and all values start at 0.

Dyna-Q

episode 1 · steps this episode 0 · model entries 0
5

Arrows: greedy action wherever some action value is nonzero. Amber: the real update. Orchid: states updated by planning on this step.

Steps per episode (this run)

What to try

Averaged over 30 runs

Steps per episode for n = 0, 5 and 50 planning steps

n = 0 (Q-learning)n = 5n = 50

Every curve starts at the same height because the first episode is pure exploration. After that, planning makes an enormous difference: with 50 planning steps the agent reaches its best path in about three episodes; without planning it takes about 25.

04When the model is wrong

A learned model can be wrong because the world is stochastic and few samples have been seen, because the environment has changed, or because function approximation generalised imperfectly. If the world gets worse (a path becomes blocked), the agent follows its plan, discovers the problem, and fixes the model. If the world gets better (a shortcut opens), a plain Dyna agent may never find out, because its model says the old path is best and it rarely explores.

Dyna-Q+ adds a curiosity bonus in planning: a transition last tried τ steps ago is replayed with reward r + κ√τ, so long-untested actions look attractive and get re-tried in the real world. This is a heuristic way to keep exploring in a changing environment.

05Prioritized sweeping

Uniformly random replay wastes effort: most updates back up values that are still zero. Better to work backward from states whose values just changed. Prioritized sweeping keeps a queue of state–action pairs ordered by how much their value would change, |R + γ maxa Q(S′, a) − Q(S, A)|. It updates the top pair, then checks every predecessor that leads into it and queues those whose priority now exceeds a threshold. On maze tasks it finds optimal solutions 5 to 10 times faster than Dyna-Q.

06Expected versus sample updates

An expected update averages over every possible next state, as in DP. A sample update uses one sampled next state, as in Q-learning. An expected update is exact but costs about b times as much, where b is the branching factor, the number of possible next states. Which is better for a fixed amount of computation?

If the successor values are already correct, one expected update removes all the error, while t sample updates (with α = 1/t) leave an RMS error of about √((b−1) / (bt)) relative to the spread of the successor values. The chart plots that formula against the computation spent, measured in units of one expected update.

RMS error vs computation, by branching factor b

With a large branching factor, a small fraction of the cost of one expected update already removes most of the error. Sample updates also start improving values sooner, and those improved values feed other updates. When b is large and there are many states, sample updates usually win.

07Where to focus the updates

DP sweeps every state equally, but in large problems most states are irrelevant to good behaviour. Trajectory sampling distributes updates according to the on-policy distribution, by simulating episodes and updating the states they visit. It focuses on states that actually occur, which helps a lot early on and in large problems. Real-time dynamic programming (RTDP) is value iteration done along simulated trajectories; it can find an optimal policy on the relevant states while never touching many others.

08Planning at decision time

So far planning improved a stored value table for all states, in the background. Decision-time planning instead starts from the current state and plans only to choose the next action, then usually throws the work away.

All the methods in Part I share three ideas: they estimate value functions, they back values up along actual or possible state trajectories, and they follow generalised policy iteration. They differ in whether updates are sample or expected, how deep they look before bootstrapping, and whether experience is real or simulated. Part II keeps all of this and drops the table.