优化变量为自变量x(1)和x(2);
约束为[-1,+1];
目标函数为三个函数值的最大值最小!
主函数:
clear all
fun = @max_f;%定义适度函数
nvars=2;%定义变量个数
A = [];
b = [];
Aeq = [];
beq = [];
lb = [-1 -1];
ub = [+1 +1];
%% Start with the default options
options = gaoptimset;
%% Modify options setting
options = gaoptimset(options,'Generations', 20);%定义代的数量
options = gaoptimset(options,'Display', 'off');
options = gaoptimset(options,'PlotFcns', { @gaplotbestf @gaplotbestindiv });%显示最优适度函数和最优个体
[x,fval,exitflag,output,population,score] = ...
ga(@max_f,nvars,[],[],[],[],lb,ub,[],[],options);
适应度函数:
function f=max_f(x)
y1=x(1)^2+x(2)^+2*x(1)+x(2)-4
y2=x(1)*x(2)+x(1)
y3=x(1)*x(2)^2+x(2)
y=[y1 y2 y3]
f=max(y);%三个函数的最大值
计算结果如下:
x =
-0.9998 -0.4142
fval =
-0.5856
exitflag =
0
output =
problemtype: 'boundconstraints'
rngstate: [1x1 struct]
generations: 20
funccount: 1050
message: 'Optimization terminated: maximum number of generations exceeded.'
maxconstraint: 0
来源:CSDN
作者:weixin_41639107
链接:https://blog.csdn.net/weixin_41639107/article/details/104166472