作业帮 > 综合 > 作业

这是用遗传算法求解matlab多峰函数最大值的程序 看了一天 还是很混乱

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/30 22:08:36
这是用遗传算法求解matlab多峰函数最大值的程序 看了一天 还是很混乱
%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;
%Parameters
Size=80;
G=100;
CodeL=10;
umax=2.048;
umin=-2.048;
E=round(rand(Size,2*CodeL)); %Initial Code
GG1=zeros(1,G);
Y=zeros(2,G);
%Main Program
for k=1:1:G
time(k)=k;
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;
%Uncoding
m1=m(1:1:CodeL);
for i=1:1:CodeL
y1=y1+m1(i)*2^(i-1);
end
x1=(umax-umin)*y1/1023+umin;
Y(1,k)=x1;
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
x2=(umax-umin)*y2/1023+umin;
Y(2,k)=x2;
%F(s)=100*(x1^2-x2)^2+(1-x1)^2;%这个就是所对应的适应值函数
F(s)=10*sin(sqrt(x1.^2+x2.^2))./sqrt(1+x1.^2+x2.^2);
end
Ji=1./F;
%
%pm=0.001;
%pm=0.001-[1:1:Size]*(0.001)/Size; %Bigger fi,smaller Pm
%pm=0.0; %No mutation
pm=0.1; %Big mutation
for i=1:1:Size
for j=1:1:2*CodeL
temp=rand;
if pm>temp %Mutation Condition
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0;
end
end
end
end
%Guarantee TempPop(30,:) is the code belong to the best individual(max(fi))
TempE(Size,:)=BestS;
E=TempE;
end
Max_Value=Bestfi
BestS
x1
x2
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
figure(2);
plot(time,bfi);
xlabel('times');ylabel('Best F');
首先第一步你要懂遗传算法的各个部分,然后对照各个部分的算法,看代码.
希望这个回答另你满意.