Main Content

非线性等式和不等式约束

此示例说明如何求解包含非线性约束的优化问题。通过编写同时计算等式和不等式约束值的函数来包含非线性约束。非线性约束函数具有以下语法

[c,ceq] = nonlinconstr(x)

函数 c(x) 表示约束 c(x) <= 0。函数 ceq(x) 表示约束 ceq(x) = 0

注意:您必须让非线性约束函数同时返回 c(x)ceq(x),即使您只有一种类型的非线性约束。如果某种约束不存在,请让函数针对该约束返回 []

非线性约束

假设您有非线性等式约束

x12+x2=1

和非线性不等式约束

x1x2-10.

将这些约束重写为

x12+x2-1=0-x1x2-100.

此示例末尾confuneq 辅助函数以正确的语法实现这些不等式。

目标函数

求解问题

minxf(x)=ex1(4x12+2x22+4x1x2+2x2+1)

需满足以下约束。此示例末尾objfun 辅助函数实现此目标函数。

求解问题

通过调用 fmincon 求解器求解问题。此求解器需要一个初始点;使用点 x0 = [-1,-1]

x0 = [-1,-1];

该问题没有边界或线性约束,因此将这些输入设置为 []

A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];

调用求解器。

[x,fval] = fmincon(@objfun,x0,A,b,Aeq,beq,lb,ub,@confuneq)
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.
x = 1×2

   -0.7529    0.4332

fval = 1.5093

求解器报告在解处满足约束。检查在解处的非线性约束。

[c,ceq] = confuneq(x)
c = -9.6739
ceq = 2.0668e-12

根据要求,c 小于 0。ceq 在默认约束容差 1e-6 内等于 0。

辅助函数

以下代码创建 confuneq 辅助函数。

function [c,ceq] = confuneq(x)
% Nonlinear inequality constraints
c = -x(1)*x(2) - 10;
% Nonlinear equality constraints
ceq = x(1)^2 + x(2) - 1;
end

以下代码创建 objfun 辅助函数。

function f = objfun(x)
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
end

相关主题