ModelPredictiveController<T>
Models & Types · AiDotNet.Control
Linear model predictive control: at every step, plan the whole future subject to the actual limits, then apply only the first move.
For Beginners
Think of driving. You look ahead as far as you can see, work out a whole route that respects the lane edges and the speed limit, then turn the wheel by just the amount that route calls for right now. A moment later you look again and re-plan from where you actually are. You never execute the whole plan, but you could not choose the current move well without having made one.
How It Works
Minimizes Σ (xₖᵀQxₖ + uₖᵀRuₖ) + x_NᵀP_f x_N over a finite horizon subject to the dynamics
and to bounds on the inputs and states, by solving one quadratic program per step. Follows the
standard condensed formulation; see J. B. Rawlings, D. Q. Mayne and M. M. Diehl, Model
Predictive Control: Theory, Computation, and Design (2nd ed., Nob Hill 2017), and the survey
D. Q. Mayne, J. B. Rawlings, C. V. Rao and P. O. M. Scokaert, “Constrained model predictive
control: Stability and optimality”, Automatica 36(6), 2000, pp. 789-814.
Why plan the whole horizon and then discard all but the first move. It sounds wasteful, and it is what makes MPC work. The plan is optimal for the model, and the model is wrong; by the next step the true state is not where the plan said it would be. Re-planning from the state that actually occurred turns an open-loop plan into feedback, and the discarded tail was never meant to be executed — it exists so that the first move accounts for what comes after it.
Where it beats LQR. Nowhere, when nothing is constrained: with the terminal cost set to the infinite-horizon Riccati solution the two produce identical inputs, and the regulator is far cheaper. The moment an actuator saturates or a state must not be exceeded, they diverge — the regulator commands what it cannot deliver and a clipped LQR is no longer optimal or even necessarily stable, while MPC plans around the limit. That is the entire trade: MPC costs a quadratic program per step and buys constraints.
Condensing. The states are eliminated rather than kept as variables, so the quadratic
program is over the inputs alone — horizon × inputs variables rather than
horizon × (states + inputs). Everything that depends only on the model and the costs is
built once in the constructor; each step recomputes only the linear term, which is the part that
depends on the current state.
Example
// A double integrator whose actuator saturates at ±0.5: state is (position, velocity).
var a = new Matrix<double>(new double[,] { { 1.0, 1.0 }, { 0.0, 1.0 } });
var b = new Matrix<double>(new double[,] { { 0.5 }, { 1.0 } });
var q = Matrix<double>.CreateIdentity(2);
var r = Matrix<double>.CreateIdentity(1);
var currentState = new Vector<double>(new double[] { 1.0, 0.0 });
var options = new ModelPredictiveControllerOptions<double>
{
Horizon = 20,
InputLowerBounds = new Vector<double>(new[] { -0.5 }),
InputUpperBounds = new Vector<double>(new[] { 0.5 }),
};
var controller = new ModelPredictiveController<double>(a, b, q, r, options);
var input = controller.ComputeControl(currentState);
Constructors
| Constructor | Summary |
|---|---|
ModelPredictiveController(Matrix<T> stateMatrix, Matrix<T> inputMatrix, Matrix<T> stateCost, Matrix<T> inputCost, ModelPredictiveControllerOptions<T> options, IQuadraticProgramSolver<T> solver) |
Creates a model predictive controller and precomputes everything that does not depend on the current state. |
Properties
| Property | Summary |
|---|---|
LinearProgramStatus LastStatus { get; set; } |
Gets the status of the most recent solve. |
Matrix<T> TerminalCost { get; } |
Gets the terminal cost matrix in use. |
int Horizon { get; } |
Gets the prediction horizon. |
Methods
| Method | Summary |
|---|---|
Matrix<T> ComputePlan(Vector<T> state) |
Computes the whole planned input sequence over the horizon. |
Vector<T> ComputeControl(Vector<T> state) |
Computes the input to apply now, by planning the whole horizon and keeping the first move. |