📄 genetic.m
字号:
%genetic.m
clear all;
close all;
tic
Size=40;
G=50;
CodeL1=11;CodeL2=11;CodeL3=9;CodeL4=9;CodeL5=14; %54
umax1=5; %定义各参数跨度x0=(x,y,tx,ty,z)=(±5,±5,±1,±1,50)
umin1=-5;
umax2=5;
umin2=-5;
umax3=1;
umin3=-1;
umax4=1;
umin4=-1;
umax5=50;
umin5=0;
k=1;
E=round(rand(Size,CodeL1+CodeL2+CodeL3+CodeL4+CodeL5));
%main program
for k=1:1:G
time(k)=k;
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;y3=0;y4=0;y5=0;
%uncoding
%(x,y,tx,ty,z)对应在个体中的位置为(a1-a14,a15-a28,a29-a42,a43-a56,a57-a70)
m1=m(1:1:CodeL1);
for i=1:1:CodeL1
y1=y1+m1(i)*2^(i-1);
end
x1=(umax1-umin1)*y1/1023+umin1;
m2=m(CodeL1+1:1:CodeL1+CodeL2);
for i=1:1:CodeL2
y2=y2+m2(i)*2^(i-1);
end
x2=(umax1-umin1)*y2/1023+umin1;
m3=m(CodeL1+CodeL2+1:1:CodeL1+CodeL2+CodeL3);
for i=1:1:CodeL3
y3=y3+m3(i)*2^(i-1);
end
x3=(umax2-umin2)*y3/1023+umin2;
m4=m(CodeL1+CodeL2+CodeL3+1:1:CodeL1+CodeL2+CodeL3+CodeL4);
for i=1:1:CodeL4
y4=y4+m4(i)*2^(i-1);
end
x4=(umax2-umin2)*y4/1023+umin2;
m5=m(CodeL1+CodeL2+CodeL3+CodeL4+1:1:CodeL1+CodeL2+CodeL3+CodeL4+CodeL5);
for i=1:1:CodeL5
y5=y5+m5(i)*2^(i-1);
end
x5=(umax3-umin3)*y5/1023+umin3;
wavelength = 1.31;
w0x = 4.5;
w0y = 4.5;
wf0 = 4.6;
thetax=pi*x3/180;thetay=pi*x4/180;
w0xz=sqrt(w0x^2*(1+(wavelength*x5./(pi*w0x^2)).^2));
w0yz=sqrt(w0y^2*(1+(wavelength*x5./(pi*w0y^2)).^2));
kx=4*w0x^2*wf0^2./((w0x^2+wf0^2)^2+wavelength^2*x5.^2/pi^2);
ky=4*w0y^2*wf0^2./((w0y^2+wf0^2)^2+wavelength^2*x5.^2/pi^2);
intax=kx.*exp(-kx.*(x1.^2/2*(1/w0x^2+1/wf0^2)+pi^2*thetax.^2.*(w0xz.^2+wf0^2)./(2*wavelength^2)-x1.*thetax.*x5/w0x^2));
intay=ky.*exp(-ky.*(x2.^2/2*(1/w0y^2+1/wf0^2)+pi^2*thetay.^2.*(w0yz.^2+wf0^2)./(2*wavelength^2)-x2.*thetay.*x5/w0y^2));
F(s)=intax.*intay;
end
Ji=1./F;
BestJ(k)=min(Ji);
fi=F; %fitness function
[Oderfi,Indexfi]=sort(fi); %arranging fi small to bigger
Bestfi=Oderfi(Size); %let bestfi=max(fi)
BestS=E(Indexfi(Size),:); %let bests=e(m),m is the indexfi belong to max(fi)
bfi(k)=Bestfi;
%select and reproduct operation
fi_sum=sum(fi); %sum of fitness
fi_Size=(Oderfi/fi_sum)*Size; %Fitness Assignment
fi_S=floor(fi_Size); %selecting bigger fi value
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i) %select an reproduce
TempE(kk,:)=E(Indexfi(i),:);
kk=kk+1; %kk is used to reproduce
end
end
%crossover operation
pc=0.7;
n=ceil(54*rand); %select >=20*rand's first integer(整数)
for i=1:2:(Size-1)
temp=rand;
if pc>temp %crossover condition
for j=n:1:54
TempE(i,j)=E(i+1,j); %single point crossovering
TempE(i+1,j)=E(i,j);
end
end
end
TempE(Size,:)=BestS;
E=TempE;
%mutation operation
%pm=0.001;
%pm=0.001-[1:1:Size]*(0.001)/Size;%bigger fi ,smaller pm
%pm=0; %no mutation
pm=0.1; %big mutation
for i=1:1:Size
for j=1:1:54
temp=rand;
if pm>temp %mutation conditon
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;
k=k+1;
end
toc
T=toc
Max_Value=Bestfi
[x1 x2 x3 x4 x5]
%k
%gen=time
figure(1);
plot(time,bfi);grid on;
xlabel('gen');ylabel('Bestfi')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -