configure optimization solver for nonlinear mpc -凯发k8网页登录

main content

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:

  1. return the opt output argument when calling . this object contains any run-time options you specified in the previous call to nlmpcmove. it also includes the initial guesses for the state (opt.x0) and manipulated variable (opt.mv0) trajectories, and the global slack variable (opt.slack0).

  2. pass this object in as the options input argument to nlmpcmove 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.

argumentinput/outputdescription
funinput

nonlinear cost function to minimize, specified as a handle to a function with the signature:

[f,g] = fun(z)

and arguments:

  • z — decision variables, specified as a vector of length nz.

  • f — cost, returned as a scalar.

  • g — cost function gradients with respect to the decision variables, returned as a column vector of length nz, where g(i)=f/z(i).

z0inputinitial guesses for decision variable values, specified as a vector of length nz
ainputlinear inequality constraint array, specified as an mcineq-by-nz array. together, a and b define constraints of the form azb.
binputlinear inequality constraint vector, specified as a column vector of length mcineq. together, a and b define constraints of the form azb.
aeqinputlinear equality constraint array, specified as an mceq-by-nz array. together, aeq and beq define constraints of the form aeqz=beq.
beqinputlinear equality constraint vector, specified as a column vector of length mceq. together, aeq and beq define constraints of the form aeqz=beq.
lbinputlower bounds for decision variables, specified as a column vector of length nz, where lb(i)z(i).
ubinputupper bounds for decision variables, specified as a column vector of length nz, where z(i)ub(i).
nlconinput

nonlinear constraint function, specified as a handle to a function with the signature:

[cineq,c,gineq,geq] = nlcon(z)

and arguments:

  • z — decision variables, specified as a vector of length nz.

  • cineq — nonlinear inequality constraint values, returned as a column vector of length ncineq.

  • ceq — nonlinear equality constraint values, returned as a column vector of length nceq.

  • gineq — nonlinear inequality constraint gradients with respect to the decision variables, returned as an nz-by-ncineq array, where gineq(i,j)=cineq(j)/z(i).

  • geq — nonlinear equality constraint gradients with respect to the decision variables, returned as an nz-by-nceq array, where geq(i,j)=ceq(j)/z(i).

zoptoutputoptimal decision variable values, returned as a vector of length nz.
costoutputoptimal cost, returned as a scalar.
flagoutput

exit flag, returned as one of the following:

  • 1 — optimization successful (first order optimization conditions satisfied)

  • 0 — suboptimal solution (maximum number of iterations reached)

  • negative value — optimization failed

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) |

objects

related examples

    more about

      网站地图