Plan the whole control schedule before the robot moves. On this drifting hovercraft, iLQR finds the cheapest schedule in 2 iterations. Gradient descent on the same cost needs about 100.
Press Optimize and watch both planners bend the path. Then raise the effort cost and run it again. After that we build each piece: the problem, the backward pass, iLQR, Newton's method and trust regions.
You need matrix multiply and inverse, a little Python, and the idea of cost-to-go from Lecture 7. We build the rest from zero.
Lecture 13 of 25 in Introduction to Robot Learning, CMU 16-831 (Spring 2024), taught by Guanya Shi. Slides on the course lectures page. Every idea is re-taught here in our own words and devices. Before this: Lecture 12, Model-Based Control Basics.
Illustrative toy. The hovercraft starts drifting north at 2.2 m/s with air drag. It must stop on the pad 4 m east after 4 s (40 steps of 0.1 s). Cost: effort times the sum of squared thrusts, plus misses at the end. Both planners start from zero thrust.
Chapter 0
Choose every control before you act, and let a known model carry the plan
You throw a ball into a bucket 6 m away. Once the ball leaves your hand, you cannot steer it. So you pick the angle and the speed first, and physics does the rest.
That is trajectory optimization: choose a whole sequence of controls, before acting, so that a known model carries the system to the goal at the lowest cost. It is also called open-loop optimal control or planning. Open loop means the plan does not look at the world while it runs.
Set the angle and the speed, then watch where the ball lands. Press Plan to let an optimizer pick both.
Illustrative toy with no air drag and g = 9.81 m/s². The planner minimizes 5 × miss² + 0.01 × speed², so it wants the slowest throw that still lands.
Many throws land in the bucket. The planner prefers the one that costs least, here the slowest: 45° at about 7.7 m/s. The range is speed² × sin(2 × angle) / g, and 45° gives the most range per unit of speed. Every method in this lesson trades the same two costs: reach the goal, and spend little effort.
Planning is possible because of cost-to-go: the cheapest cost from a state to the end. If you know what the rest of the trip costs, you can weigh "cost now" against "cost later" at each step.
The symptom is a plan that is perfect on paper and misses in the world. To measure it, run the plan with a small push and record the miss at the end. The fix is feedback: LQR returns gains that correct the plan (Chapters 3 and 8), and the next lecture plans again at every step.
Chapter 1
Write one optimization that every method in the lesson attacks, and find what makes it hard
Every algorithm here solves one problem. Name its parts first.
The state x-t is everything you need to know at step t, such as position and speed. The control u-t is what you command, such as a thrust. The cost c-t scores one step. The dynamics f give the next state. The horizon T is the number of steps.
Minimize over x and u the sum over t of c-t of x-t and u-t, such that x-t-plus-one equals f of x-t and u-t.
Pick the controls u-t. Run them through f to get the states x-t. Add up the step costs c-t. Find the controls that make the total smallest.
With made-up numbers: f(x, u) = 2x + u, step cost ½x² + ½u², x-1 = 2, T = 3. Try u-1 = −3 and u-2 = −1. Then x-2 = 1 and x-3 = 1. Total = ½(4 + 9) + ½(1 + 1) + ½(1) = 8. Chapter 4 shows no schedule does better.
The dynamics constraint is the whole difficulty. Without it, you set every state to the goal at zero cost. With it, f threads the path together, one step at a time. Real problems add constraints too: stay out of the obstacle, keep thrust under the motor limit.
Some problems are convex: the average of two allowed paths is always an allowed path. Then any local minimum is the global one. An obstacle breaks this, even with linear dynamics.
The top and bottom paths both miss the obstacle and both obey the dynamics. Slide the blend between them and watch the middle.
Illustrative toy: a mass that you push (double integrator), rest to rest from (0, 0) to (10, 0). Effort is the sum of squared pushes. A blend of two paths uses the blend of their pushes, so it obeys the same linear dynamics.
The 50% blend is the cheapest path on the slider, and it runs through the obstacle. "Go left or go right" is a choice, not a slope. That one fact decides the tool:
| Problem | Tool | Where |
|---|---|---|
| Linear dynamics, quadratic cost, no constraints | LQR, exact in one backward pass | Chapters 3, 4 |
| Nonlinear dynamics | iLQR, DDP: linearize, solve LQR, repeat | Chapters 5, 6 |
| Constraints such as obstacles | SCP, SQP: convexify, solve a QP, repeat | Chapter 7 |
The symptom is a solver that returns a bad path with full confidence. To measure it, start the solver from two different first guesses and compare the costs. The fix is a better first guess, or a method that searches more widely (Lecture 14).
Chapter 2
Choose the variables a solver sees: controls only, or states and controls tied by defects
Before you solve, decide what the variables are. There are two classic answers.
In single shooting, only the controls are variables. You get the states by simulation: apply u-1 to x-1 through f, then u-2, and so on. The dynamics hold by construction. The name comes from artillery: aim, fire, see where it lands, adjust.
The flaw is sensitivity. A push at step 1 changes every later state, so early controls swing the end far more than late ones.
Shooting: nudge one control and the whole path after it moves. Collocation: the states are free dots, and red gaps show where they break the dynamics. Drag the solver's progress to close them.
Illustrative toy: x-next = x + v and v-next = v + u, with 1 s steps, from rest at 0 to rest at 10 in 15 steps. The grey plan is the least-effort answer. The progress slider blends the first guess into it; a real solver takes its own route.
In direct collocation, both states and controls are variables. The solver may propose any states, so the dynamics become explicit constraints. Call the mismatch at step t the defect d-t.
d-t equals x-t-plus-one minus f of x-t and u-t, and it must equal zero.
For each step, apply f to the proposed state x-t and control u-t. Subtract that from the proposed next state x-t-plus-one. The solver must drive every defect d-t to zero.
From the device's first guess: x-t = 4.00 m, speed 0, push 0, so f gives 4.00 m. The guess says x-t-plus-one = 4.67 m. d-t = 4.67 − 4.00 = 0.67 m. The guess is not physical yet.
| Encoding | Variables | Dynamics | Good at |
|---|---|---|---|
| Single shooting | T controls | hold by simulation | short horizons, simple code |
| Collocation | T states + T controls | defect constraints | long horizons; each defect touches only two neighbours |
Chapters 3 to 6 use the shooting view, because it makes the dynamic programming clean. Chapter 7 handles constraints in the collocation spirit. In the lab, shooting meets the simplest improver: sample control sequences, keep the cheapest, refit.
The symptom is a shooting solver that stalls on a long horizon. To measure it, compare how far the end moves for the same nudge at step 1 and at the last step. The fix is collocation, or a method that uses the time structure, like LQR.
Chapter 3
Derive the LQR gain from one Bellman step, and see why the best control is a line in the state
One special case has an exact answer. The Linear Quadratic Regulator (LQR) assumes three things: linear dynamics x-t-plus-one = A x-t + B u-t, a quadratic cost ½xᵀQx + ½uᵀRu, and no constraints. A moves the state alone, B carries the push. Q prices distance from zero, R prices effort. Lecture 12 used LQR as a controller; here we derive it.
Work backward from the end. Call the cost-to-go from state x at step t V-t(x). The guess, which the algebra confirms, is a bowl: V-t(x) = ½xᵀP-t x for some matrix P-t. At the last step there is no future, so P-T = Q. The Bellman recursion links one step to the next.
V-t of x equals the min over u of: one half x-transpose Q x, plus one half u-transpose R u, plus V-t-plus-one of A x plus B u.
For each control u, add the cost now to the cost-to-go from the landing state Ax + Bu. Pick the u with the smallest total. That total is V-t(x).
With made-up numbers: A = 2, B = 1, Q = R = 1, P-t-plus-one = 1, x = 1. Total(u) = ½ + ½u² + ½(2 + u)². The bottom is at u = −1, with total 1.5. So V-t(1) = 1.5, which is ½ × 3 × 1²: P-t = 3.
The yellow curve is the total over every control u. Move the state x and watch the best control slide along a straight line. Raise P-t-plus-one or R and the line tilts.
Scalar toy with A = 2 and B = 1: the state doubles each step unless you push. Q = 1.
The total is a bowl in u. Set its slope to zero: (R + BᵀP-t-plus-one B) u + BᵀP-t-plus-one A x = 0. Solve for u.
u-star equals minus K-t x, where K-t equals the inverse of R plus B-transpose P-t-plus-one B, times B-transpose P-t-plus-one A.
Take what a push saves later, BᵀP-t-plus-one A. Divide by what a push costs, R + BᵀP-t-plus-one B. That ratio is K-t. Push with minus K-t times the state.
Same made-up numbers: K-t = (1 + 1 × 1 × 1)−1 × 1 × 1 × 2 = 2 / 2 = 1. At x = 1, u* = −1, as the bowl showed. Double R to 2 and K-t drops to 2 / 3.
The symptom is a gain that explodes or flips sign. To measure it, print R + BᵀP B at each step; it must stay positive definite. The fix is a positive definite R, so the bowl always has a bottom.
Chapter 4
Sweep the cost-to-go matrix from the end to the start, then check it against a forward rollout
Put u* back into the Bellman step. The total is a bowl in x again, which confirms the guess. Its matrix is the new P-t. Call the price of pushing S-t = R + BᵀP-t-plus-one B.
P-t equals Q, plus A-transpose P-t-plus-one A, minus K-t-transpose S-t K-t.
Start from P-T = Q. At each step back, add the cost now Q to the do-nothing future AᵀP-t-plus-one A. Subtract the saving K-tᵀS-t K-t. Store K-t on the way. This update is the discrete-time Riccati equation.
With made-up numbers A = 2, B = 1, Q = R = 1, T = 3. P-3 = 1. Step 2: S = 2, K = 1, P-2 = 1 + 4 − 2 = 3. Step 1: S = 4, K = 3 × 2 / 4 = 1.5, P-1 = 1 + 12 − 9 = 4.
Check it forward.
Start at x-1 = 2. Push u-1 = −1.5 × 2 = −3, landing at x-2 = 4 − 3 = 1. Push u-2 = −1, landing at x-3 = 1. The cost is ½(4 + 9 + 1 + 1 + 1) = 8. The backward pass predicted ½P-1 x-1² = ½ × 4 × 4 = 8. Doing nothing costs ½(4 + 16 + 64) = 42.
Yellow bars are P-t, filled from the right. The purple line is the gain K-t. Press Sweep, then lengthen the horizon and watch both settle away from the end.
Scalar toy: A = 2, B = 1, Q = 1, start x-1 = 2. The read-out rolls the gains forward and compares the cost with ½P-1 x-1².
With R = 1, P settles at 2 + √5 = 4.24 and K at 1.62. That limit is the infinite-horizon LQR of Lecture 12. Two more facts make LQR the building block:
The symptom is a backward pass whose forward cost does not match ½xᵀP x. To measure it, roll the gains forward and compare the two numbers. The usual fix is an index error: K-t must use P-t-plus-one, not P-t.
Chapter 5
Turn a nonlinear problem into a string of LQR problems around the current guess
Real robots are not linear. A pendulum has a sine in it. The hovercraft has drag that grows with speed squared. LQR is exact only for the linear case. Iterative LQR (iLQR) uses it anyway, around the current guess:
The forward pass applies this rule at each step. The bar over a letter means "on the current path".
u-t equals u-bar-t, minus K-t times x-t minus x-bar-t, minus alpha k-t.
Start from the old control u-bar-t. Add the improvement alpha k-t. Then correct for drift: if the true state x-t has left the old path x-bar-t, push back with K-t. If the cost went up, halve alpha and roll again.
With made-up numbers: u-bar-t = 0, K-t = 0.9, x-t − x-bar-t = 0.5, alpha = 1, k-t = −1.2. u-t = 0 − 0.45 + 1.2 = 0.75. The feedback term took 0.45 off the planned push.
Grey is the current path. Pink dashes are where the linear model predicts the new controls will go. Teal is where the true dynamics take them. Press Iterate.
The hero's hovercraft with effort 0.05. The prediction uses the linearized A-t and B-t of the grey path. The dynamics are exact; the drag is a toy.
At drag 1, the first prediction ends 6.5 m east, but the true rollout ends at 3.7 m. The cost still falls from 210 to 3.3, because the forward pass runs on the true model with K-t correcting the drift. Set drag to 0 and iLQR is plain LQR: one iteration and done.
The symptom is a cost that goes up after an iteration. To measure it, log the cost at every alpha tried. The fix is the line search: halve alpha until the cost falls. If R + BᵀPB is near singular, add a small number to its diagonal.
Chapter 6
See why a step scaled by curvature beats a step scaled by one learning rate
In the hero, gradient descent needed about 100 iterations and iLQR needed 2. The reason is Newton's method. Say you minimize a function h of one number a. Call its slope at the current point g and its curvature H. Newton fits a parabola there and jumps to its bottom.
a-next equals a minus g over H.
Measure the slope g and the curvature H. Step against the slope, divided by the curvature. A flat direction gets a long step, a steep one a short step. Gradient descent uses one learning rate eta instead: a-next = a − eta g.
The device's function at a = −2.5: g = −8.75 and H = 8.5. a-next = −2.5 + 8.75 / 8.5 = −1.47. The best answer is a = 0.
Grey is gradient descent with the largest safe learning rate. Teal is Newton. Stretch the bowl, then press Run.
Illustrative function h(a, b) = ½(a² + stretch × b²) + 0.1a⁴, started at (−2.5, 1). The learning rate is 1.8 divided by the largest curvature on the way, just under the limit of 2 where it diverges. "Done" means the slope is under 0.001.
Gradient descent must keep its learning rate safe for the steepest direction, so it crawls along the flat one. A shooting cost has just this stretch: early controls move the end far more than late ones (Chapter 2).
Write the cost with the states replaced by the dynamics: c-1(x-1, u-1) + c-2(f(x-1, u-1), u-2) + ... Each term nests f one level deeper. iLQR is an approximate Newton step on it. The Riccati sweep builds and inverts its curvature in one pass that grows linearly with T.
| Method | Uses of the dynamics | Step |
|---|---|---|
| Gradient descent | first derivatives, for the slope only | one learning rate |
| iLQR | first derivatives A-t, B-t | approximate Newton |
| DDP (differential dynamic programming) | first and second derivatives of f | full Newton |
DDP keeps how f itself curves. It is the exact second-order method. In practice iLQR is often preferred: second derivatives of f are costly and can make the step unstable, and iLQR converges well already. Both solve an unconstrained LQR each iteration.
The symptom is a Newton step that goes uphill. To measure it, check the sign of the curvature; a negative H points the step to a maximum. The fix is damping: add a positive number to H, which iLQR does as regularization.
Chapter 7
Replace a round obstacle with a straight line near the current path, solve, and move a little at a time
iLQR has no natural place for hard constraints such as an obstacle or a thrust limit. Sequential convex programming (SCP) and its relative sequential quadratic programming (SQP) make the same move as iLQR. The easy problem is now a QP: a quadratic program, with a quadratic cost and linear constraints.
Take a round obstacle. Call its centre c and its radius r. Call the i-th path point p-i, and the unit arrow from c toward the current p-i n-i. Near the current path, the circle becomes a straight line.
n-i transpose, times p-i minus c, is at least r.
Draw the line that touches the circle where the arrow n-i leaves it. Keep p-i on the far side of that line. Every point there is outside the circle, and the rule is linear, so the QP stays convex.
From the device: c = (5, 0.6), r = 1.6, current p-i = (5, −0.2), so n-i = (0, −1). The rule says −(y − 0.6) ≥ 1.6, so y ≤ −1.0. A trust region of 0.5 lets the point reach only y = −0.7, so the next iteration goes on.
Press SCP step. The pink line is the straight-line stand-in for the circle at the middle point; dashed purple circles are trust regions. Then bend the first guess over the top and solve again.
Illustrative toy: 25 points from (0, 0) to (10, 0); the cost is the sum of squared gaps between neighbours. 400 steps of projected gradient solve each QP. Here the straight-line stand-in is always safe, so a big trust region does not break it.
Why the trust region? The affine dynamics and the quadratic cost are true only near the current path. A free QP may jump to where the approximation is wrong, and the new path can be worse or even unsafe. The trust region is a leash, with the same job as iLQR's line search.
SCP is a heuristic. It can miss the best answer, or any safe answer, and it depends on the first guess. Bend the guess over the top and it settles on a route 0.76 m longer. It still powers real systems, from rocket landing to legged robots.
The symptom is a safe but poor path. To measure it, solve from several first guesses and compare the costs. The fix is a better start: the previous plan shifted by one step, a sampling planner such as RRT, or a learned guess.
Chapter 8
Plan with iLQR, then fly the plan through a gust with and without its gains
This is the hero with every knob. Drag sets how nonlinear the world is. Effort sets the trade between reaching the pad and saving thrust. The gust hits at 1.5 s, after the plan is fixed.
Purple dashes are the plan. Grey flies the plan's thrusts blind. Teal flies the plan plus its gains K-t. Set the knobs, then press Fly.
Illustrative toy. The gust adds a sudden southward speed at step 15. The gains are the K-t from iLQR's last backward pass. We count gradient descent iterations until it is within 1% of iLQR's cost, up to 3,000.
Things to try:
The gains help because the planner's last backward pass is an LQR around the plan. Lecture 14 goes one step further with model predictive control: it plans again from the measured state at every step.
Chapter 9
Keep the methods, the terms and numbers, a checklist and the core backward pass in code
Every method here makes one move. Build a local problem you can solve exactly, and solve it. Take the answer as the next guess, with steps small enough to trust.
| Method | Handles | Solves each iteration | Cost in horizon T |
|---|---|---|---|
| LQR | linear dynamics, quadratic cost, no constraints | one backward pass, no iterations | linear |
| iLQR | nonlinear dynamics, no hard constraints | a local LQR from A-t, B-t | linear |
| DDP | nonlinear dynamics, no hard constraints | a local LQR plus second derivatives of f | linear |
| SCP, SQP | nonlinear dynamics and constraints | a convex QP in a trust region | cubic for a generic QP |
| Term | One line | Number to remember |
|---|---|---|
| Trajectory optimization | min of the summed cost such that x-t+1 = f(x-t, u-t). | Throw: 45° at 7.7 m/s for 6 m |
| Defect | d-t = x-t+1 − f(x-t, u-t), zero at the answer. | First guess: 0.67 m |
| Shooting sensitivity | An early push moves the end most. | 0.2 at step 1: 2.8 m; at step 14: 0.2 m |
| LQR gain | K-t = (R + BᵀP-t+1 B)⁻¹ BᵀP-t+1 A; u = −K-t x. | A = 2, P = 1: K = 1 |
| Riccati update | P-t = Q + AᵀP-t+1 A − K-tᵀS-t K-t, from P-T = Q. | P: 1, 3, 4; cost 8 both ways |
| Steady state | Long horizons settle to infinite-horizon LQR. | A = 2, R = 1: P = 4.24, K = 1.62 |
| iLQR forward pass | u-t = ū-t − K-t(x-t − x̄-t) − αk-t on the true model. | Hero: 2 iterations against about 100 |
| Newton step | a-next = a − g / H. | −2.5 to −1.47 in one step |
| Linearized obstacle | n-iᵀ(p-i − c) ≥ r inside a trust region ε. | Wrong first guess: 0.76 m longer |
pythonimport numpy as np def lqr_backward(A, B, Q, R, T): P = Q.copy() # P_T = Q: no future at the last step Ks = [] for t in range(T - 1, 0, -1): S = R + B.T @ P @ B # price of pushing K = np.linalg.solve(S, B.T @ P @ A) # gain: saving over price P = Q + A.T @ P @ A - K.T @ S @ K # Riccati update Ks.append(K) return Ks[::-1], P # gains for t = 1..T-1, and P_1 # A = 2, B = 1, Q = R = 1, T = 3 -> gains 1.5, 1.0 and P_1 = 4
This lesson follows Lecture 13, Optimal Control and Planning, Part 1, of CMU 16-831 Introduction to Robot Learning, Spring 2024, taught by Guanya Shi. The slides are on the lectures page. The worked numbers and toys are ours.
Now press Present or Teach and explain from memory: the problem, shooting against collocation, the backward pass, the iLQR loop and the trust region. Then go back to the race, set the effort high, and say why both planners stop short of the pad.