您的位置:首页 > 编程语言 > MATLAB

matlab----GA遗传算法

2018-02-03 21:32 756 查看
1.GA的调用:







2.例:当N = 10时



函数为:

function f = lbw(x)
if x(1)>30 || x(1) <-30 || x(2)>30 || x(2) <-30 || x(3)>30 || x(3) <-30 || x(4)>30 || x(4) <-30 || x(5)>30 || x(5) <-30 || x(6)>30 || x(6) <-30 || x(7)>30 || x(7) <-30 || x(8)>30 || x(8) <-30 || x(9)>30 || x(9) <-30 || x(10)>30 || x(10) <-30
f = 300;
else
s1 = 0;
s2 = 0;
for i = 1 : 10
s1 = s1 + x(i)^2;
s2 = s2 + cos(2 * pi * x(i));
end
f = -2 * pi *exp(-0.2 * sqrt(1/10 * s1)) - exp(1/10 * s2) + 2 * pi;
end


.m为:

%调用GA遗传算法
%Generations : 超过800代时停止; StallGenLimit : 超过连续代数300不进化时停止;  PlotFcns :绘图函数
options = gaoptimset('Generations',800,'StallGenLimit',300,'PlotFcns',@gaplotbestf);

%ga(待求函数,未知变量,选择): 求最小值
[x,f] = ga(@lbw,10,options)


最终结果:



3.GA在求解多约束非线性规划问题

注:Lingo也为首选方法; Matlab优化工具箱求解精度不够,穷举法效率太低

3.1:例



函数为:

%子函数:适应度函数同时也是目标函数,
function f=ch14_2f(x)
g1=1.5+x(1)*x(2)-x(1)-x(2);
g2=-x(1)*x(2);
if(g1>0|g2>10)
f=100;
else
f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
end

.m为:

%主程序:本程序采用遗传算法接力进化,
%将上次进化结束后得到的最终种群作为下次输入的初始种群
clc;
clear all;
%进化的代数
T=100;
optionsOrigin=gaoptimset('Generations',T/2);
[x,fval,reason,output,finnal_pop]=ga(@ch14_2f,2,optionsOrigin);
%进行第二次接力进化
options1=gaoptimset('Generations',T/2,'InitialPopulation',finnal_pop,...
'PlotFcns',@gaplotbestf);
[x,fval,reason,output,finnal_pop]=ga(@ch14_2f,2,options1);
Bestx=x
BestFval=fval

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: