5.2.14 · D4Deep & Advanced RL

Exercises — Model-based RL overview

2,906 words13 min readBack to topic

Before we start, one reminder of the notation the parent note built, so nothing here is a surprise:


Level 1 — Recognition

Exercise 1.1

Classify each agent as model-free or model-based: (a) A robot that stores every observed tuple, fits , and searches action sequences inside before moving. (b) A Q-learning agent that only updates from real transitions and never predicts . (c) Dyna-Q, which does real Q-updates and extra updates from a learned model.

Recall Solution 1.1

(a) Model-based. It predicts the next state () and plans inside that prediction — the defining move of MBRL. (b) Model-free. It never builds or ; it learns values straight from real experience. (c) Both / hybrid — but counts as model-based. Dyna keeps a learned model to amplify data (generate imagined transitions) on top of real ones. Because it uses a model, it is a model-based method, even though the value updates look model-free.

Exercise 1.2

The parent note says MBRL "trades more computation for less real data." Which single word names the property MBRL improves?

Recall Solution 1.2

Sample efficiency — how much good behaviour you get per real environment interaction. MBRL spends extra CPU (planning/imagining) to squeeze more learning out of each expensive real step.


Level 2 — Application

Exercise 2.1 — Fit a linear model by least squares

Environment collects two clean transitions from : Fit . Find giving zero squared-error loss.

Recall Solution 2.1

WHAT we do: solve the two equations that force each prediction to equal its target. WHY: with two clean, non-redundant samples, ordinary least-squares regression (fitting to the observed targets, as derived in Exercise 5.1) reduces to solving a linear system exactly. From the second equation . Substitute into the first: Then . Answer: , i.e. — the true dynamics recovered, loss . The model is now a perfect simulator.

Exercise 2.2 — The delta trick

Same dynamics. Instead of predicting , predict with (no term). What is , and how do you recover ?

Recall Solution 2.2

Targets are for both samples: at ; at . So needs . Recover by adding back: . Identical model, but the regression target had smaller magnitude and near-zero mean — easier and more stable to fit. See Uncertainty and Ensembles in RL for why stable residuals also make ensemble spread meaningful.

Exercise 2.3 — One-step MPC

Learned model , reward (want next state near ). Current , actions from , horizon . Pick the best .

Recall Solution 2.3

Predict each next state and its reward:

Highest reward is at (it lands exactly on ). Best action: . This is MPC with : search inside the model, execute the first (only) action.


Level 3 — Analysis

Exercise 3.1 — Compounding error

A model has per-step prediction error bounded by (in state units), and error grows roughly linearly: predicted-vs-true gap after steps . Over a horizon , estimate the end-of-rollout error. Then compare .

Figure — Model-based RL overview
Recall Solution 3.1

WHAT/WHY: each imagined step feeds its (slightly wrong) output into the next, so errors accumulate. This is the parent note's "compounding" enemy.

  • : end error state units — potentially huge relative to a state near .
  • : end error . Conclusion: the long rollout's final state is untrustworthy; the short one stays tight. This is exactly why MPC re-plans with a short horizon instead of committing a long imagined plan.

Exercise 3.2 — Why re-plan?

In Exercise 2.3 you planned from . Suppose the true dynamics are actually (a bias your model didn't capture). What is the real after executing , and why does re-planning save you?

Recall Solution 3.2

Real next state: (not the imagined ). The model thought it landed on ; reality is off by — the model's bias. Re-planning fix: at the next step MPC observes the real (not its imagined ) and plans afresh. So it corrects the bias instead of blindly following a stale imagined trajectory. Re-planning turns a compounding error into a one-step error each time.

Exercise 3.3 — Given vs learned

For each task, say whether the model is naturally given or must be learned, and where the hard part lives: (a) Chess. (b) Balancing a real inverted pendulum. (c) A gridworld with published transition rules.

Recall Solution 3.3

(a) Chess — given. Rules are exact; the hard part is planning/search (huge tree). See MuZero and Dreamer for learning a model even when rules exist, to plan in a compact latent space. (b) Pendulum — learned. You don't know friction/mass exactly; you regress from data. Hard part: model error + compounding. (c) Gridworld with rules — given. The transition probabilities and rewards are known tables; hard part is just planning (e.g. value iteration via the Bellman Equation).


Level 4 — Synthesis

Exercise 4.1 — Bellman inside the model

A tiny MDP has two states . In the learned model:

  • From : action gives , goes to ; action gives , goes to .
  • is terminal: .

With , compute using

Recall Solution 4.1

Evaluate each action from :

  • : .
  • : .

Take the max. Guess is optimal (it grabs reward and ends). If so . Check that isn't better: . ✅ Consistent. Answer: , optimal action . The whole calculation used only the model's — zero real interaction.

Exercise 4.2 — Two-step MPC with discount

Model , reward (evaluated on the state you land in), , , , actions in . Objective: Find the best action sequence and execute only .

Figure — Model-based RL overview
Recall Solution 4.2

Exhaustive search: with gives sequences. We evaluate all nine (no pruning) — MPC's brute force is honest. With and , the objective is :

objective

Best total at . Execute , then re-plan from the real next state. Why the discount matters: halves the second term's weight, so landing on immediately () dominates — the near future counts more.

Exercise 4.3 — Dyna as a data amplifier

Real experience: transitions. A learned model can generate imagined transitions per real one for extra Q-updates. If you want total training transitions per outer loop, what is ? Name the risk of choosing too large.

Recall Solution 4.3

Total . Risk: if is huge and the model is imperfect, the flood of imagined transitions drowns the real ones, so Q-learning converges to the value function of the fantasy model, not reality. Balance against model accuracy (use ensemble uncertainty to gate imagined data).


Level 5 — Mastery

Exercise 5.1 — Derive the least-squares loss from Gaussian MLE

Assume with (a -dimensional Gaussian, where is the number of components in the state vector and is the noise standard deviation). Starting from the Gaussian density, show that maximizing the log-likelihood of a dataset is equivalent to minimizing mean squared error .

Recall Solution 5.1

Step 1 — one-sample density. For a Gaussian with mean and covariance (the identity matrix scaled by the noise variance ): Step 2 — log it (log turns the product over samples into a sum, and kills the exponential): Step 3 — sum over the dataset (independent samples ⇒ log-likelihood is the sum): Step 4 — drop constants. The second term has no ; the factor is a positive constant. Maximizing over is therefore the same as minimizing WHY it matters: the squared-error loss isn't arbitrary — it is Gaussian maximum likelihood. Change the noise assumption and the loss changes with it.

Exercise 5.2 — Bound the imagined-return error

Suppose the true per-step reward is bounded , and per-step model error causes the imagined reward to differ from the true reward by at most at each step (i.e. ). For a horizon- discounted rollout with discount , give an upper bound on the total imagined-return error, and take the limit .

Recall Solution 5.2

Total imagined return . Each term's reward error is at most , so by the triangle inequality As (with , so ): Interpretation: even with infinitely long imagination the discounted error stays bounded by — because discounting silences the far future. This is the mathematical reason short-horizon MPC + discounting keeps compounding error tame. (Numerically with : bound .)

Exercise 5.3 — Design the full MBRL loop

Design (in words + a diagram) a Dyna-style MBRL loop for a robot arm, and justify one safeguard against each of the two classic failure modes (compounding error; over-trusting imagined data).

Recall Solution 5.3

Loop:

collect s a s' r

Act in real env

Store dataset D

Fit model f and r by regression

Plan with short horizon MPC

Generate imagined transitions

Update value or policy

Safeguards:

  1. Against compounding error: short MPC horizon + re-plan every step (Ex 3.2, Ex 5.2 show the bound). The robot never blindly follows a long imagined plan.
  2. Against over-trusting imagination: train a model ensemble; where members disagree (high epistemic uncertainty), penalize or discard imagined data so the policy isn't optimized against a confident-but-wrong fantasy. Reward-focused models (MuZero and Dreamer) further help: predict only what matters for decisions, not every pixel.