Main Content

setCustomSolver

Configures an MPC object to use the QP solver from Optimization Toolbox as a custom solver

    Description

    example

    setCustomSolver(mpcobj,'quadprog') configures mpcobj to use quadprog from Optimization Toolbox™ as a custom QP solver for both simulation and code generation. Specifically, this syntax generates, in the current folder, the files mpcCustomSolver.m and mpcCustomSolverCodeGen.m, which internally call the active-set quadprog (Optimization Toolbox) solver. It then sets mpcobj.Optimizer.CustomSolver and mpcobj.Optimizer.CustomSolverCodeGen to true.

    example

    setCustomSolver(mpcobj,'none') sets mpcobj.Optimizer.CustomSolver and mpcobj.Optimizer.CustomSolverCodeGen to false, thereby reverting mpcobj back to use the built-in algorithm specified in mpcobj.Optimizer.Algorithm for both simulation and code generation.

    Examples

    collapse all

    This example shows how to use the setCustomSolver function to automatically configure an mpc object to use the Optimization Toolbox™ quadprog function as custom MPC solver for both simulation and code generation.

    Create an mpc object.

    mpcobj = mpc(tf(1,[2 1],0.1));
    -->The "PredictionHorizon" property of "mpc" object is empty. Trying PredictionHorizon = 10.
    -->The "ControlHorizon" property of the "mpc" object is empty. Assuming 2.
    -->The "Weights.ManipulatedVariables" property of "mpc" object is empty. Assuming default 0.00000.
    -->The "Weights.ManipulatedVariablesRate" property of "mpc" object is empty. Assuming default 0.10000.
    -->The "Weights.OutputVariables" property of "mpc" object is empty. Assuming default 1.00000.
    

    As a default, the controller is set to use the active-set solver for both simulation and code generation.

    mpcobj.Optimizer
    ans = struct with fields:
                    Algorithm: 'active-set'
             ActiveSetOptions: [1x1 struct]
         InteriorPointOptions: [1x1 struct]
          MixedIntegerOptions: [1x1 struct]
                 MinOutputECR: 0
        UseSuboptimalSolution: 0
                 CustomSolver: 0
          CustomSolverCodeGen: 0
    
    

    Configure mpcobj to Use quadprog as Custom Solver

    To set the quadprog function as custom MPC solver for both simulation and code generation, call setCustomSolver with 'quadprog' as second argument.

    setCustomSolver(mpcobj,'quadprog')

    The function generates, in the current folder, the files mpcCustomSolver.m and mpcCustomSolverCodeGen.m. To display the MATLAB files in the current folder, use the ls command.

    ls *.m
    mpcCustomSolverCodeGen.m  mpcCustomSolver.m
    

    To display the content of mpcCustomSolver.m, and mpcCustomSolverCodeGen.m use the type command. Both files internally call quadprog, which is configured to use the active-set solver, as other algorithms are not supported.

    type mpcCustomSolver
    function [x, status] = mpcCustomSolver(H, f, A, b, x0)
    % "mpcCustomSolver" enables using "quadprog" from Optmization Toolbox 
    % as a custom QP solver with linear MPC controller for simulation.
    
    %% Specify solver algorithm and options
    options = optimoptions('quadprog','Algorithm','active-set');
    if coder.target('MATLAB')
        options.Display = 'none';  
    end
    %% Process solver inputs
    % Use -A and -b in "quadprog" because MPC QP uses Ax>=b instead
    A_custom = -A;
    b_custom = -b;
    % ensure Hessian is symmetric
    H = (H+H')/2; 
    %% Call "quadprog"
    [x, ~, exitflag, output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);
    %% Converts exit flag to MPC "status"
    switch exitflag
        case 1
            status = output.iterations;
        case 0
            status = 0;
        case -2
            status = -1;
        otherwise
            status = -2;
    end
    %% If "quadprog" fails to find a solution, set x to the initial guess
    if status <= 0
        x = x0;
    end
    
    type mpcCustomSolverCodeGen.m
    function [x, status] = mpcCustomSolverCodeGen(H, f, A, b, x0)
    % "mpcCustomSolverCodeGen" enables using "quadprog" from Optmization 
    % Toolbox as a custom QP solver with linear MPC controller for code generation.
    
    %#codegen
    %% Specify solver algorithm (must be "active-set") and options
    options = optimoptions('quadprog','Algorithm','active-set');
    if coder.target('MATLAB')
        options.Display = 'none';  
    end
    %% Process solver inputs
    % Use -A and -b in "quadprog" because MPC QP uses Ax>=b instead
    A_custom = -A;
    b_custom = -b;
    % ensure Hessian is symmetric
    H = (H+H')/2; 
    %% Call "quadprog"
    [x, ~, exitflag, output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);
    %% Converts exit flag to MPC "status"
    switch exitflag
        case 1
            status = output.iterations;
        case 0
            status = 0;
        case -2
            status = -1;
        otherwise
            status = -2;
    end
    %% If "quadprog" fails to find a solution, set x to the initial guess
    if status <= 0
        x = x0;
    end
    

    The setCustomSolver function also sets mpcobj.Optimizer.CustomSolver and mpcobj.Optimizer.CustomSolverCodeGen to true, thereby setting up the mpcobj object to use the custom solver in the related files for simulation and code generation.

    mpcobj.Optimizer
    ans = struct with fields:
                    Algorithm: 'active-set'
             ActiveSetOptions: [1x1 struct]
         InteriorPointOptions: [1x1 struct]
          MixedIntegerOptions: [1x1 struct]
                 MinOutputECR: 0
        UseSuboptimalSolution: 0
                 CustomSolver: 1
          CustomSolverCodeGen: 1
    
    

    Revert mpcobj to Use a Built-In Solver

    To revert mpcobj back to use a built in solver, call setCustomSolver function with 'none' as second argument.

    setCustomSolver(mpcobj,'none')

    This sets mpcobj.Optimizer.CustomSolver and mpcobj.Optimizer.CustomSolverCodeGen to false.

    mpcobj.Optimizer
    ans = struct with fields:
                    Algorithm: 'active-set'
             ActiveSetOptions: [1x1 struct]
         InteriorPointOptions: [1x1 struct]
          MixedIntegerOptions: [1x1 struct]
                 MinOutputECR: 0
        UseSuboptimalSolution: 0
                 CustomSolver: 0
          CustomSolverCodeGen: 0
    
    

    The controller will now use the active-set built in solver for both simulation and code generation.

    Input Arguments

    collapse all

    MPC controller, specified as an MPC controller object. Use the mpc command to create the MPC controller.

    Introduced in R2021b