www.pudn.com > anttsp.rar > AS_main.m, change:2008-04-20,size:3344b

```clear all;
tic;
%城市数据
X=data(:,2);                                               %城市横坐标
Y=data(:,3);                                               %城市纵坐标
%AS算法参数
tau=0.003;                                                 %初始信息素量
ant_num=16;                                                %蚂蚁数目
iter_num=100;                                              %算法迭代次数
pher_evap_rate=0.5;                                        %信息素蒸发速率
pher_effe_rate=1;                                          %信息素系数
heur_effe_rate=2;                                          %启发式信息系数
cand_length=0;                                             %最近邻列表长度
%基本蚂蚁类型：ant_density,ant_quantity,ant_cycle
as_type='ant_cycle';
%计算城市之间的距离和最近邻列表
if cand_length==0
[dist,city_num]=dist_cand(X,Y);
else
[dist,city_num,cand_list]=dist_cand(X,Y,cand_length);
end
%初始化信息素矩阵和计算信息素与启发式信息联合矩阵
pher=(ones(city_num)-eye(city_num))*tau;
choice_info=calc_info(pher_effe_rate,heur_effe_rate,pher,dist);
%蚂蚁构建路径
for order=1:iter_num
%每一次迭代开始时清楚蚂蚁记录并随机初始化蚂蚁起点城市
ant=clear_fisttour(ant_num,city_num);
%采用并行方式构建蚂蚁剩余部分路径
for step=2:city_num
for i=1:ant_num
pres_city =ant(i).tour(step-1);
if cand_length==0
next_city=as_next_tour(city_num,choice_info,pres_city,ant(i));
else
next_city=as_cand_next_tour(cand_list,cand_length,city_num,pres_city,choice_info,ant(i));
end
%将下一访问城市添加到路径和记录表中
ant(i).tour(step)=next_city;
ant(i).visited(next_city)=1;
end
end
%计算每一只蚂蚁的路径长度和当前迭代最优路径
[iter_best,ant]=tourlength_iterbest(ant_num,city_num,dist,ant);
%求得迭代至今最优路径
if order==1
glob_best.tour=iter_best.tour;
glob_best.length=iter_best.length;
glob_best.found=1;
glob_best.time=toc;
elseif   iter_best.length<glob_best.length
glob_best.tour=iter_best.tour;
glob_best.length=iter_best.length;
glob_best.found=order;
glob_best.time=toc;
end
%作图
figure(1)
for i=1:city_num+1
m=glob_best.tour(i);
p(i)=X(m);
q(i)=Y(m);
end
scatter(X,Y,'.');box on;
hold on;
plot(p,q);
title(num2str(glob_best.length));
hold off;
%进行信息素更新
pher=as_pher_update(pher_evap_rate,city_num,ant_num,dist,as_type,pher,ant);
%更新信息素与启发式信息素联合矩阵
choice_info=calc_info(pher_effe_rate,heur_effe_rate,pher,dist);
end
disp(['蚂蚁系统运行时间为：' num2str(toc) '秒']);
disp(['蚂蚁系统最优路径长度为：' num2str(glob_best.length)]);
disp(['蚂蚁系统求得最优路径时间为：' num2str(glob_best.time) '秒']);
disp(['蚂蚁系统最优路径的迭代次数为：' num2str(glob_best.found)]);```