clear all;
close all;
clc;
%变量初始化
populationSize=80; %群体大小
generationSize=800; %进化代数
codeLength=35; %编码长度
children=zeros(populationSize,7); %存储子代分量及适应度
pr=0.15; %复制概率
pm=0.1; %变异概率
pc=0.60; %交叉概率
E=round(rand(populationSize,codeLength)); %随机产生二进制串
%主函数
for k=1:1:generationSize
for s=1:1:populationSize
x=E(s,:);
temp=zeros(1,7);%临时保存子代各分量的十进制值
%编码
for i=1:35
if i<=6
temp(1)=temp(1)+x(i)*2^(i-1);
elseif i>=7 && i<=13
temp(2)=temp(2)+x(i)*2^(i-6-1);
elseif i>=14 && i<=19
temp(3)=temp(3)+x(i)*2^(i-13-1);
elseif i>=20 && i<=25
temp(4)=temp(4)+x(i)*2^(i-19-1);
elseif i>=26 && i<=30
temp(5)=temp(5)+x(i)*2^(i-25-1);
elseif i>=31 && i<=35
temp(6)=temp(6)+x(i)*2^(i-30-1);
end
end
children(s,1)=round(60*temp(1)/(2^6-1));
children(s,2)=round(70*temp(2)/(2^7-1));
children(s,3)=round(60*temp(3)/(2^6-1));
children(s,4)=round(50*temp(4)/(2^6-1));
children(s,5)=round(20*temp(5)/(2^5-1));
children(s,6)=round(30*temp(6)/(2^5-1));
children(s,7)=round(children(s,1)+children(s,2)+children(s,3)+children(s,4)+children(s,5)+children(s,6));
while children(s,1)+children(s,6)<60||children(s,1)+children(s,2)<70||children(s,2)+children(s,3)<60||...
children(s,3)+children(s,4)<50||children(s,4)+children(s,5)<20||children(s,5)+children(s,6)<30
E(s,:)=round(rand(1,codeLength));
x=E(s,:);
temp=zeros(1,7);
for i=1:35
if i<=6
temp(1)=temp(1)+x(i)*2^(i-1);
elseif i>=7 && i<=13
temp(2)=temp(2)+x(i)*2^(i-6-1);
elseif i>=14 && i<=19
temp(3)=temp(3)+x(i)*2^(i-13-1);
elseif i>=20 && i<=25
temp(4)=temp(4)+x(i)*2^(i-19-1);
elseif i>=26 && i<=30
temp(5)=temp(5)+x(i)*2^(i-25-1);
elseif i>=31 && i<=35
temp(6)=temp(6)+x(i)*2^(i-30-1);
end
end
children(s,1)=round(60*temp(1)/(2^6-1));
children(s,2)=round(70*temp(2)/(2^7-1));
children(s,3)=round(60*temp(3)/(2^6-1));
children(s,4)=round(50*temp(4)/(2^6-1));
children(s,5)=round(20*temp(5)/(2^5-1));
children(s,6)=round(30*temp(6)/(2^5-1));
children(s,7)=round(children(s,1)+children(s,2)+children(s,3)+children(s,4)+children(s,5)+children(s,6));
end
end
% Step 1 : 确定适应度函数
fitness=1./children(:,7);
[orderFitness,indexFitness]=sort(fitness); %orderFitness按行从小到大排序的新矩阵,indexFitness元素位置构成的新矩阵
bestFitness=orderFitness(populationSize); %bestFitness=max(fitness)
bestS=E(indexFitness(populationSize),:); %bestS 精英保留
%Step 2 : 复制操作
sumFitness=sum(fitness);
fitnessSize=(orderFitness/sumFitness)*populationSize;
fitnessSelect=floor(fitnessSize); %朝负无穷大方向取整
kk=1;
for i=1:1:populationSize
for j=1:1:fitnessSelect(i) %Select and Reproduce
TempE(kk,:)=E(indexFitness(i),:);
kk=kk+1; %kk is used to reproduce
end
end
%Step 3 : 交叉操作
n=ceil(35*rand); %朝正无穷大方向取整
for i=1:2:(populationSize-1)
temp=rand;
if pc>temp %Crossover Condition
for j=n:1:35
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
end
TempE(populationSize,:)=bestS;
E=TempE;
%Step 4: 变异操作
for i=1:1:populationSize
for j=1:1:35
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
TempE(populationSize,:)=bestS;
E=TempE;
disp(['第',num2str(k),'代:',num2str(children(populationSize,:))]);
end
disp(['二进制编码:',num2str(bestS)]);
disp(['最优选择:',num2str(children(populationSize,:))]);