yichuansuanfa.txt
来自「这是一个关于遗传算法的MATLAB程序,用于求某种函数的极值」· 文本 代码 · 共 119 行
TXT
119 行
%Generic Alogorithm 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));
%Main Program
for k=1:1:G
time(k)=k;
for s=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;
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;
F(s)=100*(x1^2-x2)^2+(1-x1)^2;
end
Ji=1./F;
%**********Step 1 :Evaluate BestJ*******
BestJ(k)=min(Ji);
fi=F;
[Oderfi,Indexfi]=sort(fi);
Bestfi=Oderfi(Size);
BestS=E(Indexfi(Size),:);
bfi(k)=Bestfi;
%******Step 2 : Select and Reproduct Operation*******
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=floor(fi_Size);
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i)
TempE(kk,:)=E(Indexfi(i),:);
kk=kk+1;
end
end
%*******Step 3 : Crossover Operation********
pc=0.60;
n=ceil(20*rand);
for i=1:2:(Size-1)
temp=rand;
if pc>temp
for j=n:1:20
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
end
TempE(Size,:)=BestS;
E=TempE;
%**********step 4 :Mutation Operation*******
%pm=0.001;
%pm=0.001-[1:1:Size]*(0.001)/Size;
%pm=0.0;
pm=0.1;
for i=1:1:Size
for j=1:1:2*Codel
temp=rand;
if pm>temp
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0;
end
end
end
end
%Guarantee TeepPop(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);
title('目标函数J的优化过程')
xlabel('Times');
ylabel('Best J');
grid on;
figure(2);
plot(time,bfi);
title('适应度函数F的优化过程')
xlabel('times');
ylabel('Best F');
grid on;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?