文章首发于我的个人博客:欢迎大佬们来逛逛
算法流程:
min x ∑ i = 1 m w i F i ( x ) s.t. g ( x ) ⩽ 0 h ( x ) = 0 \begin{array}{ll}\min _{x} & \sum_{i=1}^{m} {w_{i} F_{i}(x)} \\\\\text { s.t. } & g(x) \leqslant 0 \\\\& h(x)=0\end{array} minx s.t. ∑i=1mwiFi(x)g(x)⩽0h(x)=0
clc;clear;
%% 指定初始解
x0 = zeros(3,1);
% <线性>不等约束
A = [2,1,3];
B = [6];
% <线性>等式约束
Aeq = [];
Beq = [];
% 变量上下限
LB = zeros(3,1);
UB = 1*ones(3,1);
%% 乘以权重,化为单目标求最优值
% 有几个目标函数,就写几个权重,然后依次相乘再相加
W1 =0.5;
W2 = 0.5;
fun = @(x) (-x(1)^2+x(2)^2-x(2)*x(3)^2)*W1 + (2*x(1)^2-x(2)^3+2*x(2)*x(3))*W2;
%% 取得非线性不等式约束函数
nonlcon = @noLinearLimited;
%% 求解fun单目标最优值
[x,fval] = fmincon(fun,x0,A,B,Aeq,Beq,LB,UB,nonlcon);
objstr=['目标函数最优值:',num2str(-fval)];
disp(objstr)
for i=1:length(x)
xstr=['x',num2str(i),'的值为:',num2str(x(i))];
disp(xstr)
end
%% 非线性不等式约束的表达式,如果有多个,则在C后面加; 补充即可
function [C,Ceq] = noLinearLimited(x)
C = [x(1)^2+x(1)*x(2)+x(2)*x(3)-x(2)-6];
Ceq = [];
end
fgoalattain
min γ , x γ s.t. F i ( x ) − w i γ ⩽ F i ∗ for i = 1 , … , m g ( x ) ⩽ 0 h ( x ) = 0 \begin{array}{ll}\min _{\gamma, x} \gamma \\\\\text { s.t. } & F_{i}(x)-w_{i} \gamma \leqslant F_{i}^{*} \quad \text { for } i=1, \ldots, m \\\\& g(x) \leqslant 0 \\\\& h(x)=0\end{array} minγ,xγ s.t. Fi(x)−wiγ⩽Fi∗ for i=1,…,mg(x)⩽0h(x)=0
clc;clear;
%% 指定初始解
x0 = zeros(3,1);
% <线性>不等约束
A = [2,1,3];
B = [6];
% <线性>等式约束
Aeq = [];
Beq = [];
% 变量上下限
LB = zeros(3,1);
UB = 1*ones(3,1);
%% 分两个(n个)非线性目标的最优值
% 两个目标函数
fun1 = @(x) -x(1)^2+x(2)^2-x(2)*x(3);
fun2 = @(x) 2*x(1)^2-x(2)^3+2*x(2)*x(3);
%% fun1的最优化:
nonlcon = @noLinearLimited;
[x1,fval1] = fmincon(fun1,x0,A,B,Aeq,Beq,LB,UB,nonlcon);
objstr=['目标函数最优值:',num2str(fval1)];
disp(objstr)
for i=1:length(x1)
xstr=['x',num2str(i),'的值为:',num2str(x1(i))];
disp(xstr)
end
% fun2的最优化:
[x2,fval2] = fmincon(fun2,x0,A,B,Aeq,Beq,LB,UB,nonlcon);
objstr=['目标函数最优值:',num2str(fval2)];
disp(objstr)
for i=1:length(x2)
xstr=['x',num2str(i),'的值为:',num2str(x2(i))];
disp(xstr)
end
%% 多目标优化
goal = [fval1,fval2]; % 目标是接近于单目标的最优值
func = @(x) [-x(1)^2+x(2)^2-x(2)*x(3);2*x(1)^2-x(2)^3+2*x(2)*x(3)];
W = [1,1];% 自己赋值权重,两个(n个)非线性
[x,fval] = fgoalattain(func,x0,goal,W,A,B,Aeq,Beq,LB,UB,nonlcon);
disp('在两个目标的优化结果为')
disp(func(x))
for i=1:length(x)
xstr=['x',num2str(i),'的值为:',num2str(x(i))];
disp(xstr)
end
%% 非线性不等式约束的表达式,如果有多个,则在C后面加; 补充即可
function [C,Ceq] = noLinearLimited(x)
C = [x(1)^2+x(1)*x(2)+x(2)*x(3)-x(2)-6];
Ceq = [];
end