《蚁群算法原理及其应用》(段海滨)附录Matlab源程序

本文提供了一个基本的蚁群算法解决旅行商问题(TSP)的Matlab源程序。程序首先定义了城市坐标,然后计算城市之间的距离矩阵。接下来,通过循环迭代,蚂蚁们构建它们的路径并更新信息素。最后,程序输出最佳路径并进行可视化展示。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%       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
      &

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值