在 surrogateopt
形式和其他求解器形式之间转换非线性约束
为什么要转换约束形式?
若要针对具有非线性不等式约束的问题尝试包括 surrogateopt
在内的各种求解器,必须在 surrogateopt
所需的形式和其他求解器所需的形式之间进行转换。
从 surrogateopt
结构体形式转换为其他求解器
surrogateopt
的目标函数 objconstr(x)
返回一个结构体。Fval
字段包含目标函数值,一个标量。Ineq
字段包含约束函数值向量。求解器尝试使 Ineq
字段中的所有值小于或等于零。正值表示约束违反。
其他求解器期望目标函数返回标量值,而不是结构体。其他求解器也期望非线性约束函数返回两个输出,c(x)
和 ceq(x)
,而不是包含 c(x)
的结构体。
要将 surrogateopt
函数转换为 objconstr(x)
以供其他求解器使用:
将目标函数设置为
@(x)objconstr(x).Fval
。将非线性约束函数设置为
@(x)deal(objconstr(x).Ineq,[])
。
例如,
function ff = objconstr(x) ff.Fval = norm(x)^2; ff.Ineq = norm(x - [5,8])^2 - 25; end
要使用 objconstr
解决约束最小化问题,请调用 surrogateopt
。
lb = [-10,-20]; ub = [20,10]; sol = surrogateopt(@objconstr,lb,ub)
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.MaxFunctionEvaluations'. sol = 2.3504 3.7598
要使用 fmincon
解决相同的问题,请将目标和约束拆分为单独的函数。使用 deal
函数非线性等式约束作为 []
包含进去。
objfcn = @(x)objconstr(x).Fval; nlcon = @(x)deal(objconstr(x).Ineq,[]);
使用目标函数 objfcn
和非线性约束函数 nlcon
调用 fmincon
。
[solf,fvalf,eflag,output] = ...
fmincon(objfcn,[0,0],[],[],[],[],lb,ub,nlcon)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. solf = 2.3500 3.7600 fvalf = 19.6602 eflag = 1 output = struct with fields: iterations: 7 funcCount: 24 constrviolation: 0 stepsize: 2.0397e-05 algorithm: 'interior-point' firstorderopt: 4.8151e-06 cgiterations: 0 message: 'Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative first-order optimality measure, 6.403047e-07,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.' bestfeasible: [1×1 struct]
您还可以使用 patternsearch
或 ga
通过相同的转换来解决问题。
从其他求解器转换为 surrogateopt
结构体形式
如果您遇到以其他求解器形式编写的问题,请使用 packfcn
函数将目标和非线性约束转换为 surrogateopt
的结构体形式。如果目标函数是函数句柄 @obj
,非线性约束函数是 @nlconst
,则对 surrogateopt
使用目标函数 objconstr
。
objconstr = packfcn(@obj,@nlconst);
在这个例子中,目标函数是罗森布洛克函数。
ros = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
指定约束函数将解限制在以点 [1/3,1/3] 为中心、半径为 1/3 的圆盘内。
function [c,ceq] = circlecon(x)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2;
ceq = [];
在每个分量上设置-2 和 2 的边界。
lb = [-2,-2]; ub = [2,2];
从 [0,0] 开始,使用 patternsearch
解决问题。
x0 = [0,0]; x = patternsearch(ros,x0,[],[],[],[],lb,ub,@circlecon)
Optimization finished: mesh size less than options.MeshTolerance and constraint violation is less than options.ConstraintTolerance. x = 0.6523 0.4258
将问题转换为通过 surrogateopt
解。
objconstr = packfcn(ros,@circlecon); xs = surrogateopt(objconstr,lb,ub)
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.MaxFunctionEvaluations'. xs = 0.6543 0.4286