qp solvers
the model predictive controller qp solver converts a linear mpc optimization problem to the general form qp problem
subject to the linear inequality constraints
where
x is the solution vector.
h is the hessian matrix. this matrix is constant when your prediction model and tuning weights do not change at run time.
a is a matrix of linear constraint coefficients. this matrix is constant when your prediction model does not change at run time.
b and f are vectors.
at the beginning of each control interval, the controller computes h, f, a, and b. if h or a is constant, the controller retrieves their precomputed values.
built-in qp solvers
model predictive control toolbox™ software supports two built-in algorithms for solving the qp problem. both solvers require the hessian matrix to be positive definite.
active-set solver — this solver can provide fast and robust performance for small-scale and medium-scale optimization problems in both single and double precision. the active-set solver uses the kwik algorithm from [1]. to use the active-set solver, set the
optimizer.algorithm
property of your mpc controller to'active-set'
. to configure the algorithm settings, use theoptimizer.activesetoptions
property of your controller.interior-point solver — this solver can provide superior performance for large-scale optimization problems, such as mpc applications that enforce constraints over large prediction and control horizons. this interior-point solver uses a primal-dual algorithm with a mehrotra predictor-corrector. to use the interior-point solver, set the
optimizer.algorithm
property of your mpc controller to'interior-point'
. to configure the algorithm settings, use theoptimizer.interiorpointoptions
property of your controller.
solver configuration
when selecting and configuring the qp solver for your application, consider the following:
the size and configuration of the mpc problem affects the performance of the built-in qp solvers. to determine which solver is best for your application, consider simulating your controller across multiple simulation scenarios using both qp solvers.
the interior-point solver is more sensitive to solver parameters than the active-set solver. therefore, it can require more adjustment to find an optimal balance between performance and robustness.
the active-set solver also uses a nonadjustable tolerance when testing for an optimal solution. you can adjust the optimality tolerances for the interior-point solver.
one or more linear constraints can be violated slightly due to numerical round-off errors. such violations are normal and do not generate warning messages. to adjust the tolerance for acceptable constraint violations, use the
constrainttolerance
setting for either the active-set or interior-point solver.the search for a qp solution is an iterative process. for either solver, you can specify the maximum number of iterations using the corresponding
maxiterations
setting. if the number of iterations reaches the maximum, the algorithm terminates.the default maximum number of iterations for the active-set solver is , where nc and nv are the number of constraints and optimization variables across the prediction horizon, respectively. for some controller configurations, this value can be very large, which can make the qp solver appear to stop responding. this value has a lower bound of
120
.the default maximum number of iterations for the interior-point solver is
50
.if your mpc problem includes hard constraints after conversion to a qp problem, the qp inequality constraints can be infeasible (impossible to satisfy). if the qp solver detects infeasibility, it terminates immediately.
when the solver detects an infeasible problem or reaches the maximum number of iterations without finding an optimal solution, the controller retains the last successful control output. for more information, see . you can detect an abnormal outcome and override the default behavior as you see fit.
in the first control step, the qp solvers use a cold start, in which the initial guess is the unconstrained solution described in . if x satisfies the constraints, it is the optimal qp solution and the algorithm terminates. otherwise, at least one of the linear inequality constraints must be satisfied as an equality, and the solver computes the optimal solution. for subsequent control steps:
the active-set solver uses a warm start where the active constraint set determined in the previous control step becomes the initial guess.
the interior-point solver continues to use a cold start.
suboptimal qp solution
for a given mpc application with constraints, there is no way to predict how many qp solver iterations are required to find an optimal solution. also, in real-time applications, the number of iterations can change dramatically from one control interval to the next. in such cases, the worst-case execution time can exceed the limit that is allowed on the hardware platform and determined by controller sample time.
you set a guaranteed worst-case execution time for your mpc controller by applying a
suboptimal solution after the number of optimization iterations exceeds a specified
maximum value. to set the worst-case execution time, first determine the time needed for a
single optimization iteration by experimenting with your controller under nominal
conditions. then, set an upper bound on the number of iterations per control interval. for
example, if it takes around 1 ms to compute each iteration on the hardware and the
controller sample time is 10 ms, set the maximum number of iterations to no greater than
10
.
mpcobj.optimizer.activesetoptions.maxiterations = 10;
by default, an mpc controller object has a lower bound of 120
on
the maximum number of iterations for the active-set solver.
by default, when the solver reaches the maximum number of solver iterations without an
optimal solution, the controller holds the manipulated variables at their previous values.
to use the suboptimal solution reached after the final iteration, set the
usesuboptimalsolution
option to true
.
mpcobj.optimizer.usesuboptimalsolution = true;
while the solution is not optimal, the mpc controller adjusts the solution such that it satisfies all your specified constraints.
there is no guarantee that the suboptimal solution performs better than if you hold the controller output constant. you can simulate your system using both approaches, and select the configuration that provides better controller performance.
for an example, see .
custom qp applications
to access the qp solvers for applications that require solving online qp problems, use the and functions, which are useful for:
advanced mpc applications that are beyond the scope of model predictive control toolbox software.
custom qp applications, including applications that require code generation.
custom qp solver
model predictive control toolbox software lets you specify a custom qp solver for your mpc controller. this solver is called in place of the built-in solvers at each control interval. this option is useful for:
validating your simulation results or generating code with an in-house third-party solver that you trust.
applications where the built-in solvers do not provide satisfactory performance for your specific problem.
you can define a custom solver for simulation or for code generation. in either instance, you define the custom solver using a custom function and configure your controller to use this custom function.
task | custom solver function | affected matlab® functions | affected simulink® blocks |
---|---|---|---|
simulation set
|
supports:
|
|
|
code generation set
|
supports:
|
|
custom solver for simulation
to simulate an mpc controller with a custom qp solver, perform the following steps.
copy the solver template file to your working folder or anywhere on the matlab path, and rename it
mpccustomsolver.m
. to copy the solver template to your current working folder, type the following at the matlab command line.src = which('mpccustomsolver.txt'); dest = fullfile(pwd,'mpccustomsolver.m'); copyfile(src,dest,'f');
modify
mpccustomsolver.m
by adding your own custom solver. your solver must be able to run in matlab and be implemented in a matlab script or mex file.configure your mpc controller
mpcobj
to use the custom solver.mpcobj.optimizer.customsolver = true;
the software now uses your custom solver for simulation in place of the built-in qp kwik solver.
simulate your controller. for more information, see .
for an example, see .
custom solver for code generation
you can generate code for mpc controllers that use a custom qp solver written in either c/c code or matlab code suitable for code generation.
to do so at the command line, you must have matlab coder™ software.
to do so in simulink you must have simulink coder or simulink plc coder™ software.
to generate code for mpc controllers that use a custom qp solver, perform the following steps.
copy the solver template file to your working folder or anywhere on the matlab path, and rename it
mpccustomsolvercodegen.m
. to copy the matlab code template to your current working folder, type the following at the matlab command line.src = which('mpccustomsolvercodegen_templateeml.txt'); dest = fullfile(pwd,'mpccustomsolvercodegen.m'); copyfile(src,dest,'f');
alternatively, you can use the c template.
src = which('mpccustomsolvercodegen_templatec.txt'); dest = fullfile(pwd,'mpccustomsolvercodegen.m'); copyfile(src,dest,'f');
modify
mpccustomsolvercodegen.m
by adding your own custom solver.configure your mpc controller
mpcobj
to use the custom solver.mpcobj.optimizer.customsolvercodegen = true;
the software now uses your custom solver for code generation in place of the built-in qp kwik solver.
generate code for the controller. for more information, see .
for an example, see .
custom solver for both simulation and code generation
you can implement the same custom qp solver for both simulation and code generation. to do so:
set both
optimizer.customsolver
andoptimizer.customsolvercodegen
totrue
.create both
mpccustomsolver.m
andmpccustomsolvercodegen.m
.
during simulation, your controller uses the mpccustomsolver.m
custom function. for code generation, your controller uses the
mpccustomsolvercodegen.m
custom function.
you can specify the same matlab code in both custom solver functions, provided the code is suitable for code generation.
if you implement mpccustomsolvercodegen.m
using c/c code, create
a mex file using the code. you can then call this mex file from
mpccustomsolver.m
. for more information on creating and using mex
files, see .
custom solver function implementation
when you implement a custom qp solver, your custom function must have one of the following signatures:
custom solver for simulation:
function [x,status] = mpccustomsolver(h,f,a,b,x0)
custom solver for code generation:
function [x,status] = mpccustomsolvercodegen(h,f,a,b,x0)
for both simulation and code generation, your custom solver has the following input and output arguments.
h
is a hessian matrix, specified as an n-by-n symmetric positive definite matrix, where n is the number of optimization variables.f
is the multiplier of the objective function linear term, specified as a column vector of length n.a
is a matrix of linear inequality constraint coefficients, specified as an m-by-n matrix, where m is the number of constraints.b
is the right side of the inequality constraint equation, specified as a column vector of length m.x0
is an initial guess for the solution, specified as a column vector of length n.x
is the optimal solution, returned as a column vector of length n.status
is a solution validity indicator, returned as an integer as shown in the following table.value description > 0
x
is optimal.status
represents the number of iterations performed during optimization.0
the maximum number of iterations was reached without finding an optimal solution. the solution
x
might be suboptimal or infeasible.if the
optimizer.usesuboptimalsolution
property of your controller istrue
, the controller uses the suboptimal solution inx
whenstatus
is0
.-1
the problem appears to be infeasible, that is, the constraints cannot be satisfied. -2
an unrecoverable numerical error occurred.
note
the mpc controller expects the custom solver functions to solve the qp problem
subject to the linear inequality constraints . if your custom solver uses , you must change the sign of both a
and
b
before passing them to your custom solver code.
use quadprog
as custom qp solver
you can configure an mpc object to use the active-set solver available with the (optimization toolbox) function as a custom qp solver.
to automatically configure the mpc object mpcobj
to use
quadprog
as custom qp solver for both simulation and code generation,
you can use the
function. specifically at the matlab command prompt, enter the
following.
setcustomsolver(mpcobj,'quadprog')
mpccustomsolver.m
and
mpccustomsolvercodegen.m
, which internally call (optimization toolbox). it then sets mpcobj.optimizer.customsolver
and
mpcobj.optimizer.customsolvercodegen
to
true
.you can also further customize these functions, for example by adjusting the solver
options, provided that you use the active-set solver (since other
quadprog
solvers are not supported for mpc problems).
to revert mpcobj
back to use the built-in algorithm specified in
mpcobj.optimizer.algorithm
for both simulation and code generation,
call setcustomsolver
as
follows.
setcustomsolver(mpcobj,'quadprog')
mpcobj.optimizer.customsolver
and
mpcobj.optimizer.customsolvercodegen
to
false
.integration with forcespro solver
you can use forcespro, a real-time embedded optimization software tool developed by embotech ag, to simulate and generate code for an mpc controller designed using model predictive control toolbox software. starting with forcespro 2.0, embotech provides a plugin that leverages the design capabilities of model predictive control toolbox software and the computational performance of forcespro. using the plugin, you can generate a custom qp solver that allows deployment on real-time hardware and is highly optimized based on your specific mpc problem to achieve satisfactory real-time performance. especially long-horizon mpc problems can be solved very efficiently.
for information on using the forcespro solver together with model predictive control toolbox software, see .
references
[1] schmid, c., and l.t. biegler. "quadratic programming methods for reduced hessian sqp." computers & chemical engineering 18, no. 9 (september 1994): 817–32. .
see also
functions
- | | | | | | (optimization toolbox)