1. 程式人生 > 其它 >【TSP】基於matlab GUI蟻群演算法求解旅行商問題【含Matlab原始碼 929期】

【TSP】基於matlab GUI蟻群演算法求解旅行商問題【含Matlab原始碼 929期】

一、簡介

1 蟻群演算法(ant colony algorithm,ACA)起源和發展歷程
Marco Dorigo等人在研究新型演算法的過程中,發現蟻群在尋找食物時,通過分泌一種稱為資訊素的生物激素交流覓食資訊從而能快速的找到目標,於是在1991年在其博士論文中首次系統地提出一種基於螞蟻種群的新型智慧優化演算法“螞蟻系統(Ant system,簡稱AS)”,後來,提出者及許多研究者對該演算法作了各種改進,將其應用於更為廣泛的領域,如圖著色問題、二次分配問題、工件排序問題、車輛路徑問題、車間作業排程問題、網路路由問題、大規模積體電路設計等。近些年來,M.Dorigo等人把螞蟻演算法進一步發展成一種通用的優化技術“蟻群優化(Ant Colony Optimization,簡稱ACO)”,並將所有符合ACO框架的演算法稱為“蟻群優化演算法(ACO algorithm)”。


具體來說,各個螞蟻在沒有事先告知食物在什麼地方的前提下開始尋找食物。當一隻找到食物以後,它會向環境釋放一種揮發性分泌物pheromone (稱為資訊素,該物質隨著時間的推移會逐漸揮發消失,資訊素濃度的大小表徵路徑的遠近)資訊素能夠讓其他螞蟻感知從而起到一個引導的作用。通常多個路徑上均有資訊素時,螞蟻會優先選擇資訊素濃度高的路徑,從而使濃度高的路徑資訊素濃度更高,形成一個正反饋。有些螞蟻並沒有像其它螞蟻一樣總重複同樣的路,他們會另闢蹊徑,如果另開闢的道路比原來的其他道路更短,那麼,漸漸地,更多的螞蟻被吸引到這條較短的路上來。最後,經過一段時間執行,可能會出現一條最短的路徑被大多數螞蟻重複著。最終,資訊素濃度最高的路徑即是最終被螞蟻選中的最優路徑。
與其他演算法相比,蟻群演算法是一種比較年輕的演算法,具有分散式計算、無中心控制、個體之間非同步間接通訊等特點,並且易於與其他優化演算法相結合,經過不少仁人志士的不斷探索,到今天已經發展出了各式各樣的改進蟻群演算法,不過蟻群演算法的原理仍是主幹。

2 蟻群演算法的求解原理
基於上述對蟻群覓食行為的描述,該演算法主要對覓食行為進行以下幾個方面模擬:
(1)模擬的圖場景中包含了兩種資訊素,一種表示家,一種表示食物的地點,並且這兩種資訊素都在以一定的速率進行揮發。
(2)每個螞蟻只能感知它周圍的小部分地方的資訊。螞蟻在尋找食物的時候,如果在感知範圍內,就可以直接過去,如果不在感知範圍內,就要朝著資訊素多的地方走,螞蟻可以有一個小概率不往資訊素多的地方走,而另闢蹊徑,這個小概率事件很重要,代表了一種找路的創新,對於找到更優的解很重要。
(3)螞蟻回窩的規則與找食物的規則相同。
(4)螞蟻在移動時候首先會根據資訊素的指引,如果沒有資訊素的指引,會按照自己的移動方向慣性走下去,但也有一定的機率改變方向,螞蟻還可以記住已經走過的路,避免重複走一個地方。
(5)螞蟻在找到食物時留下的資訊素最多,然後距離食物越遠的地方留下的資訊素越少。找到窩的資訊素留下的量的規則跟食物相同。蟻群演算法有以下幾個特點:正反饋演算法、併發性演算法、較強的魯棒性、概率型全域性搜尋、不依賴嚴格的數學性質、搜尋時間長,易出現停止現象。
螞蟻轉移概率公式:

公式中:是螞蟻k從城市i轉移到j的概率;α,β分別為資訊素和啟發式因子的相對重要程度;為邊(i,j)上的資訊素量;為啟發式因子;為螞蟻k下步允許選擇的城市。上述公式即為螞蟻系統中的資訊素更新公式,是邊(i,j)上的資訊素量;ρ是資訊素蒸發係數,0<ρ<1;為第k只螞蟻在本次迭代中留在邊(i,j)上的資訊素量;Q為一正常係數;為第k只螞蟻在本次周遊中的路徑長度。
在螞蟻系統中,資訊素更新公式為:

3 蟻群演算法的求解步驟:
(1)初始化引數在計算之初,需要對相關引數進行初始化,如蟻群規模(螞蟻數量)m、資訊素重要程度因子α、啟發函式重要程度因子β、資訊素會發銀子ρ、資訊素釋放總量Q、最大迭代次數iter_max、迭代次數初值iter=1。
(2)構建解空間將各個螞蟻隨機地置於不同的出發點,對每個螞蟻k(k=1,2,3…m),按照(2-1)計算其下一個待訪問城市,直到所有螞蟻訪問完所有城市。
(3)更新資訊蘇計算每個螞蟻經過路徑長度Lk(k=1,2,…,m),記錄當前迭代次數中的最優解(最短路徑)。同時,根據式(2-2)和(2-3)對各個城市連線路徑上資訊素濃度進行更新。
(4) 判斷是否終止若iter<iter_max,則令iter=iter+1,清空螞蟻經過路徑的記錄表,並返回步驟2;否則,終止計算,輸出最優解。
(5)判斷是否終止若iter<iter_max,則令iter=iter+1,清空螞蟻經過路徑的記錄表,並返回步驟2;否則,終止計算,輸出最優解。3. 判斷是否終止若iter<iter_max,則令iter=iter+1,清空螞蟻經過路徑的記錄表,並返回步驟2;否則,終止計算,輸出最優解。

二、原始碼

function varargout = TspACA(varargin)
%TSPACA M-file for TspACA.fig
%      TSPACA, by itself, creates a new TSPACA or raises the existing
%      singleton*.
%
%      H = TSPACA returns the handle to a new TSPACA or the handle to
%      the existing singleton*.
%
%      TSPACA('Property','Value',...) creates a new TSPACA using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to TspACA_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      TSPACA('CALLBACK') and TSPACA('CALLBACK',hObject,...) call the
%      local function named CALLBACK in TSPACA.M with the given input
%      arguments.
%
%      *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 TspACA

% Last Modified by GUIDE v2.5 18-Mar-2021 19:19:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @TspACA_OpeningFcn, ...
                   'gui_OutputFcn',  @TspACA_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 TspACA is made visible.
function TspACA_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

global AntsCod 
global bContinue
global Change
bContinue = 0;
Change =0;
CityNum = str2num(get(handles.CityNumber,'String'));
AntsCod =round(100*rand(CityNum,2));
%if MapIndex == 1
    %AntsCod(:,1) = (unidrnd(100,1,CityNum) - 1)';
    %AntsCod(:,2) = (unidrnd(100,1,CityNum) - 1)';
    plot(AntsCod(:,1),AntsCod(:,2),'mo','LineWidth',1.5,'MarkerEdgeColor','r','MarkerFaceColor','b','Parent',handles.CityMap);



% --- Outputs from this function are returned to the command line.
function varargout = TspACA_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;



function CityNumber_Callback(hObject, eventdata, handles)
% hObject    handle to CityNumber (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 CityNumber as text
%        str2double(get(hObject,'String')) returns contents of CityNumber as a double


% --- Executes during object creation, after setting all properties.
function CityNumber_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CityNumber (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 MaxIteration_Callback(hObject, eventdata, handles)
% hObject    handle to MaxIteration (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 MaxIteration as text
%        str2double(get(hObject,'String')) returns contents of MaxIteration as a double


% --- Executes during object creation, after setting all properties.
function MaxIteration_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxIteration (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 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 PhoromCoe_Callback(hObject, eventdata, handles)
% hObject    handle to PhoromCoe (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 PhoromCoe as text
%        str2double(get(hObject,'String')) returns contents of PhoromCoe as a double


% --- Executes during object creation, after setting all properties.
function PhoromCoe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PhoromCoe (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 PhoromEvap_Callback(hObject, eventdata, handles)
% hObject    handle to PhoromEvap (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 PhoromEvap as text
%        str2double(get(hObject,'String')) returns contents of PhoromEvap as a double


% --- Executes during object creation, after setting all properties.
function PhoromEvap_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PhoromEvap (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 Q_Callback(hObject, eventdata, handles)
% hObject    handle to Q (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 Q as text
%        str2double(get(hObject,'String')) returns contents of Q as a double


三、執行結果

四、備註

版本:2014a
完整程式碼或代寫加1564658423