【数学建模】智能算法-遗传算法 | matlab | GA工具箱 |求解固定区间
·
一、例题:求解固定区间里目标函数的最大值
问题:maxf(x)=200*exp(-0.05x)*sinx
-2≤x≤2
clear all;
clc;
options=gaoptimset('generations',400,'StallGenLimit',300,'PlotFcns',@gaplotbestf)
[x1,f]=ga(@fun,1,options) %1是指目标函数的自变量个数
y1=200*exp(-0.05*x1)*sin(x1);
t=-2:0.0001:2
y=200*exp(-0.05*t).*sin(t)
figure(2)
plot(t,y)
text(x1,y1,'o','color','b');
text(x1,y1,['(',num2str(x1),',',num2str(y1),')'],'color','r');
%目标函数
function f=fun(x)
if (x<-2|x>2)
f=100
else
f=-200*exp(-0.05*x)*sin(x);
end
end
结果如下:

当然可以改变plotfcns的参数,画出其他的图片如下
clear all;
clc;
options=gaoptimset('generations',400,'StallGenLimit',300,'PlotFcns',...
{@gaplotbestf,@gaplotbestindiv,@gaplotexpectation,@gaplotstopping})
[x1,f]=ga(@fun,1,options) %1是指目标函数的自变量个数
y1=200*exp(-0.05*x1)*sin(x1);
t=-2:0.0001:2
y=200*exp(-0.05*t).*sin(t)
figure(2)
plot(t,y)
text(x1,y1,'o','color','b');
text(x1,y1,['(',num2str(x1),',',num2str(y1),')'],'color','r');
%目标函数
function f=fun(x)
if (x<-2|x>2)
f=100
else
f=-200*exp(-0.05*x)*sin(x);
end
end



最后找找到的最大值点:


DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)