configure optimization solver for nonlinear mpc
by default, nonlinear mpc controllers solve a nonlinear programming problem using the
fmincon
function with the sqp algorithm, which requires optimization toolbox™ software. if you do not have optimization toolbox software, you can specify your own custom nonlinear solver.
solver decision variables
for nonlinear mpc controllers at time tk, the nonlinear optimization problem uses the following decision variables:
predicted state values from time tk 1 to tk p. these values correspond to rows 2 through p 1 of the
x
input argument of your cost, and constraint functions, where p is the prediction horizon.predicted manipulated variables from time tk to tk p-1. these values correspond to the manipulated variable columns in rows 1 through p of the
u
input argument of your cost, and constraint functions.
therefore, the number of decision variables nz is equal to p(nx nmv) 1, nx is the number of states, nmv is the number of manipulated variables, and the 1 is for the global slack variable.
specify initial guesses
a properly configured standard linear mpc optimization problem has a unique solution. however, nonlinear mpc optimization problems often allow multiple solutions (local minima), and finding a solution can be difficult for the solver. in such cases, it is important to provide a good starting point near the global optimum.
during closed-loop simulations, it is best practice to warm start your nonlinear solver. to do so, use the predicted state and manipulated variable trajectories from the previous control interval as the initial guesses for the current control interval. in simulink®, the nonlinear mpc controller block is configured to use these trajectories as initial guesses by default. to use these trajectories as initial guesses at the command line:
return the
opt
output argument when calling . this object contains any run-time options you specified in the previous call tonlmpcmove
. it also includes the initial guesses for the state (opt.x0
) and manipulated variable (opt.mv0
) trajectories, and the global slack variable (opt.slack0
).pass this object in as the
options
input argument tonlmpcmove
for the next control interval.
these command-line simulation steps are best practices, even if you do not specify any other run-time options.
configure fmincon
options
by default, nonlinear mpc controllers optimize their control move using the
fmincon
function from the optimization toolbox. when you first create your controller, the
optimization.solveroptions
property of the nlmpc
object contains the standard fmincon
options with the following
nondefault settings:
use the
sqp
algorithm (solveroptions.algorithm = 'sqp'
)use objective function gradients (
solveroptions.specifyobjectivegradient = 'true'
)use constraint gradients (
solveroptions.specifyconstraintgradient = 'true'
)do not display optimization messages to the command window (
solveroptions.display = 'none'
)
these nondefault options typically improve the performance of the nonlinear mpc controller.
you can modify the solver options for your application. for example, to specify the
maximum number of solver iterations for your application, set
solveroptions.maxiter
. for more information on the available solver
options, see (optimization toolbox).
in general, you should not modify the specifyobjectivegradient
and
specifyconstraintgradient
solver options, since doing so can
significantly affect controller performance. for example, the constraint gradient matrices
are sparse, and setting specifyconstraintgradient
to false would cause
the solver to calculate gradients that are known to be zero.
specify custom solver
if you do not have optimization toolbox software, you can specify your own custom nonlinear solver. to do so, create a custom wrapper function that converts the interface of your solver function to match the interface expected by the nonlinear mpc controller. your custom function must be a matlab® script or mat-file on the matlab path. for an example that shows a template custom solver wrapper function, see .
you can use the nonlinear programming solver developed by to simulate and generate code for nonlinear mpc controllers. for more information, see .
to configure your nlmpc
object to use your custom solver wrapper
function, set its optimization.customsolverfcn
property in one of the
following ways:
name of a function in the current working folder or on the matlab path, specified as a string or character vector
optimization.customsolverfcn = "mynlpsolver";
handle to a function in the current working folder or on the matlab path
optimization.customsolverfcn = @mynlpsolver;
your custom solver wrapper function must have the signature:
function [zopt,cost,flag] = mynlpsolver(fun,z0,a,b,aeq,beq,lb,ub,nlcon)
this table describes the inputs and outputs of this function, where:
nz is the number of decision variables.
mcineq is the number of linear inequality constraints.
mceq is the number of linear equality constraints.
ncineq is the number of nonlinear inequality constraints.
nceq is the number of nonlinear equality constraints.
argument | input/output | description |
---|---|---|
fun | input | nonlinear cost function to minimize, specified as a handle to a function with the signature: [f,g] = fun(z) and arguments:
|
z0 | input | initial guesses for decision variable values, specified as a vector of length nz |
a | input | linear inequality constraint array, specified as an
mcineq-by-nz
array. together, a and b define constraints of
the form . |
b | input | linear inequality constraint vector, specified as a column vector of length
mcineq. together, a
and b define constraints of the form . |
aeq | input | linear equality constraint array, specified as an
mceq-by-nz
array. together, aeq and beq define
constraints of the form . |
beq | input | linear equality constraint vector, specified as a column vector of length
mceq. together, aeq
and beq define constraints of the form . |
lb | input | lower bounds for decision variables, specified as a column vector of length nz, where . |
ub | input | upper bounds for decision variables, specified as a column vector of length nz, where . |
nlcon | input | nonlinear constraint function, specified as a handle to a function with the signature: [cineq,c,gineq,geq] = nlcon(z) and arguments:
|
zopt | output | optimal decision variable values, returned as a vector of length nz. |
cost | output | optimal cost, returned as a scalar. |
flag | output | exit flag, returned as one of the following:
|
when you implement your custom solver function, it is best practice to have your solver use the cost and constraint gradient information provided by the nonlinear mpc controller.
if you are unable to obtain a solution using your custom solver, try to identify a special condition for which you know the solution, and start the solver at this condition. if the solver diverges from this initial guess:
check the validity of the state and output functions in your prediction model.
if you are using a custom cost function, make sure it is correct.
if you are using the standard mpc cost function, verify the controller tuning weights.
make sure that all constraints are feasible at the initial guess.
if you are providing custom jacobian functions, validate your jacobians using .
see also
functions
- (optimization toolbox) |