主要内容

setCustomSolver

配置MPC对象使用QP求解器优化工具箱作为自定义求解器

    描述

    例子

    setCustomSolver (mpcobj, ' quadprog ')配置mpcobj使用quadprog作为自定义QP求解器,用于模拟和代码生成。具体来说,该语法在当前文件夹中生成文件mpcCustomSolver.m而且mpcCustomSolverCodeGen.m,在内部调用active-setquadprog(优化工具箱)解算器。然后它就凝固了mpcobj.Optimizer.CustomSolver而且mpcobj.Optimizer.CustomSolverCodeGen真正的

    例子

    setCustomSolver (mpcobj, '没有')mpcobj.Optimizer.CustomSolver而且mpcobj.Optimizer.CustomSolverCodeGen,从而恢复mpcobj返回使用中指定的内置算法mpcobj.Optimizer.Algorithm用于模拟和代码生成。

    例子

    全部折叠

    方法的使用setCustomSolver自动配置货币政策委员会对象来使用优化工具箱™quadprog功能为自定义MPC求解器模拟和代码生成。

    创建一个货币政策委员会对象。

    Mpcobj = mpc(tf(1,[2 1],0.1));
    ——>“PredictionHorizon”属性为空。假设默认为10。“ControlHorizon”属性为空。假设默认2。- - - >“权重。属性为空。假设默认值为0.00000。- - - >“权重。属性为空。假设默认为0.10000。 -->The "Weights.OutputVariables" property is empty. Assuming default 1.00000.

    默认情况下,控制器被设置为使用活动集求解器进行模拟和代码生成。

    mpcobj。优化器
    ans =带字段的结构:算法:'active-set' ActiveSetOptions: [1x1 struct] InteriorPointOptions: [1x1 struct] MixedIntegerOptions: [1x1 struct] MinOutputECR: 0 UseSuboptimalSolution: 0 CustomSolver: 0 CustomSolverCodeGen: 0

    配置mpcobj使用quadprog作为自定义求解器

    设置quadprog功能为自定义MPC求解器模拟和代码生成,调用setCustomSolver“quadprog”作为第二个论点。

    setCustomSolver (mpcobj“quadprog”

    该函数在当前文件夹中生成文件mpcCustomSolver.m而且mpcCustomSolverCodeGen.m.要显示当前文件夹中的MATLAB文件,请使用ls命令。

    ls* 00
    mpcCustomSolverCodeGen.mmpcCustomSolver.m

    显示的内容mpcCustomSolver.m,mpcCustomSolverCodeGen.m使用类型命令。两个文件都在内部调用quadprog,它被配置为使用活动集求解器,因为不支持其他算法。金宝app

    类型mpcCustomSolver
    function [x, status] = mpcCustomSolver(H, f, A, b, x0) % "mpcCustomSolver"允许使用优化工具箱%中的"quadprog"作为自定义QP求解器,具有线性MPC控制器用于模拟。指定解算算法和选项options = optimoptions('quadprog',' algorithm ','active-set');if code .target('MATLAB')选项。Display = 'none';在“quadprog”中使用-A和-b,因为MPC QP使用Ax>=b代替A_custom = -A;B_custom = -b;%确保黑森对称H = (H+H')/2;%%调用"quadprog" [x, ~, exitflag, output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);%%将退出标志转换为MPC“status”开关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
    类型mpcCustomSolverCodeGen.m
    function [x, status] = mpcCustomSolverCodeGen(H, f, A, b, x0) % "mpcCustomSolverCodeGen"允许使用最优化%工具箱中的"quadprog"作为自定义QP求解器,具有线性MPC控制器,用于代码生成。指定解算算法(必须是"active-set")和options options = optimoptions('quadprog',' algorithm ','active-set');if code .target('MATLAB')选项。Display = 'none';在“quadprog”中使用-A和-b,因为MPC QP使用Ax>=b代替A_custom = -A;B_custom = -b;%确保黑森对称H = (H+H')/2;%%调用"quadprog" [x, ~, exitflag, output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);%%将退出标志转换为MPC“status”开关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

    setCustomSolver函数也是集合mpcobj.Optimizer.CustomSolver而且mpcobj.Optimizer.CustomSolverCodeGen真正的,从而建立mpcobj对象在相关文件中使用自定义求解器进行模拟和代码生成。

    mpcobj。优化器
    ans =带字段的结构:算法:'active-set' ActiveSetOptions: [1x1 struct] InteriorPointOptions: [1x1 struct] MixedIntegerOptions: [1x1 struct] MinOutputECR: 0 UseSuboptimalSolution: 0 CustomSolver: 1 CustomSolverCodeGen: 1

    回复mpcobj使用内置求解器

    回复mpcobj回用一个内置的求解器,调用setCustomSolver函数与“没有”作为第二个论点。

    setCustomSolver (mpcobj“没有”

    这集mpcobj.Optimizer.CustomSolver而且mpcobj.Optimizer.CustomSolverCodeGen

    mpcobj。优化器
    ans =带字段的结构:算法:'active-set' ActiveSetOptions: [1x1 struct] InteriorPointOptions: [1x1 struct] MixedIntegerOptions: [1x1 struct] MinOutputECR: 0 UseSuboptimalSolution: 0 CustomSolver: 0 CustomSolverCodeGen: 0

    控制器现在将使用内置的active-set求解器进行模拟和代码生成。

    输入参数

    全部折叠

    MPC控制器,指定为MPC控制器对象。使用货币政策委员会命令,创建MPC控制器。

    版本历史

    R2021b中引入