50城旅行商问题遗传算法matlab实现

population=100;%种群大小
citynum=50;      %城市数目                                             
zhongqun=zeros(population,citynum);%种群
for i=1:population
    zhongqun(i,:)=randperm(citynum);
end
genenum=3000;
best=zeros(genenum,1);
citylist=[31 32;32 39;40 30;37 69;27 68;37 52;38 46;31 62;30 48;21 47;25 55;16 57;
        17 63;42 41;17 33;25 32;5 64;8 52;12 42;7 38;5 25; 10 77;45 35;42 57;32 22;
        27 23;56 37;52 41;49 49;58 48;57 58;39 10;46 10;59 15;51 21;48 28;52 33;
        58 27;61 33;62 63;20 26;5 6;13 13;21 10;30 15;36 16;62 42;63 69;52 64;43 67];
caiyangdian=[1 0:20:500 500:100:1600 1600:200:3000 ];
for u=1:genenum
    fit=zeros(population,1);
    %适应度
    for i=1:population
        sum1=0;
        for j=1:citynum-1
            sum1=sum1+sqrt((citylist(zhongqun(i,j),1)-citylist(zhongqun(i,j+1),1))^2+(citylist(zhongqun(i,j),2)-citylist(zhongqun(i,j+1),2))^2);
        end
        fit(i)=(1000/(sum1+sqrt((citylist(zhongqun(i,citynum),1)-citylist(zhongqun(i,1),1))^2+(citylist(zhongqun(i,citynum),2)-citylist(zhongqun(i,1),2))^2)))^15;
    end
    [best(u), l]=max(fit);
    bestzhongqun(u,:)=zhongqun(l,:);
    prezhongqun=zhongqun(l,:);
    pingjun(u)=sum(fit)/population;
    weizhijuzhen=[];
    weizhijuzhen=find(caiyangdian==u);
    if(~isempty(weizhijuzhen))
        figure(1);
        for k=1:citynum-1%画当代最优图
            text(citylist(zhongqun(l,k),1),citylist(zhongqun(l,k),2),int2str(zhongqun(l,k)));
            line([citylist(zhongqun(l,k),1),citylist(zhongqun(l,k+1),1)],[citylist(zhongqun(l,k),2),citylist(zhongqun(l,k+1),2)]);
        end
        text(citylist(zhongqun(l,citynum),1),citylist(zhongqun(l,citynum),2),int2str(zhongqun(l,citynum)))
        line([citylist(zhongqun(l,citynum),1),citylist(zhongqun(l,1),1)],[citylist(zhongqun(l,citynum),2),citylist(zhongqun(l,1),2)]);
        text(5,5,['第 ',int2str(u),' 代  ','当代最优距离为:',int2str(1000/((best(u))^(1/15)))])
        pause(0.1)
        close(1)
    else
    end
    %轮盘赌
    p=fit./sum(fit);
    Q=zeros(population);
    for i=1:population
        Q(i)=sum(p(1:i));
    end
    individual=zeros(population-1);
    for i=1:population-1
        r=rand();
        if(r<Q(1))
            individual(i)=1;
        else
            sel=find(Q>r);
            individual(i)=sel(1);
        end
    end
    N=population-1;
    temzhongqun=zhongqun;
    for i=1:N
        zhongqun(i,:)=temzhongqun(individual(i),:);
    end
   
   p=rand();
   if p<1 %交叉
       for j=1:N-1;
     
           wei=round(rand*(15));         
           num=35;
           juzhen=zeros(2,10);
           juzhen=zhongqun(j:j+1,wei+1:wei+num);
           tem2=zhongqun(j+1,:);
           tem1=zhongqun(j,:);
           s1=0;
           for o=1:num%找到juzhen第二行某元素的位置,该元素值在第一行也有
               temwei1=find(juzhen(1,:)==juzhen(2,o));
               if isempty(temwei1)
               else
                  s1=s1+1;
                  temjuzhen(s1)=o;
               end
           end
           temjuzhen2=zeros(1,s1);
           for i=1:s1
               temjuzhen2(1,i)=temjuzhen(i);
           end
            %删除juzhen上面找到的元素所在列
           juzhen(:,temjuzhen2)=[];
           [m,n]=size(juzhen);
           if n==0
           else
               for w=1:num-s1%根据juzhen进行交换
                   temwei2=find(tem1==juzhen(1,w));
                   tem=zhongqun(j,temwei2);
                   zhongqun(j,temwei2)=zhongqun(j+1,temwei2);
                   zhongqun(j+1,temwei2)=tem;
               end
               for w=1:num-s1%将重复的再交换
                   temwei3=find(tem1==juzhen(2,w));
                   temwei4=find(tem2==juzhen(1,w));
                   tem=zhongqun(j,temwei3);
                   zhongqun(j,temwei3)=zhongqun(j+1,temwei4);
                   zhongqun(j+1,temwei4)=tem;
               end
           end
       end
    end
    %变异
    pm=0.5;
    for t=1:population-1
        r=rand();
        if r<pm
            %m=round(rand*(N-1))+1;
            p1=round(rand*(citynum-1))+1;
            p2=round(rand*(citynum-1))+1;
            a=zhongqun(t,p1);
            zhongqun(t,p1)=zhongqun(t,p2);
            zhongqun(t,p2)=a;
        end
    end
    zhongqun(population,:)=prezhongqun;
end
[c,l2]=max(best);
f=1000/(c^(1/15))
bestzhongqun(l2,:)
figure(1)
for k=1:citynum-1%画当代最优图
    text(citylist(bestzhongqun(l2,k),1),citylist(bestzhongqun(l2,k),2),int2str(bestzhongqun(l2,k)));
    line([citylist(bestzhongqun(l2,k),1),citylist(bestzhongqun(l2,k+1),1)],[citylist(bestzhongqun(l2,k),2),citylist(bestzhongqun(l2,k+1),2)]);
end
text(citylist(bestzhongqun(l2,citynum),1),citylist(bestzhongqun(l2,citynum),2),int2str(bestzhongqun(l2,citynum)))
line([citylist(bestzhongqun(l2,citynum),1),citylist(bestzhongqun(l2,1),1)],[citylist(bestzhongqun(l2,citynum),2),citylist(bestzhongqun(l2,1),2)]);
text(5,5,['最优在第 ',int2str(l2),' 代  ','最优距离为:',int2str(f)])
figure(2)
plot(1:10:3000,1000./(best(1:10:3000).^(1/15)))
hold on
plot(1:10:3000,1000./(pingjun(1:10:3000).^(1/15)))
legend('最优解','平均解')
title('搜索过程')

猜你喜欢

转载自blog.csdn.net/weixin_42146232/article/details/80212468
今日推荐