A boat approaching a quay must arrive in the right place, face along the berth, and lose its remaining momentum. A sequence of thrust commands determines all three outcomes. The shooting formulation turns this task into an optimization over those commands: simulate a candidate sequence, measure its cost, and improve it. How can an optimizer use the time structure of the simulation when computing that improvement?
Linearizing the dynamics along the current trajectory and quadratically approximating its costs gives a problem whose controls can be eliminated backward, one time step at a time. The resulting correction is then tested through the nonlinear simulator. Repeating these operations gives the iterative linear-quadratic regulator, or iLQR. Keeping additional curvature from the dynamics gives differential dynamic programming, or DDP. Both methods compute corrections around the current plan. The local problem is the linear-quadratic regulator solved by dynamic programming in the previous chapter, and its backward recursion can also be read as substitution and quadratic minimization.
The chapter is a bridge between the exact dynamic programming of the previous chapter and the stochastic dynamic programming of the next. iLQR reuses the LQR backward recursion, but only locally: its quadratic models and its feedback gains describe a neighborhood of one trajectory, not the whole state space.
Learning Goals¶
After working through the derivations and examples, you should be able to:
Construct a local quadratic optimal-control problem around a feasible rollout.
Eliminate its controls backward and recover an affine correction at each step.
Implement nonlinear trial rollouts with line search and regularization.
Identify the dynamics-curvature terms that distinguish DDP from iLQR.
Assess a docking plan using its cost, arrival velocity, and hull clearance.
Explain how thermal load and nonlinear power loss shape a bounded cooling schedule.
Relate the shape of a radiotherapy schedule to organ repair and the curvature that DDP retains.
Prerequisites¶
The chapter uses the finite-horizon optimal-control formulation, single shooting, the linear-quadratic regulator from finite-horizon dynamic programming, and first and second derivatives. The SQP section of the nonlinear-programming appendix introduces general SQP subproblems, and the initial-value-problem appendix reviews numerical integration.
A Docking Problem¶
Docking requires the boat to reach the berth, align with the quay, and shed its momentum. If the boat reaches the right position with appreciable translational or angular velocity, its momentum will carry it away again.
Consider a two-metre boat moving in the horizontal plane. Its state and control are
The position and velocity are measured in fixed world coordinates, is the heading, and is the angular velocity. Each control is a fraction of maximum thrust. Positive commands push forward and negative commands push backward. Unequal thrusts turn the boat. There is no directly commanded sideways force, so an approach with sideways momentum requires a maneuver.
Let point along the boat and point across it. The simplified inertial model is
The drag opposes velocity along each boat axis, with stronger resistance to sideways motion. Thrust changes acceleration, so releasing the controls does not stop the boat immediately. These equations are a synthetic teaching model for calm water, with neither contact forces nor waves.
| Quantity | Value |
|---|---|
| Mass | |
| Angular inertia | |
| Thruster lever arm | |
| Maximum force per thruster | |
| Longitudinal drag | |
| Lateral drag | |
| Angular drag | |
| Rectangular hull length and width | , |
The quay occupies , and the desired arrival state is . The boat should lie parallel to the quay with its center one metre from the edge. The angled approach starts from . The sideways-drift case changes only to . Angles are in radians. Both experiments use piecewise-constant controls, each held for , for a total of 20 seconds. Thus is the initial state and is the arrival state, as in the preceding chapters. One fourth-order Runge--Kutta step gives for .
To discourage the hull from approaching the quay, let be the world-coordinate height of rectangular corner . With a desired buffer and smoothing length , define
This is a smooth penalty for violating the buffer. Its finite weight permits violations, so the final trajectories will also undergo a separate geometric clearance check. The full rectangle is used for that check, including the area outside the pointed boat icon in the figures.
For , the running and terminal costs are
These numerical weights apply to the stated SI coordinates and dimensionless thrust commands. The periodic heading penalty assigns the same cost to angles that differ by a full turn. The simulator retains an unwrapped angle; it does not insert a discontinuity at into the differentiated transition.
The initial control sequence is zero, so the boat coasts and slows under drag. It stops far from the berth. Successive control updates can first bring it closer and then adjust the arrival heading and velocity.
Figure 1:Accepted iLQR iterates for the angled approach. Every curve is a nonlinear rollout from the same initial state. Boat outlines are spaced four seconds apart; their overlap indicates slow motion. The dotted outline marks the desired berth. The first update reduces the position error substantially, while later updates refine the turn and stopping maneuver.
Taylor Models of Dynamics and Cost¶
The local model describes the change in cost caused by a small change in the thrust sequence. Start from the finite-horizon problem
Here is the complete control sequence. Its rollout determines every later state. This is an open-loop problem: the decision variable is a sequence of commands fixed in advance, not a rule that reacts to the state. iLQR returns such a sequence. The feedback gains that appear below are a by-product of the computation. Near the returned plan they tell how to adjust each command when the state drifts from the planned one, through , and they say nothing about states far from it.
Figure 2 shows what “near the plan” means. The boat follows the final docking plan and receives a small push after two seconds. Replaying the planned thrusts ignores the push. The boats drift calmly but none reaches the berth: they miss it by 0.9 m on average and by up to 2.2 m. Applying the local feedback steers all twelve pushed runs back onto the plan, and they arrive within a millimetre of it. These runs stay inside a thin tube around the planned trajectory.
The tube has a limited width. With pushes twice as large, shown in the animation below, several feedback runs leave it and two never reach the berth. Their corrections ask for more thrust than the thrusters can give, and the boat moves far enough from the plan that the linear model is no longer accurate. The gains describe a neighborhood of one trajectory, and this whole chapter works inside such neighborhoods.
Figure 2:Twelve random pushes at 2 s (velocity changes up to 0.3 m/s in each direction, with small heading and spin changes) applied to the final iLQR plan for the angled approach. Left: the planned thrusts are replayed unchanged. Right: the thrusts follow the local feedback law, clipped to the thruster limits. The shaded tube, drawn in both panels, is the envelope of the feedback runs. Circles mark the positions at 20 s.
Play or scrub simulation time, and switch to the larger pushes to see boats leave the tube. The counters report how many boats are inside the tube and at the berth at the selected time.
We cannot solve this nonlinear problem directly. The previous chapter showed that a linear-quadratic regulator problem can be solved exactly by a backward recursion. The idea of this chapter is to build an LQR problem that resembles ours near a current plan, solve it, use its solution to improve the plan, and repeat.
Set aside the control bounds while deriving this local problem. The current plan is a control sequence together with the states obtained by rolling it out, . For the boat, the first plan is the zero-thrust coast. The plan is our current guess, not a reference that the boat must track. To ask what happens if we change it, consider nudges and , stacked as . These are hypothetical changes that we are free to choose, not measured errors.
The linear model of the transition follows the usual Taylor steps. A scalar function of one variable satisfies . With two inputs the partial derivatives add: . The transition has one such expansion for each of its components. Each component gives one row, and stacking the rows gives the Jacobian matrices and :
The left side is the transition evaluated at the nudged state and control, which is the new next state . Because the current plan is a rollout, the first term on the right is . Subtracting from both sides turns the left side into and cancels that term on the right. Dropping the quadratic remainder gives the linearized dynamics . Both rollouts start from the same state, so . There is no constant defect because the current plan satisfies the discrete dynamics. Independently chosen state guesses would generally leave such a defect. We write for the combined Jacobian.
The running cost is expanded by the same steps, one order higher. In one variable, . Applied to the stacked vector , the derivative becomes a gradient and the second derivative becomes a Hessian matrix. Splitting back into its state and control parts cuts the gradient into and , and cuts the Hessian into four blocks: for curvature in the state, for curvature in the control, and the mixed blocks and . The quadratic term therefore reads , where the two mixed blocks have combined into one term. In block-matrix form,
Here and ; the other gradient and Hessian blocks are evaluated at the same pair from the current plan. We denote the stacked gradient by and the displayed block Hessian by . Gradients are column vectors, matching the convention of the preceding chapters. We retain the linear terms: individual stage gradients need not vanish even at an optimal trajectory because the dynamics couple stages, and at a non-optimal plan they also drive its correction. The block measures how the marginal cost of a control changes with the state. Even if the running cost has no such cross term, eliminating later controls can create one in the quadratic tail.
The terminal cost has the same expansion, with no control coordinate:
Dropping the remainders in these three Taylor expansions gives the local problem. Up to the constant , which does not depend on the nudges, it reads
This is an LQR problem of the kind solved in the previous chapter: linear dynamics in the nudges, and a cost that is quadratic in them. Compared with the standard LQR problem it has linear cost terms and a state--control cross term, which the next section handles. Solving it gives a proposed change to the plan.
Notice what is missing from this local problem. Only the costs were expanded to second order. The dynamics were expanded to first order, so their curvature appears nowhere in (9). A general SQP method also forms a local quadratic subproblem, but not quite this one. SQP takes a quadratic model of the Lagrangian and linearizes the constraints. Here the constraints are the dynamics, so the Hessian of the Lagrangian is the cost Hessian plus the sum over of the multiplier times the Hessian of the th component of the transition. The shooting formulation is unconstrained only in the controls, because the simulator enforces the dynamics, but the local problem above still has the linearized dynamics as constraints on the nudges. iLQR is therefore SQP with the multiplier-weighted dynamics Hessians dropped. Those terms return in the DDP section, where the coefficient of the eliminated tail plays the role of the multiplier, the costate.
Backward Elimination¶
The local problem (9) is a deterministic finite-horizon problem with linear dynamics and quadratic costs. It is an LQR problem, so the backward induction of the previous chapter solves it. This section recalls that algorithm, specializes it to LQR, and then applies the same specialization to the local problem. Each backup minimizes over one control and removes it from the problem, which is why the backward pass of iLQR is often called backward elimination.
Backward induction¶
For a deterministic problem with dynamics , stage costs and terminal cost , Algorithm 1 computes the optimal cost-to-go backward in time:
The minimizer at each state defines the policy . A forward pass from the initial state then applies and recovers the optimal trajectory. In general is a function on the whole state space, which the previous chapter stored on a grid.
The LQR case: the Riccati recursion¶
With linear dynamics , stage cost and terminal cost , every cost-to-go is quadratic, , so the backup only has to update a matrix. Substituting the dynamics into makes the bracket a quadratic function of the stacked state and control,
Setting its control gradient to zero gives the linear feedback , and substituting it back gives the Riccati recursion of Algorithm 3:
starting from .
Riccati for the local problem¶
The local problem has the same structure, with the nudges as state and control. Its terminal cost-to-go is the terminal Taylor model, and its backup is
Two features differ from textbook LQR. The current plan is generally not optimal, so the local costs have linear terms, the gradients and . The stage cost may also contain a state--control cross term . Because of the linear terms, the cost-to-go needs a linear part as well:
with and the gradient and Hessian of at the end of the plan. Substituting the linear dynamics into makes the bracket a quadratic in with linear coefficient and Hessian . Written in blocks, next to the LQR matrix above,
The Hessian blocks are the LQR blocks with replaced by , by , and the cross term added. The vector is new. In stacked form, with , these are and . We use for this Hessian because denoted the Hamiltonian in the Pontryagin chapter.
Setting the control gradient to zero, , gives an affine law instead of a linear one:
The feedback gain plays the role of the LQR gain, written here with the sign included. The feedforward term comes from the linear terms: it changes the plan even when the state nudge is zero. Substituting the control back gives the coefficients for the preceding step,
The update of is the Riccati recursion. Written out, it reads , which is the LQR formula when . The update of is the new part. It carries the cost gradients backward, and with zero gradients it stays zero and vanishes. In code, these formulas are linear solves with ; an explicit matrix inverse is unnecessary.
The tail is the cost-to-go of the local problem only. It is accurate near the current plan, and it is not a value function of the nonlinear system over its whole state space. Unlike the grid of the previous chapter, it is stored as a vector and a matrix per step, so the backward pass costs a fixed amount of work per step and grows linearly with the horizon. For linear dynamics and quadratic costs it solves the original problem exactly whenever each is positive definite. Along a nonlinear trajectory, , , and the cost derivatives describe only a local approximation. They must be recomputed after the plan changes. Exercise 1 carries out one backward pass by hand on a scalar problem.
Nonlinear Rollouts and iLQR¶
The backward pass returns, at every step, an affine law for the local problem. iLQR turns it into a new plan and repeats:
Roll out. Simulate the current controls through the true dynamics. This gives the current plan , and its cost.
Backward pass. Linearize the dynamics and expand the costs around this plan, then solve the local LQR problem with Algorithm 1. This gives and .
Forward pass. Run the affine law in closed loop through the true simulator, starting from the same initial state:
The new states and controls are the next plan.
Repeat until the plan stops changing.
This loop is the iterative linear-quadratic regulator, iLQR Li & Todorov, 2004: an LQR problem is solved at every iteration, around a different trajectory. The control law in step 3 is the local solution , with the nudge measured as the simulated state minus the planned one. The feedback term matters because the true simulator does not follow the linear model exactly. When the new states drift from the linear prediction, adjusts the controls the way the local model says the best control changes with the state.
The loop always linearizes around a trajectory that the true dynamics produced. The gains from the old linearization generate the new trajectory, and that trajectory gives the next linearization. Everything in steps 2 and 3 follows from the derivation above: , , , , and all come out of the LQR algebra. Two safeguards are added by hand, a step size and a regularization, and the rest of this section explains why each is needed.
Step size and line search¶
The local model is accurate only near the current plan. Far from the optimum, the full step of the forward pass can move the boat well outside that neighborhood, and the true cost can then go up instead of down. Nothing in the Riccati derivation prevents this.
The usual fix comes from numerical optimization. Treat the LQR solution as a search direction, as one would treat a Newton step, and decide separately how far to move along it. This is a damped Newton method. The step size is not part of the Riccati derivation; it is added afterward and multiplies only the feedforward term:
Putting on alone is a choice, and the reason is that it gives a path between two known trajectories. At every state nudge stays zero, by induction through the forward pass, and the old plan comes back. At the forward pass takes the full LQR step, which is the exact solution when the problem is linear-quadratic. In between, the gains keep each trial consistent with the true dynamics.
A line search chooses at every iteration. Try the full step , simulate, and keep the result if the true cost went down enough. Otherwise halve and simulate again. Each trial is one run through the nonlinear simulator. The step size is therefore not a fixed hyperparameter; it adapts at every iteration. When the linear model is accurate, as near convergence, the full step usually passes at once. The real settings are the halving schedule and the meaning of “enough”.
The implementation tries . For “enough”, the backward pass predicts the reduction of the local model. With
the predicted reduction is . A trial is accepted when its true cost decreases strictly and by at least a small fraction of this prediction, . The prediction guides acceptance; it does not replace evaluation of the original objective.
Regularization¶
The second problem appears in the backward pass itself. Away from the optimum, the control curvature may be indefinite or nearly singular, and then the control solve fails or proposes a huge change. Replace the matrix used in the control solve by , where . Increasing makes the correction more conservative and can make the solve positive definite. The code checks a Cholesky factorization, increases after a failed backward pass or line search, and decreases it after an accepted step. The numerical defaults start at 10-4, multiply by ten after failure, and divide by three after acceptance, with a floor of 10-9.
When regularization changes and , the formulas for and above no longer describe substitution into the original quadratic model. The implementation therefore uses the full expressions
Here is the original curvature block; regularization was used to choose the correction. These expressions also remain applicable when some controls are fixed at their bounds.
With both safeguards in place, the complete method reads as follows.
The iteration uses second derivatives of the costs but only first derivatives of the dynamics. It is often described as a Gauss--Newton approximation to DDP. That terminology should not obscure what is approximated: the omitted terms come from dynamics curvature, as the next section shows. If a nonlinear cost is itself a sum of squared residuals, replacing its Hessian by a residual-Jacobian product would be a further approximation; the docking code uses the exact cost Hessian.
The returned gains describe a neighborhood of the final plan. The docking animation below executes one stored plan. The gains used inside the optimizer do not imply that the animation replans as the boat moves.
Dynamics Curvature and DDP¶
iLQR drops the quadratic remainder in the transition’s Taylor expansion. For a turning boat, a change in heading changes the direction of thrust, so products of state and control changes can affect the next state. DDP keeps these second-order terms in the backward pass. Everything else stays the same.
The chain rule in one variable¶
Each backup minimizes a stage cost plus the cost-to-go evaluated at the next state. With one variable, write for the cost-to-go and for the dynamics. The second derivative of the composition has two terms:
The first term is the curvature of the cost-to-go times the squared slope of the dynamics. The second is the slope of the cost-to-go times the curvature of the dynamics. iLQR keeps the first term only. DDP keeps both.
One backup, expanded to second order¶
Write the quantity minimized in one backup as a function of the stacked nudge :
Expand it to second order in . The next-state nudge has a linear part and, for component , a quadratic part , where is the Hessian of the th component of along the current plan. Inserted in the tail, the quadratic part of sees only the linear part; the rest is of order three or higher. The linear part of the tail, however, sees both. The linear coefficient is the same as before, and the quadratic coefficient is
iLQR drops the last term:
These are the multiplier-weighted dynamics Hessians that the local problem left out. The tail gradient plays the role of the SQP multiplier: it is the costate, the sensitivity of the remaining cost to the next state.
DDP keeps the term¶
DDP Tassa et al., 2014 keeps the dynamics-curvature term. In blocks,
Everything downstream is the same recipe with the new : the formulas for , , and , the forward pass, the line search and the regularization. In Algorithm 2, only step 3 changes.
DDP does not build a new local problem with quadratic dynamics and solve it with Riccati. With quadratic dynamics the local problem would no longer be LQR. DDP instead expands each backup to second order directly, and the extra term lands in .
The only line that differs from Algorithm 1 is the formula for . The curvature choice can still change both the direction of a step and the amount of regularization needed to accept it.
For the boat, even the continuous acceleration contains a state--control cross derivative: . The discrete derivatives used by the solver also include how heading and velocity change within the RK4 step. Automatic differentiation is applied through that complete step. A differential equation that is affine in its controls need not yield a numerical transition whose second control derivatives vanish.
The extra curvature costs computation and can be indefinite. DDP therefore still needs regularization and nonlinear acceptance tests. Neither method guarantees the globally best docking maneuver. For affine dynamics the transition Hessians are zero, so the two backward constructions coincide when all other choices agree.
Thruster Limits and Stopping Criteria¶
The boat’s controls must remain in , even when the unconstrained correction asks for more thrust. At zero state deviation, the feedforward step is the solution of the box-constrained quadratic problem
For two controls, each coordinate is either free, fixed at its lower bound, or fixed at its upper bound. The teaching implementation checks the nine combinations, minimizes over each face, and keeps the best feasible candidate. Positive-definite curvature makes this a strictly convex QP. Larger systems can use an active-set solver rather than enumerate all combinations Tassa et al., 2014.
The feedback rows of saturated controls are zero while their active set stays fixed. For the free coordinates , solve . Use the full substitution formulas to update the quadratic tail. With bounds, this tail describes a neighborhood with the selected active sets; across active-set changes the minimized expression is generally piecewise quadratic. During the nonlinear trial, project the resulting control onto its box before simulation. This enforces the physical bounds when a trial leaves the neighborhood represented by the backward pass. The nonlinear cost check then decides whether that trial should be retained.
The stopping test is separate from docking success. Let be the gradient of the original shooting objective, computed by the adjoint recursion along the current nonlinear rollout. With normalized controls, the solver stops when
At an interior optimum this reduces to a small control gradient. At a bound it also allows a gradient that points toward a forbidden improvement. The experiment permits at most 120 accepted updates. If regularization exceeds 1012 without an acceptable trial, the solver retains its last accepted trajectory and reports failure to make progress. A small residual certifies approximate first-order stationarity, not a globally optimal path or a successful arrival.
Docking Trajectories and Recorded Futures¶
Do the two curvature choices produce the same maneuver from the same starting plan? Both methods receive the coasting initialization, identical costs and control limits, and the same stopping settings. The following tables report their final nonlinear costs and arrival checks. Each final control sequence is also replayed with four RK4 substeps per control interval, giving a validation grid.
| Approach | Method | Cost | Accepted updates |
|---|---|---|---|
| Angled approach | iLQR | 8.0515 | 18 |
| Angled approach | DDP | 11.9879 | 34 |
| Sideways drift | iLQR | 7.6674 | 13 |
| Sideways drift | DDP | 7.6674 | 45 |
The arrival checks below use the finer replay.
| Approach | Method | Position error (m) | Heading error (°) | Speed (m/s) | Clearance (m) |
|---|---|---|---|---|---|
| Angled approach | iLQR | 0.000125 | 8.98e-05 | 6.89e-05 | 0.599 |
| Angled approach | DDP | 0.00125 | 0.139 | 0.0144 | 0.352 |
| Sideways drift | iLQR | 3.89e-05 | 2.65e-06 | 4.25e-05 | 0.600 |
| Sideways drift | DDP | 3.89e-05 | 3.71e-06 | 4.25e-05 | 0.600 |
All four runs satisfy the stopping test and the docking checks: position error below , heading error below , speed below , angular speed below , bounded thrust, and positive hull clearance. The maximum position difference between the planning trajectory and the finer replay is below in these runs. Clearance is checked on the finer grid; this remains a sampled validation, not a proof of continuous-time collision avoidance.
Figure 4:Final trajectories under the same initialization and objective. Solid blue is iLQR and dashed orange is DDP; the dotted gray path is the initial coast. Outlines show orientation every four seconds. DDP takes a different turning maneuver in the angled approach. The two final paths nearly coincide in the sideways-drift case.
In the angled approach, iLQR reaches cost 8.0515 in 18 accepted updates, while DDP reaches a different stationary trajectory with cost 11.9879 in 34 updates. The DDP trajectory makes two full turns near the berth. Its final unwrapped heading is approximately , which the periodic heading cost treats as the same orientation as zero. Reaching this stationary trajectory does not remove the extra motion accumulated on the way there. In the sideways-drift case, both reach cost 7.6674, with 13 accepted updates for iLQR and 45 for DDP. These are outcomes of two particular local solves, not a general ranking of the algorithms. The difference in the first case also shows why comparing iteration counts alone can hide a difference in the solutions obtained.
Figure 5:Actual nonlinear cost at every accepted iteration, including the initial rollout at iteration zero. The vertical axis is logarithmic. Rejected trial steps are excluded from the horizontal count; forward-evaluation and backward-attempt counts are available in the downloadable diagnostics.
Within one selected plan, simulation time has a different meaning from optimizer iteration. At two seconds, for example, the boat occupies the two-second state of that rollout. Its remaining states give a prediction of where it will go if the rest of the stored controls are applied. Outlines spaced equally in time spread apart while the boat is moving quickly and cluster as it comes to rest. Reverse thrust helps remove momentum before arrival.
Figure 6:The final iLQR plan for the angled approach. The filled boat marks the selected simulation time; translucent outlines show future poses every two seconds. The right panel gives speed and both physical thrusts. Negative thrust during the approach brakes the boat. The vertical marker identifies the same time as the filled boat.
Choose a scenario and optimizer iteration, then scrub or play simulation time within that fixed plan. The path ahead and boat outlines display its remaining predicted states. Arrival errors and plots belong to the selected iteration, including intermediate plans that do not dock successfully.
The replay reads recorded simulations, so exploring it does not rerun an optimizer. Recomputing a plan from newly observed states is the additional step introduced in receding-horizon control.
Pulling Down a Thermoacoustic Refrigerator¶
A standing-wave thermoacoustic refrigerator is a sealed, gas-filled tube with a loudspeaker at one end and a stack of thin parallel plates inside (Figure 8a). The loudspeaker excites a standing sound wave in the tube. Each gas parcel near a plate is pushed back and forth along it, compressing and heating as it moves toward the loudspeaker end and expanding and cooling as it moves away. At the loudspeaker end of its travel the parcel is warmer than the plate and gives up heat to it; at the far end it is cooler than the plate and takes heat from it. Every cycle therefore moves a little heat one step along the plate, and the parcels together act as a bucket brigade that transports heat from the far end of the stack toward the loudspeaker end. Sound is used to move heat from a cold place to a warmer one, against the direction it would flow on its own. A household heat pump does the same thing with a compressor when it extracts heat from cold outdoor air and delivers it indoors; a refrigerator is the same machine used for its cold side. A cold heat exchanger at the far end of the stack connects to the payload, the refrigeration term for the object or compartment being cooled, and a hot heat exchanger at the loudspeaker end rejects the heat to ambient air. There are no moving parts other than the loudspeaker, and the only knob is how hard it drives. The control problem is what refrigeration engineers call a pull-down: bringing the payload from ambient temperature down to its set point after switch-on. Here that means choosing a driver-amplitude schedule that brings the payload to a target temperature by a fixed deadline while spending little acoustic energy.
Figure 8:Geometry of the thermoacoustic refrigerator and its two-state abstraction. (a) The device: a loudspeaker piston at the left end of a sealed resonator, a hot heat exchanger, a stack of thin plates, and a cold heat exchanger. The dashed envelope is the standing-wave pressure amplitude. Gas parcels oscillating along the plates pump heat from the cold exchanger toward the hot one. (b) The lumped model the optimizer sees. The state is the pair of exchanger temperatures, the action is the driver amplitude, the stage cost charges acoustic work, and the terminal cost penalizes the final cold-side temperature error. In the online book the figure replays the recorded iLQR and DDP plans: the piston stroke, wave, and parcel motion follow the action, the exchanger and node tints follow the state, and the panels below compare the plan with a constant-amplitude schedule. The static version shows the same two panels.
In the online book, the replay reads the recorded plans and integrates only the constant-amplitude comparison; it does not rerun the optimizer.
Rather than simulate the acoustics, we lump the whole device into two thermal masses (Figure 8b): the cold exchanger together with its payload, and the hot exchanger. The state is their temperatures, , and the action is the normalized driver amplitude , so this is a two-dimensional continuous-state, one-dimensional continuous-action problem. Let be the temperature span across the stack and a pumping efficiency that decreases linearly with that span. The continuous-time dynamics are
Here is the heat pumped out of the cold side, is the acoustic work the driver injects, is a parasitic heat leak into the payload, and is the hot exchanger’s conductance to ambient. Both heat flows scale with because acoustic power is quadratic in wave amplitude, and both weaken as the span grows; this is the short-stack approximation of thermoacoustics Swift, 1988Swift, 2017. Two loss terms complete the work: a viscous term in and a cubic term that makes hard driving disproportionately expensive. The coupling matters for the optimizer. Driving hard pumps heat faster, but it also dumps that heat plus the work into the hot side, which raises , widens the span, and lowers the efficiency of every later step. Refrigeration engineers measure this efficiency by the coefficient of performance (COP), the ratio of heat pumped to work spent while the driver is on. Unlike an efficiency it can exceed one, because the work only moves heat rather than producing cold. In this model the COP falls as the span grows and, through the cubic term, as the amplitude grows; one scenario below removes the cubic term so that the COP no longer depends on amplitude. All coefficients are synthetic teaching values, not measurements of a particular device.
| Quantity | Value |
|---|---|
| Cold and hot heat capacities | |
| Hot conductance | |
| Ambient temperature | |
| Parasitic cold-side load | |
| Pumping coefficient | |
| Work and viscous coefficients | |
| Cubic-loss coefficient | |
| Critical temperature span | |
| Target temperature |
Both temperatures begin at ambient, . A fourth-order Runge--Kutta step of length defines the deterministic transition over a horizon of actions, so the episode is exactly five minutes. The cost uses the same stage-plus-terminal form as the docking problem:
The stage cost charges only energy, and the target enters only through the terminal cost, so the objective is sparse in the RL sense: nothing tells the optimizer how cold to be at intermediate times. The terminal penalty is soft, so a plan can trade some arrival error for lower energy. Both methods start from the constant open-loop sequence and use the same action bounds and stopping settings.
| Experiment | Method | Status | Accepted updates | Cost | Energy (J) | Cold at 300 s (°C) |
|---|---|---|---|---|---|---|
| Baseline pull-down | iLQR | converged | 24 | 51.224 | 1016 | 5.20 |
| Baseline pull-down | DDP | converged | 12 | 51.224 | 1016 | 5.20 |
| No parasitic load | iLQR | converged | 28 | 20.652 | 411 | 5.09 |
| No parasitic load | DDP | converged | 8 | 20.652 | 411 | 5.09 |
| Sluggish hot side | iLQR | converged | 28 | 45.716 | 911 | 5.13 |
| Sluggish hot side | DDP | converged | 14 | 45.585 | 908 | 5.13 |
| Amplitude-independent COP | iLQR | iteration_limit | 200 | 33.353 | 666 | 5.08 |
| Amplitude-independent COP | DDP | converged | 27 | 33.353 | 666 | 5.08 |
| Target out of reach in 300 s | iLQR | converged | 2 | 119.582 | 1426 | 2.20 |
| Target out of reach in 300 s | DDP | converged | 10 | 119.582 | 1426 | 2.20 |
Costs are evaluated on the final accepted nonlinear trajectory. The iteration-limit row is a retained local plan, not a converged solve.
For comparison, constant full amplitude in the baseline case ends at 2.20 °C, uses 1426 J, and has cost 149.89.
Figure 9:Cold and hot temperatures under the baseline plan, with a constant full-amplitude rollout for comparison. The lower panel shows their driver amplitudes. The target line and temperature traces show the tradeoff between energy use and final cold temperature.
The baseline plan drives relatively hard at first, eases off, then ramps to full amplitude near the deadline. Early driving uses the initially small temperature span, but it also warms the hot side and weakens later pumping. The fixed load adds the same total heat over every 300-second plan. Control timing still changes the temperature span and therefore the final cold temperature. Running at full amplitude throughout uses about and reaches ; its nonlinear cost is 149.886, versus 51.224 for the baseline local plan. The soft terminal cost need not favor the coldest possible final state.
Figure 10:Final computed amplitude schedules for the baseline, no-load, sluggish-hot-side, and no-cubic-loss cases. The first three are iLQR plans; the no-cubic-loss plan is from DDP. Removing the cold-side load flattens the schedule, while changing the hot-side dynamics produces a long ramp.
Without the parasitic load, a nearly constant amplitude is economical. To see the tendency, temporarily hold the temperature span fixed and write . Heat pumped is then proportional to , while the cubic loss is proportional to , a convex function for . For a fixed total amount of pumping, Jensen’s inequality favors spreading through time. The actual schedule is not exactly constant because the span and hot-side temperature still respond to the controls. With and , the amplitude rises through most of the horizon, then turns off for the final two intervals. Both thermal storage and heat rejection changed in that experiment, so it does not isolate the effect of either parameter.
The DDP curvature term also has a direct physical interpretation here. For the continuous cold-side equation,
Increasing the temperature span weakens pumping, and this mixed derivative measures how that effect changes with amplitude. The solver differentiates the complete RK4 transition, whose Hessian also contains effects from the intermediate integration stages.
Figure 11:Actual nonlinear cost after each accepted update for iLQR and DDP in the baseline and no-cubic-loss cases. The vertical axis is logarithmic. The curves compare these local solves from the same initial control sequence, not a general ranking of the methods.
When , both pumping and work are quadratic in amplitude at a fixed temperature span. Their instantaneous ratio then no longer depends on amplitude, leaving weaker preferences among some schedules. In the recorded run, iLQR reaches its iteration limit while DDP satisfies the stopping test; both reach nearly the same final cost. For the sluggish hot side, DDP reaches a different, slightly lower-cost local plan than iLQR. Lowering the target to produces a plan saturated at throughout the fixed horizon, exercising the box-constrained backward step. These outcomes depend on the initial sequence. Starting the baseline from constant amplitudes 0.1 and 0.9 leads to other local plans, so neither result certifies a global minimum.
Scheduling Radiotherapy Fractions¶
Radiotherapy rarely delivers its dose in one sitting. A course is split into fractions, typically one per weekday over several weeks, because healthy tissue repairs part of its radiation damage between sessions while many tumors repair less. Every beam that reaches the tumor also crosses healthy organs on its way in and out, so a treatment plan must decide both how hard to irradiate on each day and from which directions. With one control vector per session and a state that summarizes the damage accumulated so far, this is again a finite-horizon optimal-control problem, and iLQR applies to it directly.
The example below is a synthetic slice of the chest with a tumor at the center and three organs around it: the spinal cord, the heart, and a lung. Three beams aim at the tumor, and each one crosses exactly one organ before reaching it (Figure 12). The control holds the three beam intensities of session , each between 0 and . The beams are scaled so that intensity adds gray to the tumor’s mean dose. The mean dose received by structure is , where the dose matrix with rows is computed once from the beam geometry. The tumor row is , and each organ row has a single large entry, between 1.28 and 1.50, for the beam that crosses it. That beam enters through the organ before it has lost intensity in the tissue, so the organ receives more than the tumor does.
Figure 12:The fractionation problem and its optimized plan. Left: the synthetic slice, with the course dose of the final DDP plan. Arrows mark where the three beams enter. Top right: the dose that each session delivers to the tumor, split by beam, against the uniform 3 Gy plan (dashed). Bottom right: health of the tumor and the three organs under the optimized plan (solid) and the uniform plan (dotted). In the online book the figure replays the recorded plans session by session and lets the reader scrub through the accepted iLQR and DDP iterates.
The state is a health score for each structure, following the convention of the clinical planning work this example is adapted from Fu et al., 2019. The tumor starts at and is controlled when its health reaches 0. Each organ starts at 0, and its health becomes negative as damage accumulates; -1 is its tolerance. A session damages structure according to the linear-quadratic model of radiobiology Fowler, 1989, and the days until the next session then let the organs repair and the tumor regrow:
Here is the number of days until the next session: one from Monday to Thursday and three on Friday. The scale is the accumulated effect that moves a structure by one unit of health, from untreated to controlled for the tumor and from intact to tolerance for an organ. The quadratic term makes one large fraction more damaging than two half-size fractions with the same total dose. Late-responding healthy tissue has a larger relative to than most tumors, and that difference makes splitting the course worthwhile. The coefficients below are synthetic teaching values with plausible ratios, not clinical parameters.
| Structure | (1/Gy) | (1/Gy²) | Effect scale | Daily repair or regrowth |
|---|---|---|---|---|
| Tumor | 0.30 | 0.030 | 22 | regrowth 0.005 |
| Spinal cord | 0.10 | 0.050 | 2 | 0.01 |
| Heart | 0.10 | 0.030 | 2 | 0.04 |
| Lung | 0.10 | 0.020 | 2 | 0.10 |
The cost charges a small amount for beam intensity, penalizes organ health below tolerance at every session, requires the tumor to reach zero at the end of the course, and rewards organ health left at the end:
where is a smooth version of , as in the docking costs. The course has sessions over four weeks. Both solvers start from equal beams of , a uniform plan of per session that leaves the tumor uncontrolled, and use the same box-constrained backward pass as the docking problem.
| Plan | Accepted updates | Cost | Tumor dose (Gy) | Final tumor health | Lowest cord, heart, lung health |
|---|---|---|---|---|---|
| Uniform, 3 Gy per session | – | 436.079 | 60.0 | -0.000 | -2.26, -1.05, -0.54 |
| iLQR (converged) | 53 | 2.932 | 56.2 | -0.001 | -0.72, -0.76, -0.79 |
| DDP (converged) | 11 | 2.932 | 56.2 | -0.001 | -0.72, -0.76, -0.79 |
Health -1 is the tolerance of each organ. Both solvers start from equal beams delivering 2.1 Gy per session, a plan that leaves the tumor uncontrolled.
The simplest curative alternative gives every session equal beams, raised until the tumor just reaches zero. That takes per session and in total, and it drives the spinal cord to a health of -2.26, more than twice its tolerance; the heart also ends just past its limit. The optimized plan controls the tumor with and keeps every organ within tolerance. Three features of the schedule in Figure 12 follow from the model. The beam through the lung carries more than half of the dose, and the beam through the cord the least, because the lung repairs fastest and has the smallest while the cord repairs slowest and has the largest. The doses are front-loaded: early damage to an organ has more days to be repaired before the end of the course, and early tumor kill leaves less tumor to regrow. Fridays receive extra dose because three days of repair follow them. The organ health curves level off after the first week, where each session’s new damage is roughly balanced by repair. Clinical courses are not front-loaded this way; the pattern reflects the first-order repair model and the absence of acute toxicity limits, which a clinical planning model would include.
Figure 13:Left: actual nonlinear cost after each accepted update for iLQR and DDP from the same uniform 2.1 Gy plan, on a logarithmic axis. Right: the dose that selected DDP iterates deliver to the tumor in each session. The first update pushes dose to the last sessions, where it is most directly tied to the terminal tumor cost; later updates move it to the start of the course and to the Fridays.
Both methods reach the same plan, but DDP needs 11 accepted updates and iLQR 53. The transition explains the difference. It is linear in the health state and quadratic in the beams, so the only second-order transition term that DDP adds to the backward recursion is
where is the derivative of the cost-to-go with respect to . Along the final plan this derivative is positive for the tumor, whose remaining health costs, and negative for each organ, whose health is rewarded. The organ terms therefore add positive curvature: they tell the backward pass that pushing a beam harder damages its organ more than in proportion. The tumor term subtracts curvature, because a larger fraction also kills the tumor more than in proportion. iLQR drops both terms and treats the effect of each extra gray as fixed around the current plan, so it needs more updates to discover the advantage of spreading dose over the sessions and beams.
Exercises¶
Solution to Exercise 1
The last control is , and its minimized tail is . With , minimizing the remaining expression gives , followed by the same value for . Thus . For this gives and , so . The final position falls short because the objective trades terminal error against effort; arrival is penalized, not imposed as an equality.
The function is a feedback law: if the state reaching the last step changes, the minimizing last control changes with it. In the vector case this dependence becomes the gain . As the terminal weight grows, the two controls tend to and the terminal state tends to .
Solution to Exercise 2
The local expansions are exact. Backward elimination therefore minimizes the original quadratic problem in perturbation coordinates, and recovers its solution. The affine simulator agrees exactly with the linearized perturbation dynamics. All transition Hessians vanish, so DDP produces the same correction. This is also a useful implementation test: compare the returned controls with a dense solve of the reduced quadratic objective.
Solution to Exercise 3
The Euler velocity update contains . The mixed derivative is therefore . DDP multiplies it by the component of and adds it to the corresponding state--control curvature entry. iLQR uses the first-order transition, whose second derivatives are zero within the local model. The actual experiment differentiates RK4 instead, so its mixed derivative includes the intermediate integration stages.
Solution to Exercise 4
Use make_problem(velocity_weight=0.) and keep every other choice fixed for
the first comparison. For the shorter experiment, set steps=80 in
BoatParameters and regenerate the initial controls. Removing the terminal
velocity term removes an explicit incentive to stop at the final time.
Running position and velocity costs can still lead to a slow arrival,
especially over the longer horizon, so failure is not inevitable. Measure
the final speed independently of position error and retain the same docking
tolerances for both objectives. A low-cost pass through the target is not
a successful stationary arrival.
Solution to Exercise 5
Fixing one coordinate at a bound changes the stationarity equation for the other coordinate through the off-diagonal entry of . Componentwise clipping does not solve that new equation. The face minimization does. If no nonlinear trial is accepted, the solver retains its previous controls and states and reports that it could not make progress; it must not replace them with an unaccepted trial or report convergence from rejection alone.
Solution to Exercise 6
With no load, cooling obtained early is not directly undone by a constant heat leak. If the temperature span were fixed, a given amount of cooling would fix the sum of . The cubic loss is proportional to , so Jensen’s inequality favors equal across time. The temperature span still evolves, which explains the modest variation in the computed schedule. With the load restored, the longer horizon admits lower amplitude over more intervals, but the leak adds heat throughout that extra time. In this model, the local solution starts near 0.59, reaches 0.89 by , and uses full amplitude in the final intervals. Its approximately of energy exceeds the shorter baseline’s use.
Computational Sources¶
The implementation separates the generic backward recursion from the physical models and artifact builders:
Run uv run python scripts/build_boat_docking_artifacts.py from the repository
root to reproduce the solves, figures, results table, and browser data. Normal
book builds read those artifacts. Tests independently check the derivatives,
the composed DDP curvature, the scalar calculation, and agreement between
backward elimination and a dense quadratic solve.
Run uv run python scripts/build_thermoacoustic_pulldown_artifacts.py to
regenerate the refrigerator results table, figures, diagnostics, and the
browser replay data. Run
uv run python scripts/build_radiotherapy_fractionation_artifacts.py for the
radiotherapy solves, figures, table, and replay data. That example is adapted
from the ddp-fractionation
repository, which applies box-constrained iLQR to a clinical phantom with a
thousand beamlets.
Summary and Outlook¶
A feasible nonlinear rollout supplies the point around which dynamics and costs are approximated. Backward elimination of a local quadratic problem then supplies both a feedforward change and a state-dependent correction. iLQR repeats that calculation with linearized dynamics; DDP includes the second-order chain-rule terms from the transition. Nonlinear rollouts, regularization, and acceptance tests connect these local calculations to a decreasing sequence of actual trajectory costs. The refrigerator applies the same calculation to a bounded driver amplitude, with energy use and terminal temperature competing in the objective. The radiotherapy course applies it to three bounded beams over twenty sessions, where organ repair between sessions shapes the schedule and the quadratic dose response gives DDP curvature that iLQR ignores.
The quadratic tail produced by elimination is a local model of the cost-to-go along one trajectory. For linear-quadratic problems it is the optimal cost-to-go itself. When the model prescribes a distribution over successors instead of one next state, stochastic dynamic programming replaces the continuation cost by a conditional expectation.
- Li, W., & Todorov, E. (2004). Iterative Linear Quadratic Regulator Design for Nonlinear Biological Movement Systems. Proceedings of the First International Conference on Informatics in Control, Automation and Robotics, 1, 222–229. https://roboti.us/lab/papers/LiICINCO04.pdf
- Tassa, Y., Mansard, N., & Todorov, E. (2014). Control-Limited Differential Dynamic Programming. IEEE International Conference on Robotics and Automation, 1168–1175. 10.1109/ICRA.2014.6907001
- Swift, G. W. (1988). Thermoacoustic engines. The Journal of the Acoustical Society of America, 84(4), 1145–1180. 10.1121/1.396617
- Swift, G. W. (2017). Thermoacoustics: A Unifying Perspective for Some Engines and Refrigerators (2nd ed.). Springer Cham. 10.1007/978-3-319-66933-5
- Fu, A., Ungun, B., Xing, L., & Boyd, S. (2019). A convex optimization approach to radiation treatment planning with dose constraints. Optimization and Engineering, 20(1), 277–300.
- Fowler, J. F. (1989). The linear-quadratic formula and progress in fractionated radiotherapy. The British Journal of Radiology, 62(740), 679–694.