www.pudn.com > GA.rar > A6A7A8.m, change:2015-09-24,size:3198b


clear all; 
popsize=1000; 
Generationmax=300; 
pcrossover=0.9; 
pmutation=0.0001; 
i=1; 
 
t1=zeros(popsize,1); 
t2=zeros(popsize,1); 
t3=zeros(popsize,1); 
t4=zeros(popsize,1); 
t5=zeros(popsize,1); 
 
%population=round(rand(popsize,40));%产生初始种群 
 
while(1) 
    temp=round(rand(1,40)); 
    t1(i,1)=transform2to10(temp(1,1:8)); 
    t2(i,1)=transform2to10(temp(1,9:16)); 
    t3(i,1)=transform2to10(temp(1,17:24)); 
    t4(i,1)=transform2to10(temp(1,25:32)); 
    t5(i,1)=transform2to10(temp(1,33:40)); 
    if (t1(i,1)>0&&t1(i,1)<t2(i,1)&&t2(i,1)+1<t3(i,1)&&t3(i,1)<t4(i,1)&&t4(i,1)<t5(i,1)&&t5(i,1)+1<220) 
        population(i,:)=temp;%产生初始种群 
        i=i+1; 
    end 
    if i==popsize+1 
        break 
    end 
    t=[t1,t2,t3,t4,t5]; 
end 
 
[Fitvalue,cumsump,VV,SS,AA,FF,WW]=fitnessfun(population); 
Generation=1; 
global BitLength; 
 
%% 
%{ 
    [fmin,nmin]=min(Fitvalue);%fmin,nmin分别为当前代最优的那条染色体的适应度值和编号 
    T1(Generation,1)=transform2to10(population(nmin,1:8)); 
    T2(Generation,1)=transform2to10(population(nmin,9:16)); 
    T3(Generation,1)=transform2to10(population(nmin,17:24)); 
    T4(Generation,1)=transform2to10(population(nmin,25:32)); 
    T5(Generation,1)=transform2to10(population(nmin,33:40)); 
    Vgather(:,Generation)=VV(:,nmin); 
    fmean=mean(Fitvalue); 
    ymin(Generation,1)=fmin;%ymin为各代最小值的集合 
    ymean(Generation,1)=fmean; 
    [MIN,l]=min(abs(ymin));%l为最好的代数 
    x=Vgather(:,l); 
    plot(x'); 
%} 
%% 
allpopulation=cell(Generationmax,1); 
 
while Generation<Generationmax+1 
     
    for j=1:2:popsize  
      %选择操作 
      seln=selection(population,cumsump); 
       
      %交叉操作 
      scro=crossover(population,seln,pcrossover); 
      scnew(j,:)=scro(1,:); 
      scnew(j+1,:)=scro(2,:); 
       
      %变异操作 
      smnew(j,:)=mutation(scnew(j,:),pmutation); 
      smnew(j+1,:)=mutation(scnew(j+1,:),pmutation); 
    end 
    allpopulation{Generation,1}=population; 
    %popgather(Generation,:,:)=population;把所有的种群放在一个数组里 
    population=smnew;  %产生了新的种群 
     
    %计算新种群的适应度    
    [Fitvalue,cumsump,VV,SS,AA,FF,WW]=fitnessfun(population); 
     
    %记录当前代最好的适应度和平均适应度 
 
    [fmin,nmin]=min(Fitvalue);%fmin,nmin分别为当前代最优的那条染色体的适应度值和编号 
    T1(Generation,1)=transform2to10(population(nmin,1:8)); 
    T2(Generation,1)=transform2to10(population(nmin,9:16)); 
    T3(Generation,1)=transform2to10(population(nmin,17:24)); 
    T4(Generation,1)=transform2to10(population(nmin,25:32)); 
    T5(Generation,1)=transform2to10(population(nmin,33:40)); 
    Vgather(:,Generation)=VV(:,nmin); 
    Sgather(:,Generation)=SS(:,nmin); 
    Agather(:,Generation)=AA(:,nmin); 
    Wgather(:,Generation)=WW(:,nmin); 
    Fgather(:,Generation)=FF(:,nmin); 
    fmean=mean(Fitvalue); 
    Nmin(Generation,1)=nmin;%这里面的第L个元素即最优的那条染色体所在的位置 
    ymin(Generation,1)=fmin;%ymin为各代最小值的集合 
    ymean(Generation,1)=fmean; 
    Generation=Generation+1;    
end 
 
[MIN,l]=min(ymin);%l为最好的代数 
x=Vgather(:,l); 
y=Sgather(:,l); 
z=Agather(:,l); 
w=Wgather(:,l); 
q=Fgather(:,1); 
plot(x');