%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function: Open and import file of city coordinates in TSP %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[fname,pname,filterindex]=uigetfile('*.*','All Files (*.*)');
set(handles.text_filename,'String',filename);
fid=fopen(filename,'r');
if fid==-1
warndlg('Can not open the file','WARN');
fclose(fid);
end
[citymat,COUNT]=fscanf(fid,' %g');
cN=COUNT/3;
set(handles.edit_citysum,'String',cN);
fclose(fid);
aN=str2double(get(handles.edit_antsum,'String'));
ncmax=str2double(get(handles.edit_ncmax,'String'));
for nc=1:ncmax
for k=1:aN
g(nc,k)=fix((aN-1)*rand(1))+1;
end
end
% Basic Ant Colony Algorithm for TSP
global citymat;
global cN; % city number
global g
aN=str2double(get(handles.edit_antsum,'String')); % Set ant number by using Matlab GUI
initao=str2double(get(handles.edit_tao,'String'));
alpha=str2double(get(handles.edit_alpha,'String'));
beta=str2double(get(handles.edit_beta,'String'));
Q=str2double(get(handles.edit_q,'String'));
rou=str2double(get(handles.edit_rou,'String'));
ncmax=str2double(get(handles.edit_ncmax,'String'));
%
% 原文中以上参数是通过作者自建的GUI界面读入的
% 为便于调用,可把程序改为Matlab的M函数文件,以上参数作为函数的输入
%
% function bestroute = fun_basic_TSP(aN,initao,alpha,beta,Q,rou,ncmax)
%
cities=reshape(citymat,3,cN); % get the city coordinates in TSP
x=cities(2,:);
y=cities(3,:);
for i=1:cN
for j=1:cN
dist(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2);
end
end
for i=1:cN
for j=1:cN
if j~=i
tao(i,j)=initao;
yita(i,j)=1/dist(i,j);
end
end
end
initao=initao.*ones(cN,cN);
detatao=zeros(cN,cN);
bestsolu=1e13;
%% This is the phase in which ants build their tours
for nc=1:ncmax
tabu=zeros(aN,cN);
for k=1:aN
&