蚁群算法求解TSP问题MATLAB程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

%%蚁群算法¨clearcloseclcn=10;%城市数量m=100;%蚂蚁数量alfa=1.5;beta=2.5;rho=0.1;Q=1000;maxgen=50;x=[214963248125]';y=[891241258115]';%x=[37,49,52,20,40,21,17,31,52,51,42,31,5,12,36,52,27,17,13,57,62,42,16,8,7,27,30,43,58,58,37,38,46,61,62,63,32,45,59,5,10,21,5,30,39,32,25,25,48,56,30]';%y=[52,49,64,26,30,47,63,62,33,21,41,32,25,42,16,41,23,33,13,58,42,57,57,52,38,68,48,67,48,27,69,46,10,33,63,69,22,35,15,6,17,10,64,15,10,39,32,55,28,37,40]';City=[x,y];%城市坐标%%城市之间的距离fori=1:nD(i,:)=((City(i,1)-City(:,1)).^2+(City(i,2)-City(:,2)).^2).^0.5+eps;endeta=1./D;%启发因子tau=ones(n);%信息素矩阵path=zeros(m,n);%记录路径foriter=1:maxgen%%放置蚂蚁path(:,1)=randi([1n],m,1);fori=2:nforj=1:mvisited=path(j,1:i-1);leftcity=setdiff(1:n,visited);%%计算剩下城市的概率P=zeros(1,length(leftcity));fork=1:length(leftcity)P(k)=tau(visited(end),leftcity(k))^alfa*eta(visited(end),leftcity(k))^beta;%判断是否有重复城市endP1=sum(P);Pk=P/P1;P=cumsum(Pk);r=rand;index=find(P=r);nextcity=leftcity(index(1));path(j,i)=nextcity;endendforflag=1:miflength(unique(path(flag,:)))~=n%keyboard;endendifiter=2path(1,:)=Pathbest(iter-1,:);endfori=1:mnode=path(i,:);d=0;forj=1:n-1d=d+D(node(j),node(j+1));endL(i)=d;end[shortroute,antindex]=min(L);Lbest(iter)=shortroute;Pathbest(iter,:)=path(antindex,:);detatau=zeros(n);fori=1:mforj=1:n-1detatau(path(i,j),path(i,j+1))=detatau(path(i,j),path(i,j+1))+Q/L(i);detatau(path(i,j+1),path(i,j))=detatau(path(i,j),path(i,j+1));enddetatau(path(i,n),path(i,1))=detatau(path(i,n),path(i,1))+Q/L(i);detatau(path(i,1),path(i,n))=detatau(path(i,n),path(i,1));endtau=(1-rho)*tau+detatau;path=zeros(m,n);endindex=find(Lbest==min(Lbest));shortestpath=Pathbest(index(1),:);shortestdistance=Lbest(index(1))subplot(1,2,1)plot(x,y,'o')holdonfori=1:n-1firstcity=shortestpath(i);nextcity=shortestpath(i+1);plot([x(firstcity),x(nextcity)],[y(firstcity),y(nextcity)],'b');endfirstcity=shortestpath(n);nextcity=shortestpath(1);plot([x(firstcity),x(nextcity)],[y(firstcity),y(nextcity)],'b');axisequalaxis([018018])subplot(1,2,2)plot(Lbest)holdontitle('×î¶Ì¾àÀë')

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功