1. 程式人生 > 其它 >【MTSP】基於matlab GUI遺傳演算法求解多旅行商問題【含Matlab原始碼 935期】

【MTSP】基於matlab GUI遺傳演算法求解多旅行商問題【含Matlab原始碼 935期】

一、簡介

1 遺傳演算法概述
遺傳演算法(Genetic Algorithm,GA)是進化計算的一部分,是模擬達爾文的遺傳選擇和自然淘汰的生物進化過程的計算模型,是一種通過模擬自然進化過程搜尋最優解的方法。該演算法簡單、通用,魯棒性強,適於並行處理。

2 遺傳演算法的特點和應用
遺傳演算法是一類可用於複雜系統優化的具有魯棒性的搜尋演算法,與傳統的優化演算法相比,具有以下特點:
(1)以決策變數的編碼作為運算物件。傳統的優化演算法往往直接利用決策變數的實際值本身來進行優化計算,但遺傳演算法是使用決策變數的某種形式的編碼作為運算物件。這種對決策變數的編碼處理方式,使得我們在優化計算中可借鑑生物學中染色體和基因等概念,可以模仿自然界中生物的遺傳和進化激勵,也可以很方便地應用遺傳操作運算元。
(2)直接以適應度作為搜尋資訊。傳統的優化演算法不僅需要利用目標函式值,而且搜尋過程往往受目標函式的連續性約束,有可能還需要滿足“目標函式的導數必須存在”的要求以確定搜尋方向。遺傳演算法僅使用由目標函式值變換來的適應度函式值就可確定進一步的搜尋範圍,無需目標函式的導數值等其他輔助資訊。直接利用目標函式值或個體適應度值也可以將搜尋範圍集中到適應度較高部分的搜尋空間中,從而提高搜尋效率。
(3)使用多個點的搜尋資訊,具有隱含並行性。傳統的優化演算法往往是從解空間的一個初始點開始最優解的迭代搜尋過程。單個點所提供的搜尋資訊不多,所以搜尋效率不高,還有可能陷入區域性最優解而停滯;遺傳演算法從由很多個體組成的初始種群開始最優解的搜尋過程,而不是從單個個體開始搜尋。對初始群體進行的、選擇、交叉、變異等運算,產生出新一代群體,其中包括了許多群體資訊。這些資訊可以避免搜尋一些不必要的點,從而避免陷入區域性最優,逐步逼近全域性最優解。
(4) 使用概率搜尋而非確定性規則。傳統的優化演算法往往使用確定性的搜尋方法,一個搜尋點到另一個搜尋點的轉移有確定的轉移方向和轉移關係,這種確定性可能使得搜尋達不到最優店,限制了演算法的應用範圍。遺傳演算法是一種自適應搜尋技術,其選擇、交叉、變異等運算都是以一種概率方式進行的,增加了搜尋過程的靈活性,而且能以較大概率收斂於最優解,具有較好的全域性優化求解能力。但,交叉概率、變異概率等引數也會影響演算法的搜尋結果和搜尋效率,所以如何選擇遺傳演算法的引數在其應用中是一個比較重要的問題。
綜上,由於遺傳演算法的整體搜尋策略和優化搜尋方式在計算時不依賴於梯度資訊或其他輔助知識,只需要求解影響搜尋方向的目標函式和相應的適應度函式,所以遺傳演算法提供了一種求解複雜系統問題的通用框架。它不依賴於問題的具體領域,對問題的種類有很強的魯棒性,所以廣泛應用於各種領域,包括:函式優化、組合優化生產排程問題、自動控制
、機器人學、影象處理(影象恢復、影象邊緣特徵提取......)、人工生命、遺傳程式設計、機器學習。

3 遺傳演算法的基本流程及實現技術
基本遺傳演算法(Simple Genetic Algorithms,SGA)只使用選擇運算元、交叉運算元和變異運算元這三種遺傳運算元,進化過程簡單,是其他遺傳演算法的基礎。

3.1 遺傳演算法的基本流程
通過隨機方式產生若干由確定長度(長度與待求解問題的精度有關)編碼的初始群體;
通過適應度函式對每個個體進行評價,選擇適應度值高的個體參與遺傳操作,適應度低的個體被淘汰;
經遺傳操作(複製、交叉、變異)的個體集合形成新一代種群,直到滿足停止準則(進化代數GEN>=?);
將後代中變現最好的個體作為遺傳演算法的執行結果。

其中,GEN是當前代數;M是種群規模,i代表種群數量。

3.2 遺傳演算法的實現技術
基本遺傳演算法(SGA)由編碼、適應度函式、遺傳運算元(選擇、交叉、變異)及執行引數組成。
3.2.1 編碼
(1)二進位制編碼
二進位制編碼的字串長度與問題所求解的精度有關。需要保證所求解空間內的每一個個體都可以被編碼。
優點:編、解碼操作簡單,遺傳、交叉便於實現
缺點:長度大
(2)其他編碼方法
格雷碼、浮點數編碼、符號編碼、多引數編碼等
3.2.2 適應度函式
適應度函式要有效反映每一個染色體與問題的最優解染色體之間的差距。
3.2.3選擇運算元

3.2.4 交叉運算元
交叉運算是指對兩個相互配對的染色體按某種方式相互交換其部分基因,從而形成兩個新的個體;交叉運算是遺傳演算法區別於其他進化演算法的重要特徵,是產生新個體的主要方法。在交叉之前需要將群體中的個體進行配對,一般採取隨機配對原則。
常用的交叉方式:
單點交叉
雙點交叉(多點交叉,交叉點數越多,個體的結構被破壞的可能性越大,一般不採用多點交叉的方式)
均勻交叉
算術交叉
3.2.5 變異運算元
遺傳演算法中的變異運算是指將個體染色體編碼串中的某些基因座上的基因值用該基因座的其他等位基因來替換,從而形成一個新的個體。

就遺傳演算法運算過程中產生新個體的能力方面來說,交叉運算是產生新個體的主要方法,它決定了遺傳演算法的全域性搜尋能力;而變異運算只是產生新個體的輔助方法,但也是必不可少的一個運算步驟,它決定了遺傳演算法的區域性搜尋能力。交叉運算元與變異運算元的共同配合完成了其對搜尋空間的全域性搜尋和區域性搜尋,從而使遺傳演算法能以良好的搜尋效能完成最優化問題的尋優過程。

3.2.6 執行引數

4 遺傳演算法的基本原理
4.1 模式定理

4.2 積木塊假設
具有低階、定義長度短,且適應度值高於群體平均適應度值的模式稱為基因塊或積木塊。
積木塊假設:個體的基因塊通過選擇、交叉、變異等遺傳運算元的作用,能夠相互拼接在一起,形成適應度更高的個體編碼串。
積木塊假設說明了用遺傳演算法求解各類問題的基本思想,即通過積木塊直接相互拼接在一起能夠產生更好的解。

二、原始碼

function varargout = guimtsp(varargin)
% GUIMTSP MATLAB code for guimtsp.fig
%      GUIMTSP, by itself, creates a new GUIMTSP or raises the existing
%      singleton*.
%
%      H = GUIMTSP returns the handle to a new GUIMTSP or the handle to
%      the existing singleton*.
%
%      GUIMTSP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIMTSP.M with the given input arguments.
%
%      GUIMTSP('Property','Value',...) creates a new GUIMTSP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guimtsp_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guimtsp_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 guimtsp

% Last Modified by GUIDE v2.5 28-Dec-2020 12:21:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guimtsp_OpeningFcn, ...
                   'gui_OutputFcn',  @guimtsp_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 guimtsp is made visible.
function guimtsp_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 guimtsp (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = guimtsp_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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


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


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


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


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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%  主函式
% 輸入:
% XY:各個城市座標的 N*2 矩陣,N 為城市的個數
% DMAT:各個城市之間的距離矩陣
% SALESMEN :旅行商人數
% MIN_TOUR:每個人所經過的最少城市點
% POP_SIZE:種群大小
% NUM_ITER:迭代次數
% 輸出:
% 最優路線
% 總距離

三、執行結果



四、備註

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