資源描述:
《TSP禁忌搜索程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、functionTspTSclear;city31=[13042312;36391315;41772244;37121399;34881535;33261556;32381229;41961004;4312790;4386570;30071970;25621756;27881491;23811676;1332695;37151678;39182179;40612370;37802212;36762578;40292838;42632931;34291908;35072367;33942643;34393201;29353240;31403
2、550;25452357;27782826;23702975];fori=1:31forj=1:31DL31(i,j)=((city31(i,1)-city31(j,1))^2+(city31(i,2)-city31(j,2))^2)^0.5;endenddislist=DL31;Clist=city31;CityNum=size(dislist,2);Tlist=zeros(CityNum);%禁忌表(tabulist)cl=80;%保留前cl個(gè)最好候選解bsf=Inf;tl=ceil(CityNum^0.5);%禁忌長度(tabule
3、ngth)l1=160;%候選解(candidate),不大于n*(n-1)/2(全部領(lǐng)域解個(gè)數(shù))S0=randperm(CityNum);S=S0;BSF=S0;Si=zeros(l1,CityNum);StopL=80*CityNum;p=1;clf;figure(1);stop=uicontrol('style','toggle','string','stop','background','white');tic;while(pCityNum*(CityNum)/2disp('候選解個(gè)數(shù),不大于n*(n-1)
4、/2(全部領(lǐng)域解個(gè)數(shù))!系統(tǒng)自動(dòng)退出!');l1=(CityNum*(CityNum)/2)^.5;break;endArrS(p)=F(dislist,S);i=1;A=zeros(l1,2);whilei<=l1M=CityNum*rand(1,2);M=ceil(M);ifM(1)~=M(2)m1=max(M(1),M(2));m2=min(M(1),M(2));A(i,1)=m1;A(i,2)=m2;ifi==1isdel=0;elseforj=1:i-1ifA(i,1)==A(j,1)&&A(i,2)==A(j,2)isdel=
5、1;break;elseisdel=0;endendendif~isdeli=i+1;elsei=i;endelsei=i;endendCL=Inf*ones(cl,4);fori=1:l1Si(i,:)=S;Si(i,[A(i,1),A(i,2)])=S([A(i,2),A(i,1)]);F(i)=F(dislist,Si(i,:));ifi<=clCL(i,2)=F(i);CL(i,1)=i;CL(i,3)=S(A(i,1));CL(i,4)=S(A(i,2));elseforj=1:clifF(i)6、(i);CL(j,1)=i;CL(j,3)=S(A(i,1));CL(j,4)=S(A(i,2));break;endendendendifCL(1,2)7、CL(i,4))==0S=Si(CL(i,1),:);form=1:CityNumforn=1:CityNumifTlist(m,n)~=0Tlist(m,n)=Tlist(m,n)-1;endendendTlist(CL(i,3),CL(i,4))=tl;break;endendendp=p+1;Arrbsf(p)=bsf;fori=1:CityNum-1plot([Clist(BSF(i),1),Clist(BSF(i+1),1)],[Clist(BSF(i),2),Clist(BSF(i+1),2)],'bo-');holdon;e
8、ndplot([Clist(BSF(CityNum),1),Clist(BSF(1),1)],[Clist(BSF(CityNum),2),Clist(BSF(1),2)],'bo-');ti