【TSP】基于蚁群算法求解TSP问题matlab源码含GUI

2021/7/9 14:09:04

本文主要是介绍【TSP】基于蚁群算法求解TSP问题matlab源码含GUI,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.关于旅行商(TSP)问题及衍化

  旅行商问题(Traveling Saleman Problem,TSP)是车辆路径调度问题(VRP)的特例,由于数学家已证明TSP问题是NP难题,因此,VRP也属于NP难题。旅行商问题(TSP)又译为旅行推销员问题、货郎担问题,简称为TSP问题,是最基本的路线问题,该问题是在寻求单一旅行者由起点出发,通过所有给定的需求点之后,最后再回到原点的最小路径成本。——旅行商问题百科

  很明显,当节点数很少时,大多数人都会想到,问题很简单,直接穷举就OK了,但实际问题中,节点数往往很大,变得不可能。例如:对于一个仅有16个城市的旅行商问题,如果用穷举法来求问题的最优解,需比较的可行解有:15!/2=653,837,184,000个。在1993年,使用当时的工作站用穷举法求解此问题需时92小时。即使现在计算机速度快,但是面对复杂的问题,仍然不够。这就是所谓的“组合爆炸”,指数级的增长,所以科学家逐步寻找近似算法或者启发式算法,目的是在合理的时间范围内找到可接受的最优解。

  TSP问题解决算法的发展可以分为3个部分:

1).经典精确算法:穷举法、线性规划算法、动态规划算法、分支定界算法等运筹学中的传统算法,这些算法复杂度一般都很大,只适用于求解小规模问题。

2).近似算法:当问题规模较大时,其所需的时间成级数增长,这是我们无法接受的,算法求解问题的规模受到了很大的限制,一个很自然的想法就是牺牲精确解法中的最优性,去寻找一个好的时间复杂度我们可以容忍的,同时解的质量我们可以接受的算法.基于这一思想所设计出的算法统称为近似算法。如插入算法,最邻近算法等。

3).智能算法:随着科学技术和生产的不断发展,许多实际问题不可能在合理的时间范围内找到全局最优解,这就促使了近代最优化问题求解方法的产生。随着各种不同搜索机制的启发式算法相继出现,如禁忌搜索、遗传算法、模拟退火算法、人工神经网络、进化策略、进化编程、粒子群优化算法、蚁群优化算法和免疫计算等,掀起了研究启发式算法的高潮。

  具体每一种算法不再详细描述,大家可以针对性的寻找相应资料进行了解。

  TSP问题在实际的生产生活中,更加实际环境不同,有很多衍生的经典问题。车辆路径调度(VRP)扩展问题是经典VRP加入各种约束条件后而形成的。例如需求约束形成的需求随机的车辆路径问题(SVRP);加入时间约束得到的带时间窗的车辆路径题(VRPTW);加入距离约束的距离约束车辆路径问题(DVRP);根据其它条件的不同,还有多配送中心车辆路径问题(MDVRP)、可切分的车辆路径问题(SDVRP);先配送再收集车辆路径问题(VRPB)、配送收集车辆路径问题(VRPPD);信息不完全的模糊车辆路径问题(FVRP)[3]。

回到目录

2.群蚁算法基本原理

2.1 算法综述

  对于VRP问题,求解算法大致可分为精确算法和人工智能算法两大类。精确性算法基于严格的数学手段,在可以求解的情况下,解的质量较好。但是由于算法严格,运算量大,特别是大规模的问题几乎无法求解。所以其应用只能是小规模的确定性问题,面对中小规模问题,人工智能算法在精度上不占优势。但规模变大时,人工智能方法基本能在可接受时间里,找到可接受的满意解,这是精确算法难以做到的。由于的实际问题,各种约束错综复杂,人工智能算法显示出了巨大的优越性,也正因为如此,实际应用中,人工智能算法要更广泛。求解车辆路径调度问题的精确算法有动态规划法、分枝定界法等。并开始寻求所得结果可接受的启发式算法,以处理大规模实际问题,一些其他学科的新一代优化算法相继出现,如禁忌搜索算法,遗传算法,人工神经网络算法,以及现在研究较多的蚁群算法等。

2.2 群蚁算法的原理

  蚁群算法是受到对真实蚂蚁群觅食行为研究的启发而提出。生物学研究表明:一群相互协作的蚂蚁能够找到食物和巢穴之间的最短路径,而单只蚂蚁则不能。生物学家经过大量细致观察研究发现,蚂蚁个体之间的行为是相互作用相互影响的。蚂蚁在运动过程中,能够在它所经过的路径上留下一种称之为信息素的物质,而此物质恰恰是蚂蚁个体之间信息传递交流的载体。蚂蚁在运动时能够感知这种物质,并且习惯于追踪此物质爬行,当然爬行过程中还会释放信息素。一条路上的信息素踪迹越浓,其它蚂蚁将以越高的概率跟随爬行此路径,从而该路径上的信息素踪迹会被加强,因此,由大量蚂蚁组成的蚁群的集体行为便表现出一种信息正反馈现象。某一路径上走过的蚂蚁越多,则后来者选择该路径的可能性就越大。蚂蚁个体之间就是通过这种间接的通信机制实现协同搜索最短路径的目标的。我们举例简单说明蚂蚁觅食行为:

    

    如上图a,b,c的示意图:

    a图是原始状态,蚂蚁起始点为A,要到达E,中途有障碍物,要绕过才能到达。BC和BH是绕过障碍物的2条路径(假设只有2条)。各个路径的距离d已经标定。

    b图是t=0时刻蚂蚁状态,各个边上有相等的信息素浓度,假设为15;

    c图是t=1时刻蚂蚁经过后的状态,各个边的信息素浓度,有变化;因为大量蚂蚁的选择概率会不一样,而选择概率是和路径长度相关的。所以越短路径的浓度会越来越大,经过此短路径达到目的地的蚂蚁也会比其他路径多。这样大量的蚂蚁实践之后就找到了最短路径。所以这个过程本质可以概括为以下几点:

    1.路径概率选择机制信息素踪迹越浓的路径,被选中的概率越大

    2.信息素更新机制路径越短,路径上的信息素踪迹增长得越快

    3.协同工作机制蚂蚁个体通过信息素进行信息交流。

    从蚂蚁觅食的原理可见,单个个体的行为非常简单蚂蚁只知道跟踪信息素爬行并释放信息素,但组合后的群体智能又非常高蚂蚁群能在复杂的地理分布的清况下,轻松找到蚁穴与食物源之间的最短路径。这种特点恰恰与元启发算法的特点相一致,蚁群优化算法正是受到这种生态学现象的启发后加以模仿并改进而来,觅食的蚂蚁由人工蚁替代,蚂蚁释放的信息素变成了人工信息素,蚂蚁爬行和信息素的蒸发不再是连续不断的,而是在离散的时空中进行。

  上述例子如果不好理解,我在这里贴几张PPT,个人感觉非常不错,也是我找了很多资料觉得最好理解的【来源是大连理工大学谷俊峰】,点击这里提供下载:蚁群算法基本知识.rar。

    从深层意义上来讲,蚁群算法作为优化的方法之一,属于人工群集智能领域。人工群集智能,大都受自然群集智能如昆虫群和动物群等的启发而来。除了具有独特的强有力的合作搜索能力外,还可以利用一系列的计算代理对问题进行分布式处理,从而大大提高搜索效率。

回到目录

3.群蚁算法的基本流程

  我们还是采用大连理工大学谷俊峰的PPT来说明问题,重要公式进行截图计算和解释,对PPT难以理解的地方进行单独解释:

3.1 基本数学模型

  首先看看基本TSP问题的基本数学模型:

  问题其实很简单,目标函数就是各个走过路径的总长度,注意的就是距离矩阵根据实际的问题不一样,长度是不一样的。

3.2 群蚁算法说明

  在说明群蚁算法流程之前,我们对算法原理和几个注意点进行描述:

1.TSP问题的人工蚁群算法中,假设m只蚂蚁在图的相邻节点间移动,从而协作异步地得到问题的解。每只蚂蚁的一步转移概率由图中的每条边上的两类参数决定:1. 信息素值也称信息素痕迹。2.可见度,即先验值。
2.信息素的更新方式有2种,一是挥发,也就是所有路径上的信息素以一定的比率进行减少,模拟自然蚁群的信息素随时间挥发的过程;二是增强,给评价值“好”(有蚂蚁走过)的边增加信息素。
3.蚂蚁向下一个目标的运动是通过一个随机原则来实现的,也就是运用当前所在节点存储的信息,计算出下一步可达节点的概率,并按此概率实现一步移动,逐此往复,越来越接近最优解。
4.蚂蚁在寻找过程中,或者找到一个解后,会评估该解或解的一部分的优化程度,并把评价信息保存在相关连接的信息素中。 

3.3 群蚁算法核心步骤

  更加我们前面的原理和上述说明,群蚁算法的2个核心步骤是 路径构建 和 信息素更新。我们将重点对这2个步骤进行说明。

3.3.1 路径构建

  每个蚂蚁都随机选择一个城市作为其出发城市,并维护一个路径记忆向量,用来存放该蚂蚁依次经过的城市。蚂蚁在构建路径的每一步中,按照一个随机比例规则选 择下一个要到达的城市。随机概率是按照下列公式来进行计算的:

  上述公式就是计算 当前点 到 每一个可能的下一个节点 的概率。分子是 信息素强度 和 能见度 的幂乘积,而分母则是所有 分子的和值。这个刚开始是很不容易理解的,我们在最后实例计算的时候,可以看得很清楚,再反过来理解公式。注意每次选择好节点后,就要从可用节点中移除选择的节点。

3.3.2 信息素更新

  信息素更新是群蚁算法的核心。也是整个算法的核心所在。算法在初始期间有一个固定的浓度值,在每一次迭代完成之后,所有出去的蚂蚁回来后,会对所走过的路线进行计算,然后更新相应的边的信息素浓度。很明显,这个数值肯定是和蚂蚁所走的长度有关系的,经过一次次的迭代, 近距离的线路的浓度会很高,从而得到近似最优解。那我们看看信息素更新的过程。

  初始化信息素浓度C(0),如果太小,算法容易早熟,蚂蚁会很快集中到一条局部最优路径上来,因为可以想想,太小C值,使得和每次挥发和增强的值都差不多,那么 随机情况下,一些小概率的事件发生就会增加非最优路径的信息素浓度;如果C太大,信息素对搜索方向的指导性作用减低,影响算法性能。一般情况下,我们可以使用贪婪算法获取一个路径值Cnn,然后根据蚂蚁个数来计算C(0) = m/Cnn ,m为蚂蚁个数

  每一轮过后,问题空间中的所有路径上的信息素都会发生蒸发,然后,所有的蚂蚁根据自己构建的路径长度在它们本轮经过的边上释放信息素,公式如下: 

  信息素更新的作用:
1.信息素挥发(evaporation)信息素痕迹的挥发过程是每个连接上的 信息素痕迹的浓度自动逐渐减弱的过程,这个挥发过程主要用于避 免算法过快地向局部最优区域集中,有助于搜索区域的扩展。
2.信息素增强(reinforcement)增强过程是蚁群优化算法中可选的部 分,称为离线更新方式(还有在线更新方式)。这种方式可以实现 由单个蚂蚁无法实现的集中行动。基本蚁群算法的离线更新方式是 在蚁群中的m只蚂蚁全部完成n城市的访问后,统一对残留信息进行 更新处理。

3.3.3 迭代与停止

  迭代停止的条件可以选择合适的迭代次数后停止,输出最优路径,也可以看是否满足指定最优条件,找到满足的解后停止。最重要的是,我刚开始理解这个算法的时候,以为每一只蚂蚁走一条边就是一次迭代,其实是错的。这里算法每一次迭代的意义是:每次迭代的m只蚂蚁都完成了自己的路径过程,回到原点后的整个过程。

回到目录

4.群蚁算法计算实例

  使用PPT中的一个案例,非常直观,对几个符号错误进行了修改,主要是计算概率的乘号,结果没有错误:

  过程总体还是比较简单的,注意理解公式,然后把公式和实例结合起来看,最好是拿笔自己手动画一画,容易理解。下面我们来看看如何编程实现TSP问题的群蚁算法代码。

function varargout = aca511(varargin)
% ACA511 M-file for aca511.fig
%      ACA511, by itself, creates a new ACA511 or raises the existing
%      singleton*.
%
%      H = ACA511 returns the handle to a new ACA511 or the handle to
%      the existing singleton*.
%
%      ACA511('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ACA511.M with the given input arguments.
%
%      ACA511('Property','Value',...) creates a new ACA511 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before aca511_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to aca511_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help aca511

% Last Modified by GUIDE v2.5 22-Oct-2011 19:55:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @aca511_OpeningFcn, ...
                   'gui_OutputFcn',  @aca511_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before aca511 is made visible.
function aca511_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to aca511 (see VARARGIN)
movegui(handles.figure1,'center');
load citys.txt%导入城市数据
global x y cnum%定义全局变量
x=citys(:,2);%城市横坐标
y=citys(:,3);%城市纵坐标
cnum=size(x,1);%城市数量
axes(handles.axes2);%选择第二个坐标轴
plot(x,y,'r*');%画图
hold on
for i=1:cnum
    text(x(i)+1,y(i),num2str(i));%标点
end
axes(handles.axes3);%选择第三个坐标轴
plot(x,y,'r*');%画图
hold on
for i=1:cnum
    text(x(i)+1,y(i),num2str(i));%标序号
end

% Choose default command line output for aca511
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes aca511 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = aca511_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% clc;
global x y cnum shortway
clc;
m=str2double(get(handles.antnum,'string'));%蚁群数量
alfap=str2double(get(handles.alfap,'string'));%
betap =str2double(get(handles.betap,'string'));%信息素增强
times=str2double(get(handles.times,'string'));%迭代次数
begininf=str2double(get(handles.begininf,'string'));%信息素初值
inflose=str2double(get(handles.inflose,'string'));%信息素挥发
infgain=str2double(get(handles.infgain,'string'));%信息素增加
mininf=str2double(get(handles.mininf,'string'));%信息素最小值
maxinf=str2double(get(handles.maxinf,'string'));%信息素最大值
limita=str2double(get(handles.limita,'string'));%a最小值
acoe=str2double(get(handles.acoe,'string'));%a倍数
bcoe=str2double(get(handles.bcoe,'string'));%b倍数
% set(handles.textrun,'string','Ants Running...');

[ reclong,shortway,minrelong ] = aca( m ,alfap ,betap ,times,begininf,limita,inflose,infgain,mininf,maxinf,acoe,bcoe);
set(handles.result,'string',minrelong);%最下路径
set(handles.shortway,'string',mat2str(shortway));%路线
% set(handles.textrun,'string','OK');
axes(handles.axes1);%坐标轴一
plot(reclong);%画图
axes(handles.axes2);%坐标轴二
hold off
plot(x,y,'r*');%画图
hold on
for i=1:cnum
    text(x(i)+1,y(i),num2str(i));%标序号
end
hold on%保持之前图
for i=1:cnum-1
      line([x(shortway(i)),x(shortway(i+1))],[y(shortway(i)),y(shortway(i+1))]);%划线
      hold on
end
      line([x(shortway(cnum)),x(shortway(1))],[y(shortway(cnum)),y(shortway(1))]);%划线





function antnum_Callback(hObject, eventdata, handles)
% hObject    handle to antnum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of antnum as text
%        str2double(get(hObject,'String')) returns contents of antnum as a double


% --- Executes during object creation, after setting all properties.
function antnum_CreateFcn(hObject, eventdata, handles)
% hObject    handle to antnum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function betap_Callback(hObject, eventdata, handles)
% hObject    handle to betap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of betap as text
%        str2double(get(hObject,'String')) returns contents of betap as a double


% --- Executes during object creation, after setting all properties.
function betap_CreateFcn(hObject, eventdata, handles)
% hObject    handle to betap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function times_Callback(hObject, eventdata, handles)
% hObject    handle to times (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of times as text
%        str2double(get(hObject,'String')) returns contents of times as a double


% --- Executes during object creation, after setting all properties.
function times_CreateFcn(hObject, eventdata, handles)
% hObject    handle to times (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function begininf_Callback(hObject, eventdata, handles)
% hObject    handle to begininf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of begininf as text
%        str2double(get(hObject,'String')) returns contents of begininf as a double


% --- Executes during object creation, after setting all properties.
function begininf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to begininf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function limita_Callback(hObject, eventdata, handles)
% hObject    handle to limita (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of limita as text
%        str2double(get(hObject,'String')) returns contents of limita as a double


% --- Executes during object creation, after setting all properties.
function limita_CreateFcn(hObject, eventdata, handles)
% hObject    handle to limita (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function inflose_Callback(hObject, eventdata, handles)
% hObject    handle to inflose (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of inflose as text
%        str2double(get(hObject,'String')) returns contents of inflose as a double


% --- Executes during object creation, after setting all properties.
function inflose_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inflose (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function infgain_Callback(hObject, eventdata, handles)
% hObject    handle to infgain (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of infgain as text
%        str2double(get(hObject,'String')) returns contents of infgain as a double


% --- Executes during object creation, after setting all properties.
function infgain_CreateFcn(hObject, eventdata, handles)
% hObject    handle to infgain (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function alfap_Callback(hObject, eventdata, handles)
% hObject    handle to alfap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of alfap as text
%        str2double(get(hObject,'String')) returns contents of alfap as a double


% --- Executes during object creation, after setting all properties.
function alfap_CreateFcn(hObject, eventdata, handles)
% hObject    handle to alfap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function result_Callback(hObject, eventdata, handles)
% hObject    handle to result (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of result as text
%        str2double(get(hObject,'String')) returns contents of result as a double


% --- Executes during object creation, after setting all properties.
function result_CreateFcn(hObject, eventdata, handles)
% hObject    handle to result (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function mininf_Callback(hObject, eventdata, handles)
% hObject    handle to mininf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of mininf as text
%        str2double(get(hObject,'String')) returns contents of mininf as a double


% --- Executes during object creation, after setting all properties.
function mininf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mininf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function maxinf_Callback(hObject, eventdata, handles)
% hObject    handle to maxinf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxinf as text
%        str2double(get(hObject,'String')) returns contents of maxinf as a double


% --- Executes during object creation, after setting all properties.
function maxinf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxinf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function shortway_Callback(hObject, eventdata, handles)
% hObject    handle to shortway (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of shortway as text
%        str2double(get(hObject,'String')) returns contents of shortway as a double


% --- Executes during object creation, after setting all properties.
function shortway_CreateFcn(hObject, eventdata, handles)
% hObject    handle to shortway (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function acoe_Callback(hObject, eventdata, handles)
% hObject    handle to acoe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of acoe as text
%        str2double(get(hObject,'String')) returns contents of acoe as a double


% --- Executes during object creation, after setting all properties.
function acoe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to acoe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function bcoe_Callback(hObject, eventdata, handles)
% hObject    handle to bcoe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bcoe as text
%        str2double(get(hObject,'String')) returns contents of bcoe as a double


% --- Executes during object creation, after setting all properties.
function bcoe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bcoe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global x y cnum shortway
shortway=cancelcross(shortway,x,y);%消除线段的交叉 做新图
axes(handles.axes3);%第三个轴
hold off%关闭保留之前的图像功能
plot(x,y,'b*');%划点
hold on%开启保留之前的图像功能
for i=1:cnum
    text(x(i)+1,y(i),num2str(i));%标序号
end
hold on
for i=1:cnum-1
      line([x(shortway(i)),x(shortway(i+1))],[y(shortway(i)),y(shortway(i+1))]);%划线
      hold on
end
      line([x(shortway(cnum)),x(shortway(1))],[y(shortway(cnum)),y(shortway(1))]);%划线
      newdis=countshortway( shortway,x,y );
set(handles.xiuresult,'string',newdis);%显示新的路径

function xiuresult_Callback(hObject, eventdata, handles)
% hObject    handle to xiuresult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xiuresult as text
%        str2double(get(hObject,'String')) returns contents of xiuresult as a double


% --- Executes during object creation, after setting all properties.
function xiuresult_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xiuresult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

 



这篇关于【TSP】基于蚁群算法求解TSP问题matlab源码含GUI的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程