模拟退火算法及禁忌搜索算法的matlab源程序

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

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

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

资源描述

%%%模拟退火算法源程序%此题以中国31省会城市的最短旅行路径为例:%clear;clc;function[MinD,BestPath]=MainAneal(pn)%CityPosition存储的为每个城市的二维坐标x和y;CityPosition=[13042312;36391315;41772244;37121399;34881535;33261556;32381229;...41961044;4312790;4386570;30071970;25621756;27881491;23811676;...1332695;37151678;39182179;40612370;37802212;36762578;40292838;...42632931;34291908;35072376;33942643;34393201;29353240;31403550;...25452357;27782826;23702975];figure(1);plot(CityPosition(:,1),CityPosition(:,2),'o')m=size(CityPosition,1);%城市的数目%D=sqrt((CityPosition(:,ones(1,m))-CityPosition(:,ones(1,m))').^2+...(CityPosition(:,2*ones(1,m))-CityPosition(:,2*ones(1,m))').^2);path=zeros(pn,m);fori=1:pnpath(i,:)=randperm(m);enditer_max=100;%im_max=5;%Len1=zeros(1,pn);Len2=zeros(1,pn);path2=zeros(pn,m);t=zeros(1,pn);T=1e5;tau=1e-5;N=1;whileT=tauiter_num=1;m_num=1;whilem_numm_max&&iter_numiter_maxfori=1:pnLen1(i)=sum([D(path(i,1:m-1)+m*(path(i,2:m)-1))D(path(i,m)+m*(path(i,1)-1))]);path2(i,:)=ChangePath2(path(i,:),m);Len2(i)=sum([D(path2(i,1:m-1)+m*(path2(i,2:m)-1))D(path2(i,m)+m*(path2(i,1)-1))]);endR=rand(1,pn);iffind((Len2-Len1t&exp((Len1-Len2)/T)R)~=0)path(find((Len2-Len1t&exp((Len1-Len2)/T)R)~=0),:)=path2(find((Len2-Len1t&exp((Len1-Len2)/T)R)~=0),:);%#okFNDSBLen1(find((Len2-Len1t&exp((Len1-Len2)/T)R)~=0))=Len2(find((Len2-Len1t&exp((Len1-Len2)/T)R)~=0));[TempMinD,TempIndex]=min(Len1);TracePath(N,:)=path(TempIndex,:);%#okAGROWDistance(N)=TempMinD;%#okAGROWN=N+1;elsem_num=m_num+1;endenditer_num=iter_num+1;T=T*0.9;end[MinD,Index]=min(Distance);BestPath=TracePath(Index,:);%disp(MinD)%画出路线图figure(2);plot(CityPosition(BestPath(1:end-1),1),CityPosition(BestPath(1:end-1),2),'r*-');functionp2=ChangePath2(p1,CityNum)while(1)R=unidrnd(CityNum,1,2);ifabs(R(1)-R(2))0break;endendI=R(1);J=R(2);ifIJp2(1:I)=p1(1:I);p2(I+1:J)=p1(J:-1:I+1);p2(J+1:CityNum)=p1(J+1:CityNum);elsep2(1:J-1)=p1(1:J-1);p2(J:I+1)=p1(I+1:-1:J);p2(I:CityNum)=p1(I:CityNum);end%%%禁忌搜索算法解决TSP问题%此题以中国31省会城市的最短旅行路径为例:%禁忌搜索是对局部领域搜索的一种扩展,是一种全局逐步寻优算法,搜索过程可以接受劣解,有较强的爬山能力.领域结构对收敛性有很大影响。function[BestShortcut,theMinDistance]=TabuSearchclear;clc;Clist=[13042312;36391315;41772244;37121399;34881535;33261556;32381229;...41961044;4312790;4386570;30071970;25621756;27881491;23811676;...1332695;37151678;39182179;40612370;37802212;36762578;40292838;...42632931;34291908;35072376;33942643;34393201;29353240;31403550;...25452357;27782826;23702975];CityNum=size(Clist,1);%TSP问题的规模,即城市数目dislist=zeros(CityNum);fori=1:CityNumforj=1:CityNumdislist(i,j)=((Clist(i,1)-Clist(j,1))^2+(Clist(i,2)-Clist(j,2))^2)^0.5;endendTabuList=zeros(CityNum);%(tabulist)TabuLength=round((CityNum*(CityNum-1)/2)^0.5);%禁忌长度(tabulength)Candidates=200;%候选集的个数(全部领域解个数)CandidateNum=zeros(Candidates,CityNum);%候选解集合S0=randperm(CityNum);%随机产生初始解BSF=S0;BestL=Inf;clf;figure(1);stop=uicontrol('style','toggle','string'…,'stop','background','white');tic;p=1;StopL=80*CityNum;whilepStopLifCandidatesCityNum*(CityNum-1)/2disp('候选解个数不大于n*(n-1)/2!');break;endALong(p)=Fun(dislist,S0);i=1;A=zeros(Candidates,2);whilei=CandidatesM=CityNum*rand(1,2);M=ceil(M);ifM(1)~=M(2)A(i,1)=max(M(1),M(2));A(i,2)=min(M(1),M(2));ifi==1isa=0;elseforj=1:i-1ifA(i,1)==A(j,1)&&A(i,2)==A(j,2)isa=1;break;elseisa=0;endendendif~isai=i+1;elseendelseendendBestCandidateNum=100;%保留前BestCandidateNum个最好候选解BestCandidate=Inf*ones(BestCandidateNum,4);F=zeros(1,Candidates);fori=1:CandidatesCandidateNum(i,:)=S0;CandidateNum(i,[A(i,2),A(i,1)])=S0([A(i,1),A(i,2)]);F(i)=Fun(dislist,CandidateNum(i,:));ifi=BestCandidateNumBestCandidate(i,2)=F(i);BestCandidate(i,1)=i;BestCandidate(i,3)=S0(A(i,1));BestCandidate(i,4)=S0(A(i,2));elseforj=1:BestCandidateNumifF(i)BestCandidate(j,2)BestCandidate(j,2)=F(i);BestCandidate(j,1)=i;BestCandidate(j,3)=S0(A(i,1));BestCandidate(j,4)=S0(A(i,2));break;endendendend%对BestCandidate[JL,Index]=sort(BestCandidate(:,2));SBest=BestCandidate(Index,:);BestCandidate=SBest;ifBestCandidate(1,2)BestLBestL=BestCandidate(1,2);S0=CandidateNum(BestCandidate(1,1),:);BSF=S0;form=1:CityNumforn=1:CityNumifTabuList(m,n)~=0TabuList(m,n)=TabuList(m,n)-1;endendendTabuList(BestCandidate(1,3),BestCandidate(1,4))=TabuLength;elsefori=1:BestCandidateNumifTabuList(BestCandidate(i,3),BestCandidate(i,4))==0S0=CandidateNum(BestCandidate(i,1),:);form=1:CityNumforn=1:CityNumifTabuList(m,n)~=0TabuList(m,n)=TabuList(m,n)-1;endendendTabuList(BestCandidate(i,3),BestCandidate(i,4))=TabuLength;break;endendendp=p+1;ArrBestL(p)=BestL;%#okAGROWfori=1:CityNum-1plot([Clist(BSF(i),1),Clist(BSF(i+1),1)],[Clist(BSF(i),2),Clist(BSF(i+1),2)],'bo-');holdon;endplot([Clist(BSF(CityNum),1),Clist(BSF(1),1)],[Clist(BSF(CityNum),2),Clist(BSF(1),2)],'ro-');title(['Counter:',int2str(p*Candidates),'TheMinDistance:',num2str(BestL)]);holdoff;pause(0.005);ifget(stop,'value')==1break;endendtoc;BestShortcut=BSF;theMinDistance=BestL;set(stop,'style','pushbutton','string',…'close','callback','close(gcf)');figure(2);plot(ArrBestL,'r');holdon;plot(ALong,'b');grid;title('搜索过程');legend('BestSoFar','当前解');endfunctionF=Fun(dislist,s)%

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

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

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

×
保存成功