Chapter 8: Linear Quadratic Regulators
While solving the dynamic programming problem for continuous systems is very hard in general, there are a few very important special cases where the solutions are very accessible. Most of these involve variants on the case of linear dynamics and convex (e.g. positive quadratic) cost. The simplest case, called the linear quadratic regulator (LQR), is formulated as stabilizing a time-invariant linear system to the origin.
The linear quadratic regulator is likely the most important and influential result in optimal control theory to date. In this chapter we will derive the basic algorithm and a variety of useful extensions.
Basic Derivation
Consider a linear time-invariant system in state-space form,
There is one important step here -- it is well known that for this problem the optimal cost-to-go function is quadratic. This is easy to verify. Let us choose the form:
Since we have guaranteed, by construction, that the terms inside the are quadratic and convex (because ), we can take the minimum explicitly by finding the solution where the gradient of those terms vanishes:
Inserting this back into the HJB and simplifying yields
(K,S) = LinearQuadraticRegulator(A,B,Q,R).
If the appearance of the quadratic form of the cost-to-go seemed mysterious, consider that the solution to the linear system $\dot\bx = (\bA - \bB\bK)\bx$ takes the form , and try inserting this back into the integral cost function. You'll see that the cost takes the form .
It is worth examining the form of the optimal policy more closely. Since the value function represents cost-to-go, the optimal actions will move down this landscape as quickly as possible. Indeed, is in the direction of steepest descent of the value function. However, not all directions are possible to achieve in state-space. represents precisely the projection of the steepest descent onto the action space, and is the steepest descent achievable with the control inputs . Finally, the pre-scaling by the matrix biases the direction of descent to account for relative cost weightings that we have placed on the different control inputs. Note that although this interpretation is straight-forward, the slope that we are descending (in the value function, ) is a function that takes into account the long-term dynamics and cost.
Example: LQR for the Double Integrator
Now we can use LQR to reproduce our HJB example from the previous chapter:
python
import numpy as np from pydrake.all import LinearQuadraticRegulator
Define the double integrator's state space matrices.
A = np.array([[0, 1], [0, 0]]) B = np.array([[0], [1]]) Q = np.eye(2) R = np.eye(1)
(K, S) = LinearQuadraticRegulator(A, B, Q, R) print("K = " + str(K)) print("S = " + str(S))
>
> As in the hand-derived example, our numerical solution returns
### Local stabilization of nonlinear systems
LQR is extremely relevant to us even though our primary interest is in
nonlinear dynamics, because it can provide a local approximation of the
optimal control solution for the nonlinear system. Given the nonlinear
system , and a stabilizable operating point,
, with We can define a relative
coordinate system
and observe that which we can
approximate with a first-order Taylor expansion to
Similarly, we can define a quadratic cost function in the error
coordinates, or take a (positive-definite) second-order approximation of a
nonlinear cost function about the operating point (linear and constant
terms in the cost function can be easily incorporated into the derivation
by parameterizing a full quadratic form for , as seen in the Linear
Quadratic Tracking derivation below).
The resulting controller takes the form
or For convenience,
[Drake](http://drake.mit.edu) allows you to call `controller =
LinearQuadraticRegulator(system, context, Q, R)` on most dynamical
systems (including block diagrams built up of many subsystems); it will
perform the linearization for you.
> **Example: LQR for Acrobots, Cart-Poles,
> and Quadrotors**
>
> LQR provides a very satisfying solution to the canonical "balancing"
> problem that we've [already
> described for a number of model systems](/u/5267695e83a1e138). Here is the notebook with those examples, again:
>
> I find it very compelling that the same derivation (and effectively
> identical code) can stabilize such a diversity of systems!
## Finite-horizon formulations
Recall that the cost-to-go for finite-horizon problems is time-dependent,
and therefore the HJB sufficiency condition requires an additional term for
.
### Finite-horizon LQR
Consider systems governed by an LTI state-space equation of the form
and a finite-horizon cost function,
with
\begin{gather*} h(\bx) = \bx^T {\bf Q}_f \bx,\quad {\bf Q}_f = {\bf Q}_f^T
\succeq {\bf 0} \\ \ell(\bx,\bu) = \bx^T {\bf Q} \bx + \bu^T {\bf R}
\bu, \quad {\bf Q} = {\bf Q}^T \succeq 0, {\bf R}={\bf R}^T \succ 0
\end{gather*}
Writing the HJB, we have Due to the positive definite quadratic form on
, we can find the minimum by setting the gradient to zero:
\begin{gather*}
\pd{}{\bu} = 2 \bu^T {\bf R} + \pd{J^*}{\bx} \bB = 0 \\
\bu^* = \pi^*(\bx,t) = - \frac{1}{2}{\bf R}^{-1} \bB^T \pd{J^*}{\bx}^T
\end{gather*}
In order to proceed, we need to investigate a particular form for the
cost-to-go function, . Let's try a solution of the form:
In this case we have and therefore
\begin{gather*} \bu^* = \pi^*(\bx,t) = - {\bf R}^{-1} \bB^T {\bf S}(t) \bx
\\ 0 = \bx^T \left[ {\bf Q} - {\bf S}(t) \bB {\bf R}^{-1} \bB^T {\bf S}(t)
+ {\bf S}(t) {\bf A} + {\bf A}^T {\bf S}(t) + \dot{\bf S}(t) \right]
\bx.\end{gather*}
Therefore, must satisfy the condition (known as the
continuous-time *differential Riccati equation*): and the terminal condition Since we were able to satisfy the HJB with the minimizing
policy, we have met the sufficiency condition, and have found the optimal
policy and optimal cost-to-go function.
Note that the infinite-horizon LQR solution described in the prequel is
exactly the steady-state solution of this equation, defined by $\dot{\bf
S}(t) = 0$. Indeed, for stabilizable systems this equation is stable
(backwards in time), and as expected the finite-horizon solution converges
on the infinite-horizon solution as the horizon time limits to infinity.
The solution will be the result of numerical integration.
On weakly stabilizable systems (like the [perching airplane](/u/0882802d2ea79041#perching)), I've found that even
error-controlled integration can lead to crippling numerical artifacts
such as the loss of symmetry or positive definiteness. For a more robust
numerical solution, we can instead integrate a factorization of
that ensures these properties are maintained. Taking ${\bf S}(t) = {\bf
P}(t){\bf P}^T(t)$, and again using the fact that $\bx^T{\bf P}\dot{\bf
P}^T\bx = \bx^T\dot{\bf P}{\bf P}^T \bx$, we can write the "square-root
form" of the Riccati differential equation: This form does require that is
invertible, which in turn requires that
### Time-varying LQR
The derivation above holds even if the dynamics are given by
Similarly, the cost
functions and can also be time-varying. This is
quite surprising, as the class of time-varying linear systems is a quite
general class of systems. It requires essentially no assumptions on how
the time-dependence enters, except perhaps that if or is
discontinuous in time then one would have to use the proper techniques to
accurately integrate the differential equation.
### Local trajectory stabilization for nonlinear systems
One of the most powerful applications of time-varying LQR involves
linearizing around a nominal trajectory of a nonlinear system and using
LQR to provide a trajectory controller. This will tie in very nicely
with the algorithms we develop in the [chapter on
trajectory optimization](/u/0882802d2ea79041).
Let us assume that we have a nominal trajectory,
defined for . Similar to the time-invariant analysis,
we begin by defining a local coordinate system relative to the
trajectory: Now we have which we can again approximate with a
first-order Taylor expansion to This
is very similar to using LQR to stabilize a fixed-point, but with some
important differences. First, the linearization is time-varying.
Second, our linearization is valid for any state along a feasible
trajectory (not just fixed-points), because the coordinate system is
moving along with the trajectory.
Similarly, we can define a quadratic cost function in the error
coordinates, or take a (positive-definite) second-order approximation of a
nonlinear cost function along the trajectory (linear and constant
terms in the cost function can be easily incorporated into the derivation
by parameterizing a full quadratic form for , as seen in the Linear
Quadratic Tracking derivation below).
The resulting controller takes the form $\bar\bu^* = -{\bf
K}(t)\bar\bx$ or
[Drake](http://drake.mit.edu) provides a [`FiniteHorizonLinearQuadraticRegulator`](https://drake.mit.edu/doxygen_cxx/group__control.html#ga58307d2135757a498c434e96d7b99853)
method; if you pass it a nonlinear systems it will perform the
linearization in the proper coordinates for you using automatic
differentiation.
Remember that stability is a statement about what happens as time goes
to infinity. In order to talk about stabilizing a trajectory, the
trajectory must be defined for all . This can be
accomplished by considering a finite-time trajectory which terminates at
a stabilizable fixed-point at a time , and remains at the
fixed point for . In this case, the finite-horizon Riccati
equation is initialized with the infinite-horizon LQR solution: $S(t_2) =
S_\infty$, and solved backwards in time from to for the
remainder of the trajectory. And *now* we can say that we have
*stabilized* the trajectory!
### Linear Quadratic Optimal Tracking
For completeness, we consider a slightly more general form of the
linear quadratic regulator. The standard LQR derivation attempts to drive
the system to zero. Consider now the problem:
\begin{gather*} \dot{\bx} = {\bf A}\bx + \bB\bu \\ h(\bx) = (\bx -
\bx_d(t_f))^T {\bf Q}_f (\bx - \bx_d(t_f)), \quad {\bf Q}_f = {\bf Q}_f^T
\succeq 0 \\ \ell(\bx,\bu,t) = (\bx - \bx_d(t))^T {\bf Q} (\bx - \bx_d(t))
+ (\bu - \bu_d(t))^T {\bf R} (\bu - \bu_d(t)),\\ {\bf Q} = {\bf Q}^T
\succeq 0, {\bf R}={\bf R}^T \succ 0 \end{gather*}
Now, guess a solution
\begin{gather*} J^*(\bx,t) = \bx^T {\bf S}_{xx}(t) \bx + 2\bx^T {\bf
s}_x(t) + s_0(t), \quad {\bf S}_{xx}(t) = {\bf S}_{xx}^T(t)\succ {\bf 0}.
\end{gather*}
In this case, we have Using the HJB, we have
\begin{gather*} \pd{}{\bu} = 2 (\bu - \bu_d(t))^T{\bf R} + (2\bx^T{\bf
S}_{xx}(t) + 2{\bf s}_x^T(t))\bB = 0,\\ \bu^*(t) = \bu_d(t) - {\bf
R}^{-1} \bB^T\left[{\bf S}_{xx}(t)\bx + {\bf s}_x(t)\right]
\end{gather*}
The HJB can be satisfied by integrating backwards
\begin{align*}
-\dot{\bf S}_{xx}(t) =& {\bf Q} - {\bf S}_{xx}(t) \bB {\bf R}^{-1} {\bf
B}^T {\bf S}_{xx}(t) + {\bf S}_{xx}(t) {\bf A} + {\bf A}^T {\bf S}_{xx}(t)\\
-\dot{\bf s}_x(t) =& - {\bf Q} \bx_d(t) + \left[{\bf A}^T - {\bf S}_{xx}
\bB {\bf R}^{-1} \bB^T \right]{\bf s}_x(t) + {\bf
S}_{xx}(t) \bB \bu_d(t)\\
-\dot{s}_0(t) =& \bx_d(t)^T {\bf Q} \bx_d(t) - {\bf
s}_x^T(t) \bB {\bf R}^{-1} \bB^T {\bf s}_x(t) + 2{\bf
s}_x(t)^T \bB \bu_d(t),
\end{align*}
from the final conditions
\begin{align*}
{\bf S}_{xx}(t_f) =& {\bf Q}_f\\
{\bf s}_{x}(t_f) =& -{\bf Q}_f \bx_d(t_f) \\
s_0(t_f) =& \bx_d^T(t_f) {\bf Q}_f \bx_d(t_f).
\end{align*}
Notice that the solution for is the same as the simpler LQR
derivation, and is symmetric (as we assumed). Note also that has
no effect on control (even indirectly), and so can often be ignored.
A quick observation about the quadratic form, which might be helpful
in debugging. We know that must be uniformly positive. This
is true iff and $s_0 > {\bf s}_x^T {\bf
S}_{xx}^{-1} {\bf s}_x$, which comes from evaluating the function at
defined by $\left[ \pd{J^*(\bx,t)}{\bx}
\right]_{\bx=\bx_{min}(t)} = 0$.
### Linear Final Boundary Value Problems
The finite-horizon LQR formulation can be used to impose a strict
final boundary value condition by setting an infinite .
However, integrating the Riccati equation backwards from an infinite
initial condition isn't very practical. To get around this, let us
consider solving for . Using the matrix
relation $\frac{d {\bf S}^{-1}}{dt} = - {\bf S}^{-1} \frac{d {\bf S}}{dt}
{\bf S}^{-1}$, we have: with the final
conditions This Riccati equation can be integrated
backwards in time for a solution.
It is very interesting, and powerful, to note that, if one chooses
, therefore imposing no position cost on the trajectory
before time , then this inverse Riccati equation becomes a linear ODE
which can be solved explicitly.
These relationships are used in the derivation of the controllability
Grammian, but here we use them to design a feedback controller.
## Variations and extensions
### Discrete-time Riccati Equations
Essentially all of the results above have a natural correlate for
discrete-time systems. What's more, the discrete time versions tend to be
simpler to think about in the model-predictive control (MPC) setting that
we'll be discussing below and in the next chapters.
Consider the discrete time dynamics: and we wish to minimize The cost-to-go is given by If we once again take then we have yielding which is the famous *Riccati difference equation*. The
infinite-horizon LQR solution is given by the (positive-definite)
fixed-point of this equation: Like in the continuous time case, this
equation is so important that we have special numerical recipes for
solving this discrete-time algebraic Riccati equation (DARE). [Drake](http://drake.mit.edu) delegates to these numerical methods automatically when you
evaluate the `LinearQuadraticRegulator` method on a system that
has only discrete state and a single periodic time step.
> **Example: Discrete-time vs Continuous-time LQR**
>
> You can explore the relationship between the discrete-time and continuous-time formulations in this notebook:
In reinforcement learning, it is popular to consider the
infinite-horizon "discounted" cost: $\min \sum_{n=0}^\infty \gamma^n
(\bx^T[n]{\bf Q}\bx[n] + \bu^T[n]{\bf R}\bu[n]).$ The optimal controller
is and the corresponding Riccati equation is
whose solution can be found using
`DiscreteAlgebraicRiccatiEquation`$(\sqrt{\gamma} {\bf A},
{\bf B}, {\bf Q}, \frac{1}{\gamma} {\bf R}).$
> **Example: LQR via Fitted Value Iteration**
>
> In the dynamic programming chapter, we developed general purpose
> tools for [approximating value
> functions with function approximators](/u/fdba470e9277f024#function-approximation). I think it is particularly
> illustrative to see how these work out for LQR. Let's consider the
> discrete-time, infinite-horizon, discounted case.
>
> For LQR, we know that the optimal value function will take a
> quadratic form, Although it is quadratic in ,
> this form is linear in the parameters, . We can
> leverage some of the special tools for fitted value iteration with a
> *linear function approximator*.
>
> I've included two versions of the value iteration update in the
> notebook -- one that samples over both and , and one that
> samples only over and uses the LQR policy (given the current
> estimated cost-to-go) to determine . The difference is not
> subtle when . Take a look!
>
> We must remember that there are multiple solutions to the Riccati
> equation -- the solution to the LQR problem is the (unique) positive
> definite solution. But there are non-positive definite solutions, too,
> which lead to unstable controllers -- these solutions do achieve zero
> Bellman residual. Every solution to the Riccati equation is a fixed
> point of the (fitted) value iteration update, but only the
> positive-definite solution is a stable fixed point of the
> algorithm.
>
> To see this, write $\hat{J}(\bx) = \bx^{T} ({\bf S}^* + {\bf
\Delta})\bx$, where is any solution to the Riccati
> equation, and is a small deviation matrix. If we write
> the error dynamics through the fitted value iteration update, we can
> see that where is the optimal controller given ${\bf
S}_i = {\bf S}^* + {\bf \Delta}_i \approx \bK^*.$ This error converges
> to zero if and only if is stable, which is only if
> .
>
> Go ahead and try many different initial in the code to
> verify it for yourself.
### LQR with input and state constraints
A natural extension for linear optimal control is the consideration of
strict constraints on the inputs or state trajectory. Most common are
linear inequality constraints, such as or
(any linear constraints of the form ${\bf Cx} +
{\bf Du} \le {\bf e}$ can be solved with the same tools). Much is known
about the solution to this problem in the discrete-time case, but it's
computation is signficantly harder than the unconstrained case. Almost
always, we give up on solving for the best control policy in closed form,
and instead solve for the optimal control trajectory from a
particular initial condition over some finite horizon.
Fortunately, this problem is a convex optimization and we can often solve
it quickly and reliably enough to solve it at every time step, effectively
turning a motion planning algorithm into a feedback controller; this idea
is famously known as model-predictive control (MPC). We will provide the
details in the [trajectory
optimization chapter](/u/0882802d2ea79041).
### LQR on a manifold
One important case that does have closed-form solutions is LQR with
linear *equality* constraints. This is particularly relevant in the
case of stabilizing robots with kinematic constraints such as a closed
kinematic chain, which appears in four-bar linkages or even for the
linearization of a biped robot with two feet on the ground.
Our formulation is as follows: in addition to the linear dynamics (in
continuous time or discrete time), we have a linear equality constraint
of the form with
Form a matrix that forms an orthonormal
basis for the nullspace of such that Then for any
we can write form some $\by
\in \Re^d$ and However, as the result of the
constraints we know that Instead of stabilizing
via LQR, we will stabilize , using the projected cost and
dynamics given by where
and are the matrices from the full coordinates
model. I've chosen to write it this way because it is the same in both
discrete and continuous time. The resulting cost-to-go is given by ${\bf
S}_\bx = {\bf P}^T{\bf S}_y{\bf P}$ and the controller is given by
and the where are the
solutions to the reduced LQR problem in
More generally, for a nonlinear system with nonlinear equality
constraints, we would linearize both the system dynamics and the
constraint, and then apply the derivation above. [[1](#Posa15), IIIb]
provides the time-varying LQR version, which follows naturally.
> **Example: Balancing a Segway**
>
> A nice example of this is the task of balancing a [Segway](https://en.wikipedia.org/wiki/Segway) in the
> plane. In 3D, one could either consider the nonholonomic wheels of the
> Segway, or turn it into a [Ballbot](https://en.wikipedia.org/wiki/Ballbot). For the
> purposes of simulation, one can simply construct the ground, the ball,
> and the bot with appropriate collision geometry, and drop it into
> `MultibodyPlant`. The frictional contact between the ball
> and the ground generates the expected dynamics.
>
> Designing the LQR controller is another story. We will use a
> `PlanarJoint` for the floating base, giving us 4 degrees of
> freedom: If we linearize
> this model and call LQR, the LQR call will fail because *the system
> is not stabilizable in the full coordinates*. The first reason is
> easy to see: there is nothing that the actuators can do to change ,
> the vertical position of the ball. Perhaps the more subtle reason is
> that there is a (holonomic) constraint from the ball rolling without
> slip on the ground: where is
> the radius of the ball.
>
> I will use one small implementation "trick". In my code, rather than
> deal with finding the resting penetration of the ball, I decided to
> remove the vertical degree of freedom completely, and used a prismatic
> joint for and a revolute joint for instead of the
> `PlanarJoint` for the floating base. I configured it so that
> the ball was sufficiently in penetration that the normal forces from
> the ground were substantial enough that the friction cone could support
> the horizontal frictional forces I needed to roll. This effectively
> implements the constraint by construction (completely removing that
> degree of freedom).
>
> To use LQR, we will write the constraints and
> the implied as The nullspace matrix can be obtained
> numerically, but here it was simple enough to get from inspection,
> We can obtain the matrices $\bA_x \in
\Re^{6 \times 6}, \bB_x \in \Re^{6 \times 1}$ by linearizing the
> `MultibodyPlant` dynamics. Then we will call LQR with the
> projected matrices. Note that the we will use the discrete-time
> dynamics mode of the `MultibodyPlant` (by passing in a
> non-zero `time_step` to the constructor), because
> discrete-time dynamics engine contains our most advanced and robust
> algorithms for simulating contact.
>
> Importantly, although we design the LQR controller using the reduced
> model, we can run this controller directly on the original model.
### LQR for linear systems in implicit form
Consider a linear system given by the state-space equations, where may or may not be
invertible. We consider this model to be in "implicit form", and will
discuss much more about the use of implicit forms, and their strong
connections to mechanical systems, [in
the next chapter](/u/e7141078b86346f4#implicit). Under stabilizability and detectability
conditions[[2](#Mehrmann91)], the solution for the standard
infinite-horizon LQR cost function is given by where is the positive-definite solution to a generalized
Riccati equation: The
discrete-time form follows similarly.
### LQR as a convex optimization
One can also design the LQR gains using linear matrix inequalities
(LMIs). I will defer the derivation til we cover the policy gradient view
of LQR, because the LMI formulation is based on a change of variables
from the basic policy evaluation criterion. If you want to look ahead,
you can find that formulation [here](/u/d6f7edd745325532#lqr-lmi).
Solving the algebraic Riccati equation is still the preferred way of
computing the LQR solution. But it is helpful to know that one could
also compute it with convex optimization. In addition to deepening our
understanding, this can be useful for generalizing the basic LQR solution
(e.g. for [robust
stabilization](/u/e7141078b86346f4#common-lyapunov-linear)) or to solve for the LQR gains jointly as part of a
bigger optimization.
### Finite-horizon LQR via least squares
We can also obtain the solution to the discrete-time finite-horizon
(including the time-varying or tracking variants) LQR problem using
optimization -- in this case it actually reduces to a simple
least-squares problem. The presentation in this section can be viewed as
a simple implementation of the [Youla
parameterization](https://en.wikipedia.org/wiki/Youla%E2%80%93Kucera_parametrization) (sometimes called "Q-parameterization") from
controls. Small variations on the formulation here play an important
role in the minimax variants of LQR (which optimize a worst-case
performance), which we will discuss in the robust control chapter (e.g.
[[3](#Lofberg03), [4](#Sadraddini20)]).
First, let us appreciate that the default parameterization is not
convex. Given \begin{gather*} \min \sum_{n=0}^{N-1} \bx^T[n] {\bf Q}
\bx[n] + \bu^T[n] {\bf R} \bu[n], \qquad {\bf Q} = {\bf Q}^T \succeq 0,
{\bf R} = {\bf R}^T \succ 0 \\ \subjto \quad \bx[n+1] = {\bf A} \bx[n] +
{\bf B}\bu[n], \\ \bx[0] = \bx_0 \end{gather*} if we wish to search over
controllers of the form then we have
\begin{align*}\bx[1] &= {\bf A}\bx_0 + {\bf B}{\bf K}_0\bx_0, \\ \bx[2] &=
{\bf A}({\bf A} + {\bf BK}_0)\bx_0 + {\bf BK}_1({\bf A} + {\bf BK}_0)\bx_0
\\ \bx[n] &= \left( \prod_{i=0}^{n-1} ({\bf A} + {\bf BK}_i) \right) \bx_0
\end{align*} As you can see, the terms in the cost function
include our decision variables multiplied together -- resulting in a
non-convex objective. The trick is to re-parameterize the decision
variables, and write the feedback in the form: leading to \begin{align*}\bx[1] &= {\bf A}\bx_0 + {\bf
B}\tilde{\bf K}_0\bx_0, \\ \bx[2] &= {\bf A}({\bf A} + {\bf B}\tilde{\bf
K}_0)\bx_0 + {\bf B}\tilde{\bf K}_1 \bx_0 \\ \bx[n] &= \left( {\bf A}^n +
\sum_{i=0}^{n-1}{\bf A}^{n-i-1}{\bf B}\tilde{\bf K}_{i} \right) \bx_0
\end{align*} Now all of the decision variables, , appear
linearly in the solution to and therefore (convex) quadratically
in the objective.
We still have an objective function that depends on , but we
would like to find the optimal *for all*
. To achieve this let us evaluate the optimality conditions of this
least squares problem, starting by taking the gradient of the objective
with respect to , which is: We can satisfy this
optimality condition for all by solving the *linear* matrix
equation: We can always solve for
since it's multiplied by a (symmetric) positive definite
matrix (it is the sum of a positive definite matrix and many positive
semi-definite matrices), which is always invertible.
If you need to recover the original parameters, you can
extract them recursively with \begin{align*} \tilde{\bf K}_0 &= {\bf K}_0,
\\ \tilde{\bf K}_n &= {\bf K}_n \prod_{i=0}^{n-1} ({\bf A} + {\bf BK}_i),
\qquad 0 < n \le N-1. \end{align*} But often this is not actually
necessary. In some applications it's enough to know the performance cost
under LQR control, or to handle the response to disturbances explicitly
with the disturbance-based feedback (which I've already promised for the
robust control chapter). After all, the problem formulation that we've
written here, which makes no mention of disturbances, assumes the model is
perfect and the controls are just as suitable for
deployment as .
"System-Level Synthesis" (SLS) is the name for an important and
slightly different approach, where one optimizes the *closed-loop
response* directly[[5](#Anderson19)]. Although SLS is a very
general tool, for the particular formulation we are considering here it
reduces to creating additional decision variables , such at
that and writing the optimization above
as \begin{gather*} \min_{\tilde{\bf K}_*, {\bf \Phi}_*} \sum_{n=0}^{N-1}
\bx^T[0] \left( {\bf \Phi}_n^T {\bf Q \Phi}_n + \tilde{\bf K}_n^T{\bf R}
\tilde{\bf K}_n \right) \bx[0], \\ \subjto \qquad \forall n, \quad {\bf
\Phi}_{n+1} = {\bf A \Phi}_n + {\bf B}\tilde{\bf K}_n. \end{gather*}
Once again, the algorithms presented here are not as efficient as
solving the Riccati equation if we only want the solution to the simple
case, but they become very powerful if we want to combine the LQR
synthesis with other objectives/constraints. For instance, if we want to
add some sparsity constraints (e.g. enforcing that some elements of
are zero), then we could solve the quadratic
optimization subject to linear equality constraints
[[6](#Wang14)].
### Minimum-time LQR
Coming soon. The basic result can be found in [[7](#Verriest91)],
and some robotics applications in, for instance,
[[8](#Glassman10), [9](#Liu17)]. For discrete-time systems, [[10](#Marcucci21)] provides a new computational approach.
### Parameterized Riccati Equations
When we are designing LQR controllers based on a linearization (around
some operating state or trajectory) of a nonlinear system, then it is
interesting to ask: how does the LQR solution change as you change the
nominal point (or trajectory)? If the nonlinear dynamics are smooth, does
the solution also change smoothly?
The answer is yes -- the Riccati solution does change smoothly, and
its local variation can be obtained with a little bit of work by taking
Taylor approximation of the and matrices
[[11](#Majumdar12)].
## Exercises
> **Exercise 1 (LQR on a Drake Diagram)**
>
> To help you get a little more familiar with Drake, the exercise in
> will step you through the process of building a Drake Diagram, and
> designing an LQR controller for it. Systems and Diagrams are the way we
> write modular code describing dynamical systems, and implementing an
> algorithm like LQR against the System abstraction becomes very powerful.
## Notes
### Finite-horizon LQR derivation (general form)
For completeness, I've included here the derivation for continuous-time finite-horizon LQR with all of the bells and whistles.
Consider an time-varying affine (approximation of a) continuous-time
dynamical system in state-space form: and a running cost function in the general
quadratic form: \begin{gather*} \ell(t, \bx,\bu) = \begin{bmatrix} \bx \\
1 \end{bmatrix}^T {\bf Q}(t) \begin{bmatrix} \bx \\ 1 \end{bmatrix} +
\begin{bmatrix} \bu \\ 1 \end{bmatrix}^T {\bf R}(t) \begin{bmatrix} \bu
\\ 1 \end{bmatrix} + 2\bx^T{\bf N}(t)\bu, \\ \forall t\in[t_0, t_f],
\quad \bQ = \begin{bmatrix} \bQ_{xx} & \bq_x \\ \bq_x^T & q_0
\end{bmatrix}, \bQ_{xx} \succeq 0, \quad \bR(t) = \begin{bmatrix}
\bR_{uu} & {\bf r}_u \\ {\bf r}_u^T & r_0 \end{bmatrix}, \bR_{uu} \succ
0.\end{gather*} It's tempting to require that the cost is always
positive, e.g. with conditions of the form $r_0 - \bf{r}_u^T
\bR_{uu}^{-1} {\bf r}_u \ge 0$, but this is not actually necessary in the
finite-horizon case.
Observe that our LQR "optimal tracking" derivation fits in this form,
as we can always write by taking \begin{gather*} \bQ_{xx} = \bQ_t,\quad
\bq_x = -\bQ_t \bx_d - {\bf N}_t\bu_d, \quad q_0 = \bx_d^T \bQ_t \bx_d +
2 \bx_d^T {\bf N}_t \bu_d, \\ \bR_{uu} = \bR_t, \quad {\bf r}_u = -\bR_t
\bu_d - {\bf N}_t^T \bx_d, \quad r_0 = \bu_d^T \bR_t \bu_d, \quad {\bf N}
= {\bf N}_t.\end{gather*} Of course, we can also add a quadratic final
cost with . Let's search for a positive quadratic, time-varying
cost-to-go function of the form: \begin{gather*} J(t,
\bx)=\begin{bmatrix} \bx \\ 1 \end{bmatrix}^T {\bf S}(t) \begin{bmatrix}
\bx \\ 1 \end{bmatrix}, \quad {\bf S}(t) = \begin{bmatrix} {\bf S}_{xx} &
{\bf s}_x \\ {\bf s}_x^T & s_0 \end{bmatrix}, {\bf S}_{xx} \succeq
0, \\ \frac{\partial
J}{\partial \bx} = 2\bx^T{\bf S}_{xx} + 2{\bf s}_x^T, \quad
\frac{\partial J}{\partial t} = \begin{bmatrix} \bx \\ 1
\end{bmatrix}^T\dot{\bf S} \begin{bmatrix} \bx \\ 1 \end{bmatrix}.
\end{gather*} Writing out the HJB: \begin{gather*} \min_\bu
\left[\ell(\bx,\bu) + \frac{\partial J}{\partial \bx} \left[{\bf A}(t)\bx
+ {\bf B}(t)\bu + {\bf c}(t) \right] + \frac{\partial J}{\partial t}
\right] = 0, \end{gather*} we can find the minimizing with
\begin{gather*} \frac{\partial}{\partial \bu} = 2\bu^T{\bf R}_{uu} +
2{\bf r}_u^T + 2\bx^T{\bf N} + (2\bx^T{\bf S}_{xx} + 2{\bf s}_x^T){\bf B}
= 0 \\ \bu^* = -{\bf R}_{uu}^{-1} \begin{bmatrix} {\bf N} + {\bf S}_{xx}
\bB \\ {\bf r}^T_{u} + {\bf s}^T_{x}\bB \end{bmatrix}^T \begin{bmatrix}
\bx \\ 1 \end{bmatrix} = -{\bf K}(t) \begin{bmatrix} \bx \\ 1
\end{bmatrix} = -{\bf K}_x(t) \bx - {\bf k}_0(t). \end{gather*} Inserting
this back into the HJB gives us the updated Riccati differential
equation. Since this must hold for all , we can collect the
quadratic, linear, and offset terms and set them each individually equal
to zero, yielding: \begin{align*} -\dot{\bf S}_{xx} =& \bQ_{xx} - ({\bf
N} + {\bf S}_{xx} \bB){\bf R}_{uu}^{-1}({\bf N} + {\bf S}_{xx} \bB)^T +
{\bf S}_{xx}{\bf A} + {\bf A}^T{\bf S}_{xx}, \\ -\dot{\bf s}_x =& \bq_x -
({\bf N} + {\bf S}_{xx} \bB){\bf R}_{uu}^{-1} ({\bf r}_u + \bB^T {\bf
s}_x ) + {\bf A}^T{\bf s}_x + {\bf S}_{xx}{\bf c}, \\ -\dot{s}_0 =& q_0 +
r_0 - ({\bf r}_u + \bB^T {\bf s}_x)^T{\bf R}_{uu}^{-1} ({\bf r}_u + \bB^T
{\bf s}_x) + 2{\bf s}_x^T {\bf c}, \end{align*} with the final conditions
In the discrete-time version, we have and a running cost function of the form
\begin{gather*} \ell(n, \bx,\bu) =
\begin{bmatrix} \bx \\ 1 \end{bmatrix}^T {\bf Q}[n] \begin{bmatrix} \bx
\\ 1 \end{bmatrix} + \begin{bmatrix} \bu \\ 1 \end{bmatrix}^T {\bf R}[n]
\begin{bmatrix} \bu \\ 1 \end{bmatrix} + 2\bx^T{\bf N}[n]\bu, \\ \forall
n \in [n_0, n_f] \in \mathbb{Z}, \quad \bQ[n] = \begin{bmatrix} \bQ_{xx}
& \bq_x \\ \bq_x^T & q_0 \end{bmatrix}, \bQ_{xx} \succeq 0, \quad \bR[n]
= \begin{bmatrix} \bR_{uu} & {\bf r}_u \\ {\bf r}_u^T & r_0
\end{bmatrix}, \bR_{uu} \succ 0. \end{gather*} and a terminal cost
evaluated at of the form
Let's search for a positive quadratic, time-varying cost-to-go of the
form: \begin{gather*} J(n,\bx) = \begin{bmatrix} \bx \\ 1 \end{bmatrix}
{\bf S}[n]\begin{bmatrix}\bx \\ 1 \end{bmatrix}, \,{\bf S}[n] =
\begin{bmatrix} {\bf S}_{xx} & {\bf s}_x \\ {\bf s}_x^T & s_0
\end{bmatrix}, \quad {\bf S}_{xx} \succeq 0, s_0 - \bf{s}_x^T {\bf
S}_{xx}^+ \bf{s}_x \ge 0. \end{gather*} Writing out the HJB: we can find the minimizing (with the time
dependence suppressed for brevity) with \begin{gather*}
\frac{\partial}{\partial \bu} = 2\bu^T{\bf R}_{uu} + 2{\bf r}_u^T +
2\bx^T{\bf N} + 2((\bA\bx + \bB\bu + {\bf c})^T{\bf S}_{xx} + {\bf
s}_x^T){\bf B} = 0 \\ \bu^* = -({\bf R}_{uu} + \bB^T{\bf S}_{xx}\bB)^{-1}
\begin{bmatrix} {\bf N} + \bA^T{\bf S}_{xx} \bB, \\ {\bf r}^T_{u} + {\bf
c}^T{\bf S}_{xx} \bB + {\bf s}^T_{x}\bB \end{bmatrix}^T \begin{bmatrix}
\bx \\ 1 \end{bmatrix} = -{\bf K}[n] \begin{bmatrix} \bx \\ 1
\end{bmatrix} = -{\bf K}_x[n] \bx - {\bf k}_0[n]. \end{gather*} Inserting
this back into the HJB gives us the updated Riccati difference equation,
we obtain: \begin{align*} {\bf S}_{xx}[n] =& \, \bQ_{xx} +
\bA^T{\bf S}_{xx}\bA - ({\bf N}+\bA^T{\bf S}_{xx}\bB)(\bR_{uu}+\bB^T{\bf
S}_{xx}\bB)^{-1}({\bf N}+\bA^T{\bf S}_{xx}\bB)^T, \\ {\bf s}_x[n] =&
\,\bq_x + \bA^T{\bf S}_{xx} c + \bA^T{\bf s}_x \\ &- ({\bf N}+\bA^T{\bf
S}_{xx}\bB)(\bR_{uu}+\bB^T{\bf S}_{xx}\bB)^{-1}({\bf r}_u + \bB^T{\bf
S}_{xx} {\bf c} + \bB^T{\bf s}_x), \\ s_0[n] =&\, q_0 + r_0 + s_0 + {\bf
c}^T{\bf S}_{xx}{\bf c} + 2 {\bf c}^T {\bf s}_x \\ &- ({\bf r}_u +
\bB^T{\bf S}_{xx} {\bf c} + \bB^T{\bf s}_x)^T(\bR_{uu}+\bB^T{\bf
S}_{xx}\bB)^{-1}({\bf r}_u + \bB^T{\bf S}_{xx} {\bf c} + \bB^T{\bf s}_x).
\end{align*} where the terms on the right hand side evaluated
at time , and all remaining time-dependent terms ( etc)
are evaluated at time .
Phew! Numerical solutions to these equations can be obtained by
calling the [`FiniteHorizonLinearQuadraticRegulator`](https://drake.mit.edu/doxygen_cxx/group__control.html#ga58307d2135757a498c434e96d7b99853)
methods in [Drake](http://drake.mit.edu). May you never have to type them in and unit
test them yourself.
## References
1. <a id="Posa15"></a>
Michael Posa and Scott Kuindersma and Russ Tedrake,
"Optimization and stabilization of trajectories for constrained dynamical systems",
Proceedings of the International Conference on Robotics and Automation (ICRA) , pp. 1366-1373, May, 2016.
[ [link](http://groups.csail.mit.edu/robotics-center/public_papers/Posa15.pdf) ]
2. <a id="Mehrmann91"></a>
Volker Mehrmann,
"The autonomous linear quadratic control problem: theory and numerical solution", Springer
, no. 163, 1991.
3. <a id="Lofberg03"></a>
J. Lofberg,
"Approximations of closed-loop minimax MPC",
42nd IEEE International Conference on Decision and Control (IEEE Cat. No.03CH37475) , vol. 2, pp. 1438--1442 Vol.2, Dec, 2003.
4. <a id="Sadraddini20"></a>
Sadra Sadraddini and Russ Tedrake,
"Robust Output Feedback Control with Guaranteed Constraint Satisfaction",
In the Proceedings of 23rd ACM International Conference on Hybrid Systems: Computation and Control , pp. 12, April, 2020.
[ [link](http://groups.csail.mit.edu/robotics-center/public_papers/Sadraddini20.pdf) ]
5. <a id="Anderson19"></a>
James Anderson and John C. Doyle and Steven Low and Nikolai Matni,
"System Level Synthesis",
arXiv:1904.01634 [cs, math], apr, 2019.
6. <a id="Wang14"></a>
Yuh-Shyang Wang and Nikolai Matni and John C. Doyle,
"Localized LQR Optimal Control",
arXiv:1409.6404 [cs, math], September, 2014.
7. <a id="Verriest91"></a>
E.I. Verriest and F.L. Lewis,
"On the Linear Quadratic Minimum-Time Problem",
IEEE Transactions on Automatic Control, vol. 36, no. 7, pp. 859-863, July, 1991.
8. <a id="Glassman10"></a>
Elena Glassman and Russ Tedrake,
"A Quadratic Regulator-Based Heuristic for Rapidly Exploring State Space",
Under review, 2010.
[ [link](http://groups.csail.mit.edu/robotics-center/public_papers/Glassman10.pdf) ]
9. <a id="Liu17"></a>
Sikang Liu and Nikolay Atanasov and Kartik Mohta and Vijay Kumar,
"Search-based motion planning for quadrotors using linear quadratic minimum time control",
2017 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS) , pp. 2872-2879, Sep., 2017.
10. <a id="Marcucci21"></a>
Tobia Marcucci and Jack Umenberger and Pablo A. Parrilo and Russ Tedrake,
"Shortest Paths in Graphs of Convex Sets",
arxiv, 2023.
[ [link](http://groups.csail.mit.edu/robotics-center/public_papers/Marcucci21.pdf) ]
11. <a id="Majumdar12"></a>
Anirudha Majumdar and Mark Tobenkin and Russ Tedrake,
"Algebraic Verification for Parameterized Motion Planning Libraries",
Proceedings of the 2012 American Control Conference (ACC) , pp. 8, June, 2012.
[ [link](http://groups.csail.mit.edu/robotics-center/public_papers/Majumdar12.pdf) ]