-
Notifications
You must be signed in to change notification settings - Fork 1
/
ant2.m
96 lines (89 loc) · 2.59 KB
/
ant2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function Shortest_Length = ant2(Data)
clc;
%% 初始化参数
N=50; %% m 蚂蚁个数
alpha=1; %% 信息素重要程度
beta=5; %% 启发式因子重要程度
rho=0.1; %% 信息素蒸发系数
max_iter=200; %%最大迭代次数
Q=100; %%信息素增强系数
n=size(Data,1);
road_length=zeros(n,n); %D表示完全图的赋权邻接矩阵
for i=1:n
for j=1:n
if i~=j
road_length(i,j)=Distance(Data(i,1),Data(i,2),Data(j,1),Data(j,2));
else
road_length(i,j)=eps;
end
road_length(j,i)=road_length(i,j);
end
end
%% 初始化路线和信息素矩阵
heu=1./road_length;
pheromoneMatrix=ones(n,n);
path=zeros(N,n);
iter=1;
path_best=zeros(max_iter,n);
length_best=inf.*ones(max_iter,1);
length_mean=zeros(max_iter,1);
%% 迭代循环;停止条件:达到最大的迭代次数
dots = 0;
while iter<=max_iter
positionInit=[];
for i=1:(ceil(N/n))
positionInit = [positionInit,randperm(n)];
end
path(:,1)=(positionInit(1,1:N))';
for j=2:n
for i=1:N
visited=path(i,1:(j-1));
pos=zeros(1,(n-j+1));
P=pos;
pass_cities=1;
for k=1:n
if isempty(find(visited==k, 1))
pos(pass_cities)=k;
pass_cities=pass_cities+1;
end
end
for k=1:length(pos)
P(k)=(pheromoneMatrix(visited(end),pos(k))^alpha)*(heu(visited(end),pos(k))^beta);
end
P=P/(sum(P));
Psum=cumsum(P);
Select=find(Psum>=rand);
to_visit=pos(Select(1));
path(i,j)=to_visit;
end
end
if iter>=2
path(1,:)=path_best(iter-1,:);
end
L=zeros(N,1);
for i=1:N
R=path(i,:);
for j=1:(n-1)
L(i)=L(i)+road_length(R(j),R(j+1));
end
L(i)=L(i)+road_length(R(1),R(n));
end
length_best(iter)=min(L);
pos=find(L==length_best(iter));
path_best(iter,:)=path(pos(1),:);
length_mean(iter)=mean(L);
delta_pheromone=zeros(n,n);
for i=1:N
for j=1:(n-1)
delta_pheromone(path(i,j),path(i,j+1))=delta_pheromone(path(i,j),path(i,j+1))+Q/L(i);
end
delta_pheromone(path(i,n),path(i,1))=delta_pheromone(path(i,n),path(i,1))+Q/L(i);
end
pheromoneMatrix=(1-rho).*pheromoneMatrix+delta_pheromone;
path=zeros(N,n);
iter = iter+1;
end
fprintf('Done!shortest=');
%% 最终路径
Pos=find(length_best==min(length_best));
Shortest_Length=length_best(Pos(1));