www.pudn.com > TWVRP.rar > gjmain.m, change:2016-04-20,size:1697b


function gjmain() 
clear; 
clc; 
  
%for xx=1:10 
      
tic  
  
genmax=500; 
[xy,n,nind,D]=datas(); 
%生成初试种群 
chrom=initpop(nind,n); 
gen=1;  
while gen<=genmax 
     %计算适应值矩阵 
     [fit,fitx]=fitness(D,n,nind,chrom); 
     %找出最优个体适应值 
     avefit(gen)=sum(fit)/nind;%平均适应值 
     [bestfitc,bestindex]=min(fit); 
     bestindex=bestindex(1); 
     bestfit(gen)=bestfitc;%最小适应值fit的集 
     bestpop(gen,:)=chrom(bestindex,:);%最优个体集 
     %选择 
     [chrom,fits]=gjselect(chrom,fit); 
     %交叉 
     chrom=gjcrossover0(chrom,fits); 
     %变异 
     chrom=gjmutate(chrom,avefit,gen); 
     %精英策略 
     chrom(1,:)=bestpop(gen,:); 
     gen=gen+1; 
end 
y=bestfit; 
y1=avefit; 
%找出最优的适应值、个体 
[minbestfit,minindex]=min(bestfit);%取最优适应值的位置、最优适应值 
minindex=minindex(1); 
%取最优个体 
minbestpop=bestpop(minindex,:); 
  
%解码最优染色体 
a=randperm(size(minbestpop,2)); 
a1=sort(a); 
a2=sort(minbestpop); 
a3=zeros(1,size(minbestpop,2)); 
for i=1:size(minbestpop,2) 
    for j=1:size(minbestpop,2) 
        if minbestpop(i)==a2(j); 
           a3(i)=a1(j); 
           a2(j)=0; 
           break; 
        end 
    end 
end 
  
minbestpop1=a3;%解码的最优个体 
minbestfit ;%最优值 
  
  
%画线路 
% DrawPath(minbestpop1,xy); 
  
%查看适应值的变化情况 
bestfit ; 
%迭代图 
figure 
x=1:1:genmax; 
hold on; 
plot(x,y); 
  
plot(x,y1,'r-');%适应值平均数 
title('优化过程') 
xlabel('迭代次数') 
ylabel('最优值') 
disp('最优解为:'); 
%axis([0, 500, 30, 60]); 
R=[minbestpop1 minbestpop1(1)]; 
N=size(R,2); 
p=num2str(R(1)); 
for i=2:N; 
    p=[p,'->',num2str(R(i))]; 
end 
disp(p); 
disp(['最优成本为:',num2str(minbestfit)]); 
toc 
  
%end