Numerics#
When computing control energy, we mean that for a linear dynamical system of the form
we are computing the amount of energy it costs to bring the system from an initial state \(\mathbf{x}(0)\) to a final state \(\mathbf{x}(T)\) in \(T\) time through a set of input functions \(\mathbf{u}(t).\) This cost is called the control energy, and is defined by
From the theory, we found that the minimum control energy is given by
where \(W_c\) is the controllability Gramian. Here, we will discuss how to numerically evaluate the theoretical relationships. Because the specific states \(\mathbf{x}(0)\) and \(\mathbf{x}(T)\) are just vectors, we will focus on the terms \(e^{AT}\) and \(W_c.\)
The Matrix Exponential#
The matrix exponential has a long history in both analytical and numerical applications. But what is the exponential of a matrix? Fortunately, there is a relatively simple way to conceptualize it through a Taylor series expansion:
So in one sense, the matrix exponential is just a weighted sum of powers of \(A,\) so nothing too fancy. Most numerical packages will have built in functions for the evaluation of matrix exponentials. Please note that the matrix exponential is not simply an element-wise exponential of \(A.\) As we can see in the series expansion, the matrix exponential contains matrix powers of \(A,\) which are very different from element-wise operations.
nctpy uses scipy.linalg.expm() (a Padé approximation with scaling and squaring). nctpy.energies.get_control_inputs() can instead use an eigendecomposition, nctpy.utils.expm(), with expm_version="eig".
The Controllability Gramian#
Of great importance to linear network control is the controllability Gramian, defined in the theory as
We can simplify this expression a bit through a simple change of variables \(\tau = T-t\) (classic exercise left to reader) to write
So… how do we turn this equation into a matrix on our computer? Well, it is no different than any other form of numerical integration! It just looks a bit scary because of the matrices, but never fear. Let us start with a very simple approach.
built-in numerical integrators#
Perhaps the most simple approach would be to use a built-in numerical integrator that accepts matrix-valued functions. In Python, for example,
import scipy as sp
T = 1
Wc, _ = sp.integrate.quad_vec(lambda t: sp.linalg.expm(A * t) @ B @ B.T @ sp.linalg.expm(A.T * t), 0, T)
right-hand Riemann sum#
Provided our particular numerical package lacks a matrix-valued numerical integrator, we can use the most basic integration scheme: the right-hand Riemann sum. The basic idea is that rather than compute the exact area under the curve, we can just evaluate the curve at different points in time, and pretend the function is constant in between points. So if we break up our integration into time steps of \(\Delta T,\) then our Gramian approximately evaluates to
Because we assume our numerical package can evaluate matrix integrals and perform matrix multiplications, we can perform this summation without much issue. We can speed up this process by recognizing that the product of matrix exponentials yields the sum of their exponents (when the matrices in the exponentials commute), such that
Hence, we only ever actually have to evaluate one matrix exponential, \(e^{A\Delta T},\) and can obtain all subsequent matrix exponentials by accumulating products of matrix exponentials.
Simpson’s rule#
While the Riemann sum is simple, it is also prone to errors if the function being integrated changes too quickly with respect to the time step, and might require too small of a time step \(\Delta T.\) Now, taking \(T / \Delta T\) products of matrices shouldn’t be too computationally expensive given the system is not too large. However, another issue arises when \(A\Delta T\) becomes too small to evaluate to numerical precision. For example, if we require 10,000 time steps to accurately capture the curvature of the matrix exponential, then we need to accurately compute \(e^{A/10,000},\) and then accurately multiply those very small matrices 10,000 times. This approach can lead to the exponential growth of numerical precision errors.
Instead, we can use Simpson’s rule, which essentially uses higher-order polynomials to fit the curvature of functions. So, rather than assuming the function stays constant at each sampled point as in the Riemann sum, we instead fit polynomials, which ultimately evaluates to
More advanced versions of this polynomial integration scheme can be found in the Newton-Cotes formulas.
what nctpy does#
nctpy.energies.gramian(), which computes the Gramian with every node controlled (\(B = I\)), combines the two ideas: it evaluates \(e^{A\Delta T}\) once, with \(\Delta T = 0.001,\) accumulates the products \(e^{An\Delta T}\) step by step, and integrates with Simpson’s rule (scipy.integrate.simpson()). In discrete time the Gramian is a sum rather than an integral, \(\sum_{k=0}^{T} A^k (A^\top)^k,\) which nctpy accumulates in the same way.
the infinite horizon#
For a stable system, the Gramian over an infinite horizon needs no integration at all: it solves the Lyapunov equation \(AW_c + W_cA^\top + BB^\top = 0\) (in discrete time, \(AW_cA^\top - W_c + BB^\top = 0\)), for which SciPy has direct solvers (scipy.linalg.solve_continuous_lyapunov(), scipy.linalg.solve_discrete_lyapunov()). nctpy uses them for gramian(A_norm, np.inf, system), nctpy.energies.minimum_energy_infinite() and nctpy.energies.average_energy_infinite(). When few nodes are controlled, \(W_c\) is close to singular, and inverting it loses accuracy; see Measures of control energy.
Evaluating Minimum Control Energy#
Now, once we have our controllability Gramian and state transitions, we evaluate the minimum control energy using
But let’s pause for a moment here. Notice that the controllability Gramian is only a function of the connectivity matrix \(A,\) the input matrix \(B,\) and the time horizon \(T\) as we reproduce below
What this means is that for any analysis that involves assessing many state transitions for one set of system parameters \(A,B,T,\) we only have to compute the Gramian once, and invert the Gramian once. After obtaining \(W_c^{-1},\) we can evaluate all of the energies for all state transitions through simple matrix multiplications, which are computationally way more efficient. The matrix exponential, \(e^{AT},\) likewise only has to be evaluated once.
nctpy.energies.minimum_energy_fast() does exactly this: it computes \(W_c\) (by Simpson’s rule, as above) and \(e^{AT}\) once per \(A, B, T,\) reuses them while consecutive calls share them, and evaluates the energies of all the transitions it is given at once, using the pseudoinverse of \(W_c.\) See Performance.
Evaluating Optimal Control#
nctpy.energies.get_control_inputs() solves the optimal control problem described in Theory, with the trajectory constraint \(S,\) the weight \(\rho\) and the reference state \(\mathbf{x}_r.\)
continuous time#
The state and the costate evolve together as \(\dot{\mathbf{z}} = M\mathbf{z} + \mathbf{c},\) with \(\mathbf{z} = [\mathbf{x}; \mathbf{p}],\) the matrix \(M = \begin{bmatrix} A & -BB^\top/(2\rho) \\ -2S & -A^\top \end{bmatrix}\) and the constant \(\mathbf{c} = [\mathbf{0}; 2S\mathbf{x}_r].\) nctpy
computes \(e^{MT}\) and \(e^{M\Delta T},\) with \(\Delta T = 0.001;\)
writes the final state, \(\mathbf{x}(T),\) in terms of the initial state and the unknown initial costate through the top block row of \(e^{MT},\) and solves that linear system for \(\mathbf{p}(0)\);
steps \(\mathbf{z}\) forward exactly, multiplying by \(e^{M\Delta T}\) at each step, which gives the trajectory at \(T/\Delta T + 1\) time points;
reads off the input, \(\mathbf{u}(t) = -B^\top\mathbf{p}(t)/(2\rho).\)
The two numerical errors it returns come from steps 2 and 3: the inversion error is the residual of the linear solve for \(\mathbf{p}(0),\) and the reconstruction error is the distance between the computed \(\mathbf{x}(T)\) and the target state. See Error terms and failed transitions.
discrete time#
In discrete time nctpy writes the state and costate equations of every time step, with the known \(\mathbf{x}(0)\) and \(\mathbf{x}(T),\) as one sparse linear system in all the unknown states and costates, and solves it with a sparse LU factorisation. The inversion error is the residual of that solve, and the reconstruction error is how far the solution departs from the dynamics.
reuse#
The matrix exponentials of step 1, and in discrete time the system and its LU factorisation, depend only on \(A, T, B, S\) and \(\rho,\) not on the states. nctpy computes them once and reuses them while consecutive calls share them, so a loop over the transitions of one system is fast. Results are the same either way.
from inputs to energy#
nctpy.energies.integrate_u() integrates each node’s squared input with Simpson’s rule, treating the time points as one unit apart. In continuous time they are \(\Delta T = 0.001\) apart, so its energies are \(1/\Delta T = 1000\) times the integral \(\int_0^T \lVert\mathbf{u}(t)\rVert^2 \mathrm{d}t\) that the formulas above compute. See Measures of control energy.