目标函数为:
function y = Obj_Fun1(x)
%UNTITLED2 此处显示有关此函数的摘要
% 此处显示详细说明
y(1) = 0.5*(x(1).^2 + x(2).^2) + sin(x(1).^2 + x(2).^2);
y(2) = (3*x(1) - 2*x(2) + 4).^2/8 + (x(1) - x(2) + 1).^2/27 + 15;
y(3) = 1/(x(1).^2 + x(2).^2 + 1) - 1.1*exp(-(x(1).^2 + x(2).^2));
end
非线性约束条件为:
function [c,ceq] = Con_Fun(x)
%UNTITLED3 此处显示有关此函数的摘要
% 此处显示详细说明
c(1) = x(1)*x(2);
c(2) = x(1) + x(2)^3;
ceq = [];
end
主函数:
clc
clear
%% 模型设置
% 变量个数、约束条件设置
nvars = 2; % 变量个数
% 约束条件形式1:下限与上限(若无取空数组[])
% lb<= X <= ub
lb = [-3,-3];
ub = [3,3];
% 约束条件形式2:线性不等式约束(若无取空数组[])
% A*X <= b
A = [];
b = [];
% 约束条件形式3:线性等式约束(若无取空数组[])
% Aeq*X == beq
Aeq = [];
beq = [];
% 约束条件形式4:非线性约束(若无取空数组[])
% function [c,ceq] = Con_Fun(x)
% c = ... % Compute nonlinear inequalities at x; c <= 0;
% ceq = ... % Compute nonlinear equalities at x; ceq = 0;
% nonlcon = @Con_Fun;
nonlcon = @Con_Fun;
%% 求解器设置
options = gaoptimset();
options.ParetoFraction = 0.3; % 最优个体系数
options.PopulationSize = 200; % 种群大小
options.Generations = 200; % 最大进化代数
options.StallGenLimit = 200; % 停止代数
options.TolFun = 1e-10; % 适应度函数偏差
options.HybridFcn = @fgoalattain; % GA优化后获得初始值代入标准优化器
options.PlotFcns = @gaplotpareto; % GA优化后获得初始值代入标准优化器
%% 主求解
fitnessfcn = @Obj_Fun1; % 适应度函数的函数句柄
% 求解
tic
[x,fval] = gamultiobj(fitnessfcn,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options);
toc
% 画图
figure(1)
plot3(fval(:,1),fval(:,2),fval(:,3),'o')
grid on
由于是三目标优化,画图的时候将gaplotpareto函数修改为objectivesToPlot = [1 2 3];