西安工业大学毕业设计(论文) 程序2:
函数1:
function F=CalDist(dislist,s)
DistanV=0; n=size(s,2); for i=1:(n-1)
DistanV=DistanV+dislist(s(i),s(i+1)); end
DistanV=DistanV+dislist(s(n),s(1)); F=DistanV; 函数2:
function C=changeNum(x,y); CityNum=size(x,2); C=ones(CityNum-1,2); for i=1:CityNum-1 pos=find(x==y(i)); C(i,:)=[i pos];
x=changeFun(x,C(i,:)); end end 函数3:
function xnew=changeFun(x,C); changeLen=size(C,1); xnew=x;
for i=1:changeLen a=xnew(C(i,1));
xnew(C(i,1))=xnew(C(i,2)); xnew(C(i,2))=a; end end 函数4:
function [DLn,cityn]=tsp(n)
if n==30
city30=[41 94;37 84;54 67;25 62;7 64;2 99;68 58;71 44;54 62;83 69;64 60;18 54;22 60;
83 46;91 38;25 38;24 42;58 69;71 71;74 78;87 76;18 40;13 40;82 7;62 32;58 35;45 21;41 26;44 35;4 50];0 cities d'=423.741 by D B Fogel for i=1:30
for j=1:30
DL30(i,j)=((city30(i,1)-city30(j,1))^2+(city30(i,2)-city30(j,2))^2)^0.5; end end
40
西安工业大学毕业设计(论文)
DLn=DL30; cityn=city30; end
if n==50
city50=[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];P cities d'=427.855 by D B Fogel for i=1:50
for j=1:50
DL50(i,j)=((city50(i,1)-city50(j,1))^2+(city50(i,2)-city50(j,2))^2)^0.5; end end
DLn=DL50; cityn=city50; end
if n==75
city75=[48 21;52 26;55 50;50 50;41 46;51 42;55 45;38 33;33 34;45 35;40 37;50 30;
55 34;54 38;26 13;15 5;21 48;29 39;33 44;15 19;16 19;12 17;50 40;22 53;21 36;
20 30;26 29;40 20;36 26;62 48;67 41;62 35;65 27;62 24;55 20;35 51;30 50;
45 42;21 45;36 6;6 25;11 28;26 59;30 60;22 22;27 24;30 20;35 16;54 10;50 15;
44 13;35 60;40 60;40 66;31 76;47 66;50 70;57 72;55 65;2 38;7 43;9 56;15 56;
10 70;17 64;55 57;62 57;70 64;64 4;59 5;50 4;60 15;66 14;66 8;43 26];u cities d'=549.18 by D B Fogel for i=1:75
for j=1:75
DL75(i,j)=((city75(i,1)-city75(j,1))^2+(city75(i,2)-city75(j,2))^2)^0.5; end end
DLn=DL75; cityn=city75; end 函数5:
41
西安工业大学毕业设计(论文)
function m=drawTSP(Clist,BSF,bsf,p,f) CityNum=size(Clist,1); for i=1:CityNum-1
plot([Clist(BSF(i),1),Clist(BSF(i+1),1)],[Clist(BSF(i),2),Clist(BSF(i+1),2)],'ks-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',3); hold on; end
plot([Clist(BSF(CityNum),1),Clist(BSF(1),1)],[Clist(BSF(CityNum),2),Clist(BSF(1),2)],'ks-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',3); title([num2str(CityNum),'City-TSP-GA']); if f==0
text(5,5,['Step ',int2str(p),' Shortest Distance ',num2str(bsf)]); else
text(5,5,['Final Search Result: Shortest Distance ',num2str(bsf)]); 函数6: clear
%初始化
Alpha=0.5; %个体经验保留概率 Beta=0.5; %全局经验保留概率 NC_max=400; %最大迭代次数 m=30; %微粒数
CityNum=30; %问题的规模(城市个数) [dislist,Clist]=tsp(CityNum);
NC=1;%迭代计数器
R_best=zeros(NC_max,CityNum); %各代最佳路线 L_best=inf.*ones(NC_max,1);%各代最佳路线的长度 L_ave=zeros(NC_max,1);%各代路线的平均长度
%产生微粒的初始位置 for i=1:m
x(i,:)=randperm(CityNum); L(i)=CalDist(dislist,x(i,:)); end
p=x; %p为个体最好解 pL=L;
[L_best(1,1) n_best]=min(L); R_best(1,:)=x(n_best,:); L_ave(1,1)=mean(L);
%初始交换序
v=ones(CityNum-1,2,m);
42
西安工业大学毕业设计(论文)
figure(1);
while NC<=NC_max %停止条件之一:达到最大迭代次数 for i=1:m
xnew(i,:)=changeFun(x(i,:),v(:,:,i)); if rand
A=changeNum(x(i,:),p(i,:));
xnew(i,:)=changeFun(xnew(i,:),A); end
if rand B=changeNum(x(i,:),R_best(NC,:)); xnew(i,:)=changeFun(xnew(i,:),B); end v(:,:,i)=changeNum(x(i,:),xnew(i,:)); L(i)=CalDist(dislist,xnew(i,:)); if L(i) p(i,:)=xnew(i,:); pL(i)=L(i); end end [L_bestnew n_best]=min(L); R_bestnew=xnew(n_best,:); L_ave(NC+1,1)=mean(L); if L_bestnew L_best(NC+1,1)=L_bestnew; R_best(NC+1,:)=R_bestnew; else L_best(NC+1,1)=L_best(NC,1); R_best(NC+1,:)=R_best(NC,:); end x=xnew; drawTSP(Clist,R_best(NC,:),L_best(NC,1),NC,0); NC=NC+1; end %输出结果 Pos=find(L_best==min(L_best)); Shortest_Route=R_best(Pos(1),:); Shortest_Length=L_best(Pos(1)); figure(2); plot([L_best L_ave]); 43 西安工业大学毕业设计(论文) legend('最短距离','平均距离'); 完 44