Main Content

setCustomSolver

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

Since R2021b

    Description

    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.

    example

    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));
    -->"PredictionHorizon" is empty. Assuming default 10.
    -->"ControlHorizon" is empty. Assuming default 2.
    -->"Weights.ManipulatedVariables" is empty. Assuming default 0.00000.
    -->"Weights.ManipulatedVariablesRate" is empty. Assuming default 0.10000.
    -->"Weights.OutputVariables" 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:
             OptimizationType: 'QP'
                       Solver: 'active-set'
                SolverOptions: [1x1 mpc.solver.options.qp.ActiveSet]
                 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:
             OptimizationType: 'QP'
                       Solver: 'active-set'
                SolverOptions: [1x1 mpc.solver.options.qp.ActiveSet]
                 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:
             OptimizationType: 'QP'
                       Solver: 'active-set'
                SolverOptions: [1x1 mpc.solver.options.qp.ActiveSet]
                 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.

    Version History

    Introduced in R2021b