NonlinearModelPredictiveController<T>
Models & Types · AiDotNet.Control
Nonlinear model predictive control by sequential quadratic programming: repeatedly linearize the plan you have, improve it with a quadratic program, and relinearize about the result.
For Beginners
Straight-line thinking works fine over short distances even on a curved road. This plans as if the system were linear, checks what the real system would actually have done, corrects for the difference, and repeats until the plan stops changing — then applies only its first move, and does the whole thing again next step.
How It Works
Handles systems of the form x[k+1] = f(x[k], u[k]) where f is any differentiable
function. Follows the sequential quadratic programming approach to nonlinear predictive control;
see M. Diehl, H. G. Bock, J. P. Schlöder, R. Findeisen, Z. Nagy and F. Allgöwer, “Real-time
optimization and nonlinear model predictive control of processes governed by differential-
algebraic equations”, Journal of Process Control 12(4), 2002, pp. 577-585, and Chapter 8 of
Rawlings, Mayne and Diehl.
How it works. A nonlinear predictive control step is not a quadratic program and cannot be solved as one. But a correction to an existing plan very nearly is: simulate the current plan through the true nonlinear dynamics, linearize about the trajectory that produces, and the question “how should I adjust this plan” becomes exactly the linear problem the previous class solves. Apply the correction, simulate again, repeat. Each pass costs one quadratic program and the sequence converges to the nonlinear optimum.
Why the linearization must be time-varying. The trajectory visits a different point of the
state space at every step, and the system’s behaviour there is different — that is what nonlinear
means. So there is a distinct Aₖ and Bₖ for each step of the horizon, and the
condensed dynamics accumulate products of them rather than powers of a single matrix. This is the
one structural difference from ModelPredictiveController, and it is why that
class cannot simply be called in a loop.
Warm starting matters more here than anywhere else. Consecutive control steps solve nearly the same problem, so the previous plan shifted forward by one step is an excellent guess — often good enough that a single sequential iteration suffices. That observation is the basis of the real-time iteration scheme, and it is what makes nonlinear predictive control possible at millisecond rates on real hardware.
Example
// A pendulum: state is (angle, rate), input is torque. The costs are weighting MATRICES, not
// scalars — identity here means every state and input is penalised equally.
var stateCost = Matrix<double>.CreateIdentity(2);
var inputCost = Matrix<double>.CreateIdentity(1);
var currentState = new Vector<double>(new double[] { 0.1, 0.0 });
var controller = new NonlinearModelPredictiveController<double>(
dynamics: (x, u) => x, // your discrete-time step, x_{k+1} = f(x_k, u_k)
jacobians: (x, u) => (Matrix<double>.CreateIdentity(2), Matrix<double>.CreateIdentity(2)),
stateCost: stateCost, inputCost: inputCost,
options: new NonlinearModelPredictiveControllerOptions<double> { Horizon = 20 });
var torque = controller.ComputeControl(currentState);
Constructors
| Constructor | Summary |
|---|---|
NonlinearModelPredictiveController(Func<Vector<T>, Vector<T>, Vector<T>> dynamics, Func<Vector<T>, Vector<T>, ValueTuple<Matrix<T>, Matrix<T>>> jacobians, Matrix<T> stateCost, Matrix<T> inputCost, NonlinearModelPredictiveControllerOptions<T> options, IQuadraticProgramSolver<T> solver) |
Creates a nonlinear model predictive controller. |
Properties
| Property | Summary |
|---|---|
int Horizon { get; } |
Gets the prediction horizon. |
int LastIterationCount { get; set; } |
Gets the number of sequential iterations the most recent step actually used. |
Methods
| Method | Summary |
|---|---|
Matrix<T> ComputePlan(Vector<T> state) |
Computes the whole refined input sequence over the horizon. |
Vector<T> ComputeControl(Vector<T> state) |
Computes the input to apply now. |