clear,close all;
loc = [0.3663, 0.9076; 0.7459, 0.8713; 0.4521, 0.8465;
0.7624, 0.7459; 0.7096, 0.7228; 0.0710, 0.7426;
0.4224, 0.7129; 0.5908, 0.6931; 0.3201, 0.6403;
0.5974, 0.6436; 0.3630, 0.5908; 0.6700, 0.5908;
0.6172, 0.5495; 0.6667, 0.5446; 0.1980, 0.4686;
0.3498, 0.4488; 0.2673, 0.4274; 0.9439, 0.4208;
0.8218, 0.3795; 0.3729, 0.2690; 0.6073, 0.2640;
0.4158, 0.2475; 0.5990, 0.2261; 0.3927, 0.1947;
0.5347, 0.1898; 0.3960, 0.1320; 0.6287, 0.0842;
0.5000, 0.0396; 0.9802, 0.0182; 0.6832, 0.8515];
% loc=rand(50,2);
NumCity=length(loc);
for i = 1:NumCity,
for j = 1:NumCity,
distance(i, j) = norm(loc(i, :) - loc(j, :));
end
end
distance=distance+eye(NumCity).*eps;
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^ Initialize AC Parameters ^^^^^^^^^^^^^
% P: ant α β ρ η Q
% V: 31 1 5 0.1 1./distance 100
ant=31; a=1; b=5; p=0.1; E=1./distance; Q=100;
%^^^^^^^^^^^^
t=ones(NumCity); Nm=200;
Tabu=zeros(ant,NumCity); R_best=zeros(Nm,NumCity);
L_ave=zeros(Nm,1); L_best=inf.*ones(Nm,1);
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tic
for Nc=1:Nm
Tabu=[];
%^^^^^^^^^^^^^^^^^^ Put Ants into Cities ^^^^^^^^^^^^^
Randpos=[];
for i=1:(ceil(ant/NumCity)),Randpos=[Randpos,randperm(NumCity)];end
Tabu(:,1)=(Randpos(1,1:ant))';
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^ Ants According To Probability Choose Next City ^^^^^^^^^^^^^
for j=2:NumCity,
for i=1:ant,
VisitedCity=Tabu(i,1:(j-1));
UnVisited=zeros(1,(NumCity-j+1));
P=UnVisited;
Jc=1;
for k=1:NumCity
if length(find(VisitedCity==k))==0
UnVisited(Jc)=k;
Jc=Jc+1;
end
end
%^^^^^^^^^^^^
for k=1:length(UnVisited)
P(k)=(t(VisitedCity(end),UnVisited(k)).^a)...
*(E(VisitedCity(end),UnVisited(k)).^b);
end
P=P./sum(P);
%^^^^^^^^^^^^
Pcum=cumsum(P);
Select=find(Pcum>=rand);
% Select=find(P==max(P));
ToVisit=UnVisited(Select(1));
Tabu(i,j)=ToVisit;
end
end
if Nc>=2,Tabu(1,:)=R_best(Nc-1,:);end
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^ Record the Best Path ^^^^^^^^^^^^^
L=zeros(ant,1);
dt=zeros(NumCity);
for i=1:ant
R=Tabu(i,:);
for j=1:(NumCity-1), L(i)=L(i)+distance(R(j),R(j+1));end
L(i)=L(i)+distance(R(1),R(NumCity));
for j=1:(NumCity-1)
dt(Tabu(i,j),Tabu(i,j+1))=dt(Tabu(i,j),Tabu(i,j+1))+Q./(L(i));
end
dt(Tabu(i,NumCity),Tabu(i,1))=dt(Tabu(i,NumCity),Tabu(i,1))+Q./L(i);
end
L_best(Nc)=min(L);
pos=find(L==L_best(Nc));
R_best(Nc,:)=Tabu(pos(1),:);
L_ave(Nc)=mean(L);
t=(1-p).*t+dt;
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^ Updata the t ^^^^^^^^^^^^^
% dt=zeros(NumCity);
% for i=1:ant
% for j=1:NumCity-1
% dt(Tabu(i,j),Tabu(i,j+1))=dt(Tabu(i,j),Tabu(i,j+1))+Q./(L(i));
% end
% dt(Tabu(i,NumCity),Tabu(i,1))=dt(Tabu(i,NumCity),Tabu(i,1))+Q./L(i);
% end
% t=(1-p).*t+dt;
% Tabu=zeros(ant,NumCity);
end
toc
Pos=find(L_best==min(L_best));
Shortest_Route=R_best(Pos(1),:);
Shortest_Length=L_best(Pos(1));
subplot(1,2,1)
DrawRoute(loc,Shortest_Route)
subplot(1,2,2)
plot(L_best)
hold on
plot(L_ave)
寻迹,最优路径是吧?你到程序员联合开发网(pudn.com)或者CSDN下载频道找找看,肯定能找到。
PS:我在网上一搜就能搜到好多啊,可能不是你想要的吧。
去百度贴吧,有人留下带密码的邮箱