Optimal Control and Planning
The course shifts gears from model-free RL to using models — and before models can be learned, you need to know what to do with one. Levine covers planning with known dynamics and no learning at all: black-box optimizers like random shooting and CEM, Monte Carlo tree search for stochastic discrete problems, and derivative-based trajectory optimization via LQR, extended to stochastic and nonlinear systems as iLQR/DDP and deployed inside model predictive control.
What changes when you know the dynamics
The first nine lectures were model-free: the algorithms only ever sampled from , never knew the probabilities, and never asked what would have happened under a different action — Q-learning was explicitly engineered to dodge that question. This lecture asks the opposite: what if the transition probabilities (“transition dynamics”, “dynamics”, “the model” — all names for ) are known?
Often they are. Atari, chess, and Go have rules programmed or written in a rulebook. Some systems are easy to model by hand — a car’s physics on a dark slippery road is hard, but its kinematics on a clean road without slippage is a few equations of motion that serve well in practice. Simulators technically come with their dynamics. And unknown dynamics can often be learned: system identification fits unknown parameters (masses, motor torques) into a known model scaffold, and fitting general-purpose models to transition data is where the model-based RL lectures are headed.
With no policy in the picture, the objective is over actions directly, and the dependence of future states on past actions enters as a constraint:
Open loop, closed loop, and the math exam
In the stochastic case the natural generalization is to condition a state distribution on the action sequence, (no policy term — the actions are given), and choose maximizing the expected reward. Reasonable — and, Levine claims, sometimes a very bad idea.
Closed-loop planning means responding with a policy , and the objective becomes exactly the RL objective — but the policy class need not be a neural network. A neural net is a global policy; if you’ll stay near a planned trajectory — a rocket perturbed by wind, corrected quickly — a local policy suffices, such as time-varying linear feedback that pushes back proportionally to the deviation. That form is much more common in optimal control, and LQR will hand us exactly it.
Black-box planning: guess-and-check and CEM
The first family of open-loop planners makes minimal assumptions — dynamics can be discrete or continuous, stochastic or deterministic, non-differentiable. These stochastic optimization or black-box methods abstract the temporal structure away entirely: concatenate the action sequence into one variable and maximize some .
The simplest version is guess and check, a.k.a. random shooting: sample from some distribution (even uniform) and pick . It sounds silly but works well for low-dimensional systems and short horizons, takes minutes to implement, and is extremely GPU-friendly: the candidates are a mini-batch through a (later, neural-network) model, and the argmax is a max reduction. The disadvantage is equally plain — you are relying on getting lucky.
The cross-entropy method (CEM) keeps the benefits and samples smarter, iteratively:
p = initial_distribution() # e.g. uniform / broad Gaussian over A
repeat:
A_1..A_N ~ p # sample action sequences
evaluate J(A_1) .. J(A_N) # rollouts through the model, in parallel
elites = top M of N by J # e.g. the best 10%
p = fit(elites) # max-likelihood refit, e.g. a Gaussian
Each refit focuses sampling on the region where the good sequences lie. With a large enough initial distribution and enough samples, CEM in general finds the global optimum (though “enough” may be prohibitive); it needs no derivatives, extends to discrete actions via other distribution classes, and CMA-ES adds momentum-style terms for better solutions at smaller population sizes. The hard limit is dimensionality: as a rule of thumb these methods struggle beyond roughly 30–60 dimensions — about 10 action dimensions over 15 time steps, stretched somewhat by correlation between successive steps — and they only ever produce open-loop plans.
Monte Carlo tree search
Discrete, stochastic problems — board games, Atari, poker, games of chance generally — call for a planner that does account for closed-loop feedback. Full tree search finds the optimal action but needs expansions exponential in the horizon . MCTS approximates a node’s value without expanding the tree below it: expand to some small depth, then run a cheap default policy — even a random one — and take the return of that rollout as a sample-based value estimate.
The generic sketch: (1) find a leaf using the tree policy — not a policy run in the world but a strategy for picking which node to expand; (2) evaluate the leaf with the default policy, re-executing the whole action sequence from the root (the same actions can land in different states, and you want values in expectation); (3) propagate the value and visit count up to the root. Repeat until the compute budget runs out, take the root action with the best average value — and typically replan from scratch at the next step. The standard tree policy is UCT: if a node isn’t fully expanded, expand a new action; otherwise recurse into the child with the best score,
— average value plus a bonus for rarely visited children: choose the nodes with the best return, but prefer the ones you know least about.
MCTS methods are surprisingly hard to analyze — few guarantees — but work very well in practice; Levine recommends the survey “A Survey of Monte Carlo Tree Search Methods.” Learn the default policy, evaluate leaves with value functions, take it to the extreme, and you get essentially what AlphaGo did.
Trajectory optimization with derivatives: LQR
When the dynamics are differentiable, use the derivatives. Notation switches to the optimal control convention — states , actions , costs instead of rewards. Substituting the constraint into the objective gives an unconstrained problem in the actions alone,
which you could attack with backpropagation using , , , . In practice first-order gradient descent works very poorly here: the last term multiplies many Jacobians together, so gradients vanish or explode, and the first action affects everything downstream while the last affects almost nothing — a huge spread of Hessian eigenvalues, i.e. terrible conditioning. Methods that optimize over actions only are called shooting methods (the first action “shoots” you across state space); collocation methods optimize over states and actions under constraints, condition much better, and suit first-order methods — but are more complex and set aside here. The classic second-order shooting method is the linear quadratic regulator: dynamics linear, cost quadratic (a linear cost has its minimum at infinity, so quadratic is the minimum interesting case), each possibly different per time step:
The same move recurses backward: at step , ; substituting the linear dynamics into the quadratic yields another quadratic, and the solve is identical in form to the base case with replaced by .
Stochastic and nonlinear: LQG, iLQR, DDP, and MPC
Gaussian noise costs nothing. If , exactly the same control law is optimal — a Gaussian is symmetric, so left-and-right deviations cancel in the quadratic cost (derivable via the closed form for a quadratic’s expectation under a Gaussian). But the states you visit are now random, so there is no single open-loop sequence — instead is a genuine closed-loop policy, a time-varying linear controller, and it is the optimal one in this linear-quadratic-Gaussian (LQG) setting. LQR quietly produces closed-loop plans.
Nonlinear systems: iterate. Approximate the system as linear-quadratic locally by Taylor expansion around the current best trajectory — first order for the dynamics, second order for the cost — in the deviation variables , . Run LQR on the deviations, then run the forward pass with the true nonlinear dynamics — using — so the states are the ones that will actually result; set to the new trajectory; repeat. This is iterative LQR (iLQR).
Newton-style jumps have a classic failure: the quadratic approximation is only trusted near the expansion point, and its optimum can land somewhere worse than where you started — the trust-region issue from the advanced policy gradients lecture. The fix lives in the forward pass: scale the open-loop term, . At the first action is exactly , hence , hence every action reproduces the old trajectory; as grows you deviate more. Search over — reduce until the cost improves, or until you capture a fraction of the model-anticipated improvement, or bracket — a small change that matters a lot in practice.
Case study. Tassa, Erez, and Todorov’s “Synthesis and Stabilization of Complex Behaviors through Online Trajectory Optimization” runs iLQR inside model predictive control: every time step, observe , plan a full action sequence, execute only the first action, discard the rest, and replan — “a fancy way of saying replan on every single time step.” With known dynamics and zero learning, the controller discovers acrobot swing-up in real time, an undulating swimming gait, a hopper that stands up and survives extreme perturbations, and rudimentary humanoid balancing and stepping (with a heavily engineered cost, since the horizon is short). Constant replanning even forgives a misspecified model — the hopper still hops with double or half the mass the controller believes. That is the motivation for next week: replace the known model with a learned one.
Check yourself
Exercises in the lecture’s spirit — planning, search, and control on paper.
A two-step problem: at you are in state and choose ; nature then flips a fair coin revealing , and at you choose . The reward is 1 if matches the coin ( for heads, for tails) and 0 otherwise; has no effect. Compute the best achievable expected reward for (a) an open-loop planner committing to before the coin flip and (b) a closed-loop policy. Which lecture example is this, and what structural feature of the problem creates the gap?
Show solution
(a) Any fixed matches the coin with probability , so the best open-loop expected reward is — the planner must average over , and no committed is right in both outcomes. (b) A closed-loop policy observes and plays on heads, on tails: expected reward . This is the math exam in miniature — you know the answer to every question but not which question will be asked. The gap exists exactly because information useful for acting (the coin’s outcome) is revealed between the commitment point and the action; in a deterministic problem is a known function of , nothing new is revealed, and open loop matches closed loop.
You run CEM on a scalar action with samples and elites. The samples and returns are: with ; with ; with ; with ; with . Fit a Gaussian to the elites (max-likelihood: sample mean and variance) and give the next sampling distribution. Then explain why CEM with a Gaussian can converge prematurely, and what property of the initial distribution the lecture’s global-optimum guarantee relies on.
Show solution
The elites are the top two by return: () and (). The max-likelihood Gaussian has mean and variance , so the next iteration samples from — sampling now focuses on the region where the good samples lay. Premature convergence: each refit shrinks the variance toward the spread of the current elites, so if early samples miss a distant better optimum, the distribution can collapse around a local one and the vanishing variance means it effectively never samples far enough away to discover the mistake. The lecture’s guarantee requires a large enough initial distribution (broad enough to cover the optimum) and enough samples per iteration — with too few of either, “enough” is exactly what you lose.
A root node has been visited times and has two children: child A with total value , , and child B with , . Using , which child does UCT descend into for ? For which values of does the choice flip? What does each regime of correspond to?
Show solution
With : child A scores ; child B scores . At : A , B — descend into B, which has both the higher average (12 vs. 10) and the fewer visits, so the choice holds for every : setting the scores equal gives , i.e. , which is negative — no valid flips it. (If A had the higher average, small would exploit A while large would explore B.) Small weights average return — exploitation; large weights the rare-visit bonus — exploration. The bonus’s numerator grows as the parent accumulates visits, so a child left unvisited eventually gets tried no matter how bad it looks.
Scalar system: dynamics (so , ), cost , horizon . Run the LQR backward pass by hand: find , the value function , then . Then, given , run the forward pass.
Show solution
Here , , at every step. Step : minimizes , so , , i.e. . Substituting back: , so , . Step : , so , , , and . Then , . Forward pass from : ; ; . Total cost: . Sanity check on the structure: the controller spends control effort at to shrink the state it must pay for at , and it takes no action at the final step because (with no terminal state cost beyond , where is already fixed) any only adds cost. Note the whole solve never needed until the forward pass — exactly the unzip-backward, zip-forward structure.
In iLQR’s modified forward pass, with . (a) Prove by induction that reproduces the previous trajectory exactly. (b) Explain what problem the search over solves, and why the forward pass — not the backward pass — is the right place for it.
Show solution
(a) Base case: is the given initial state, the same in every iteration, so , and with , . Inductive step: if and , then (the forward pass uses the true dynamics, which also generated the old trajectory), so and . By induction the whole old trajectory is reproduced; as shrinks toward 0 the new trajectory continuously approaches the old one. (b) The backward pass optimizes a Taylor approximation that is only trustworthy near ; jumping to the approximation’s optimum — as raw Newton’s method does — can land at a point where the true cost is worse. Scaling interpolates between the old trajectory and the full step, so reducing it until the true cost improves (or a fraction of the predicted improvement is realized) is a backtracking line search enforcing trust. It must live in the forward pass because only the forward pass evaluates the true nonlinear dynamics and cost — the backward pass only ever sees the linear-quadratic approximation, which by construction believes its own optimum is great.