Main Content

Configure Optimization Solver for Nonlinear MPC

By default, nonlinear MPC controllers solve a nonlinear programming problem using thefminconfunction 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 timetk,nonlinear optimization problem uses the following decision variables:

  • Predicted state values from timetk+1totk+p. These values correspond to rows 2 throughp+1 of theXinput argument of your cost, and constraint functions, wherepis the prediction horizon.

  • Predicted manipulated variables from timetktotk+p-1. These values correspond to the manipulated variable columns in rows 1 throughpof theUinput argument of your cost, and constraint functions.

Therefore, the number of decision variablesNZis equal top(Nx+Nmv) + 1,Nxis the number of states,Nmvis 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 towarm startyour 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®,Nonlinear MPC Controllerblock is configured to use these trajectories as initial guesses by default. To use these trajectories as initial guesses at the command line:

  1. Return theoptoutput argument when callingnlmpcmove. Thisnlmpcmoveoptobject 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).

  2. Pass this object in as theoptionsinput argument tonlmpcmovefor the next control interval.

These command-line simulation steps are best practices, even if you do not specify any other run-time options.

ConfigurefminconOptions

By default, nonlinear MPC controllers optimize their control move using thefminconfunction from the Optimization Toolbox. When you first create your controller, theOptimization.SolverOptionsproperty of thenlmpc对象包含standardfminconoptions with the following nondefault settings:

  • Use theSQPalgorithm (SolverOptions.Algorithm = 'sqp')

  • Use objective function gradients (SolverOptions.SpecifyObjectiveGradient = 'true')

  • 使用约束梯度(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, setSolverOptions.MaxIter. For more information on the available solver options, seefmincon(Optimization Toolbox).

In general, you should not modify theSpecifyObjectiveGradientandSpecifyConstraintGradientsolver options, since doing so can significantly affect controller performance. For example, the constraint gradient matrices are sparse, and settingSpecifyConstraintGradientto false would cause the solver to calculate gradients that are known to be zero.

指定自定义解算器

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, seeOptimizing Tuberculosis Treatment Using Nonlinear MPC with a Custom Solver.

You can use the Nonlinear Programming solver developed byEmbotech AGto simulate and generate code for nonlinear MPC controllers. For more information, seeImplement MPC Controllers using Embotech FORCESPRO Solvers.

To configure yournlmpcobject to use your custom solver wrapper function, set itsOptimization.CustomSolverFcnproperty 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:

  • NZis the number of decision variables.

  • Mcineqis the number of linear inequality constraints.

  • Mceqis the number of linear equality constraints.

  • Ncineqis the number of nonlinear inequality constraints.

  • Nceqis 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:

  • z— Decision variables, specified as a vector of lengthNZ.

  • F— Cost, returned as a scalar.

  • G— Cost function gradients with respect to the decision variables, returned as a column vector of lengthNZ, where G ( i ) = F / z ( i ) .

z0 Input Initial guesses for decision variable values, specified as a vector of lengthNZ
A Input Linear inequality constraint array, specified as anMcineq-by-NZarray. Together,AandBdefine constraints of the form A z B .
B Input Linear inequality constraint vector, specified as a column vector of lengthMcineq. Together,AandBdefine constraints of the form A z B .
Aeq Input Linear equality constraint array, specified as anMceq-by-NZarray. Together,AeqandBeqdefine constraints of the form Aeq z = Beq .
Beq Input Linear equality constraint vector, specified as a column vector of lengthMceq. Together,AeqandBeqdefine constraints of the form Aeq z = Beq .
LB Input Lower bounds for decision variables, specified as a column vector of lengthNZ, where LB ( i ) z ( i ) .
UB Input Upper bounds for decision variables, specified as a column vector of lengthNZ, where z ( i ) UB ( i ) .
NLCON Input

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 lengthNZ.

  • cineq— Nonlinear inequality constraint values, returned as a column vector of lengthNcineq.

  • ceq——非线性等式约束values, returned as a column vector of lengthNceq.

  • Gineq— Nonlinear inequality constraint gradients with respect to the decision variables, returned as anNZ-by-Ncineqarray, where Gineq ( i , j ) = cineq ( j ) / z ( i ) .

  • Geq——非线性等式约束gradients with respect to the decision variables, returned as anNZ-by-Nceqarray, where Geq ( i , j ) = ceq ( j ) / z ( i ) .

zopt Output Optimal decision variable values, returned as a vector of lengthNZ.
cost Output Optimal cost, returned as a scalar.
flag Output

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 usingvalidateFcns.

See Also

|(Optimization Toolbox)

相关的话题