TL;DR
Model-based reinforcement learning first learns a model of the environment: given a state and an action, predict the next state and the reward. The agent then improves its behaviour by using that model. It can plan ahead inside the model, or it can generate imagined experience to train a policy. Real interaction is spent on learning the model, and the model is reused many times.
The payoff is sample efficiency. A robot that can afford only a few thousand real steps can still plan with millions of imagined ones, as long as the model is accurate enough.
Where you'll see it: in robot control where real trials are slow or costly, in planning from video with PlaNet and Dreamer, and in game agents such as MuZero, which plans with a learned model.
Intuition
Practising in your head
A chess player does not learn only by playing games. Before moving a piece, she imagines the reply, then the reply to that, and rejects lines that lose. Each imagined move costs nothing, so she can look at far more lines than she could play. Her picture of the board and its rules is a model.
Model-free reinforcement learning skips the picture. It learns what to do directly from each real outcome, which is reliable but hungry for data. Model-based reinforcement learning spends real experience to learn the rules, then uses the rules to save experience.
The two uses split the field. In planning, the agent searches through imagined futures at decision time, the way model predictive control does. In Dyna-style learning, the agent replays imagined transitions to update its value estimates between real steps. Dyna, proposed by Richard Sutton in the early 1990s, is the simplest version of the second idea.
flowchart LR A[Agent acts in the real environment] --> B[Store real transitions] B --> C[Fit a model to predict next state and reward] C --> D[Plan or imagine rollouts inside the model] D --> E[Improve the policy or value estimates] E --> A
The loop is the whole idea. Real data improves the model, the model produces cheap data that improves the policy, and the better policy collects more useful real data.
The math
Setting
Take a Markov decision process with unknown transition probabilities and unknown reward function . The discount factor says how much a reward steps away is worth, relative to a reward now: .
The agent wants a policy that maximises the expected discounted return
where the expectation is over the trajectory generated by following . Model-free methods estimate or its gradient from real trajectories. Model-based methods build an approximation and and work with those instead.
Learning the model
The model is an ordinary supervised learning problem. The inputs are state-action pairs . The targets are the observed next state and reward . If the model is probabilistic, we maximise the likelihood of the observed transitions:
This is maximum likelihood again. For a discrete problem the answer is just counts: equals the number of times followed , divided by the number of times was tried. For a continuous state, the model is a neural network that outputs a mean and variance, or a mixture density network when the next state can have several plausible values.
Planning inside the model
With a model in hand, the agent can estimate the return of a plan without touching the environment. Take a rollout of steps from state under a sequence of actions:
where , each and come from the model, and is a value function that estimates the return after the horizon. Setting leaves only , with no planning at all; larger trusts the model for more steps. A planner, such as a random search, the cross-entropy method, or tree search, chooses the action sequence with the highest estimated return and executes its first action.
Error compounds
Here is the central difficulty. Suppose each predicted reward is off by at most , and the errors add up along the rollout. The accumulated error after a horizon is then at most
The error grows with and levels off at . Long imagined rollouts can therefore be dominated by model error, even when each single step looks accurate. This is the reason most practical systems use short horizons, or re-plan often from real observations.
- per-step error 0.5, gamma 0.9
Model-free and model-based in one line
A useful way to see the tradeoff: model-free learning estimates from real samples at a cost of one real step per sample. Model-based learning spends real steps on the model and then gets roughly imagined samples per real step, for some . The gain is large when is large and the model error stays small over the horizon used.
Worked examples
Example 1: estimating a transition from counts
An agent in state takes action "go" ten times and records where it lands: 6 times in , 3 times in , and once back in .
- Total tries of : .
- Estimated probabilities: , , and .
- Check: , so the estimate is a valid distribution.
- Interpretation: ten real steps give a usable model of this one state-action pair. The estimate is noisy. With only ten tries, the standard error of is about , so the model is far from certain. A planner that trusts it blindly may favour the wrong branch, which is why later sections matter.
Example 2: a planning rollout with a model error
Two steps from state , with discount and the value of the state after the horizon taken as .
- Plan: take action , then action . The model predicts reward for , then for .
- Model return: .
- The real environment gives for , because the model overestimated that reward by . The true return of the same plan is .
- Error: . The planner believes this plan is better by than it really is.
- Interpretation: a planner that picks the plan with the highest model return will be biased toward the plan whose model is most optimistic. This is called model exploitation, and it is the main reason the method can fail when the model is trained on too little data.
Tradeoffs and failure modes
| situation | what goes wrong | better choice |
|---|---|---|
| Real steps are scarce or expensive | Model-free methods need too many of them | Model-based learning, with a short planning horizon |
| Long horizon with an imperfect model | Error compounds, as the plot shows | Short rollouts, frequent re-planning, or a latent model with multi-step training |
| Planner searches hard against the model | Exploits model errors, finds plans that only the model likes | Ensembles to measure disagreement, or penalise uncertain transitions |
| Rich observations such as images | Predicting every pixel wastes capacity and misses what matters | Learn a compact latent state, as in PlaNet and Dreamer |
| Environment is cheap to simulate | Model adds complexity and a new source of bias | Model-free methods such as [[proximal-policy-optimization |
| Model is a deterministic regression on noisy data | Averages over futures and predicts a blurred state | Probabilistic model that outputs a distribution |
The field's standard fix for the exploitation problem is to keep the model honest about what it does not know. An ensemble of models gives a spread of predictions, and a planner can prefer plans where the models agree.
History
Model-based ideas go back to optimal control, where engineers had a physics model of the plant long before learning was involved. Within reinforcement learning, Sutton's Dyna architecture in the early 1990s combined learning the model with updating value estimates from imagined transitions.
In the 2010s, the probabilistic Gaussian-process model in PILCO showed that a small number of real trials could learn a control policy on a physical system. Neural network models then scaled the idea to images. PlaNet (2018) planned in a learned latent space with the cross-entropy method, and Dreamer (2019) trained a policy entirely on rollouts imagined in its latent model. MuZero later showed that a model trained only to predict quantities relevant to planning, rather than the full observation, could reach strong game play.
Check yourself
QuestionWhat is the main reason a model-based agent can use fewer real steps than a model-free one?Show answer
Each real transition is used twice: once to fit the model and again indirectly through the model's imagined rollouts. The imagined rollouts cost computation rather than real interaction, so the agent gets many more learning samples per real step, provided the model is accurate enough.
QuestionAn agent sees the state-action pair go to in 4 of 10 tries and to in the other 6. What does the count-based model predict for ?Show answer
. The complementary probability makes the distribution sum to 1.
QuestionWith and a per-step reward error of , what is the bound on the accumulated error for a horizon ? Why does it stop growing as increases?Show answer
. It stops growing because the discount shrinks each later error term. The infinite sum converges to , so later steps add less and less.
QuestionYou plan with a learned model and the chosen plan performs much worse in the real environment than the model predicted. Name the likely cause and one change that would reduce it.Show answer
The planner exploited an error in the model: it selected a plan whose predicted return was high only because the model was wrong there. A useful change is to measure the model's uncertainty, for example with an ensemble, and penalise or avoid plans that lead into regions where the models disagree. Shorter horizons and more real data collected near the chosen plan also help.
Sources
- Sutton and Barto, Reinforcement Learning: An Introduction, 2nd edition (2018), chapter 8 on planning and learning.
- Learning Latent Dynamics for Planning from Pixels (Hafner et al., 2018)
- Dream to Control: Learning Behaviors by Latent Imagination (Hafner et al., 2019)