【TSP】基於matlab GUI改進的遺傳演算法求解旅行商問題【含Matlab原始碼 926期】
一、簡介
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 = tsp_ga_gui(varargin)
% TSP_GA_GUI MATLAB code for tsp_ga_gui.fig
% TSP_GA_GUI, by itself, creates a new TSP_GA_GUI or raises the existing
% singleton*.
%
% H = TSP_GA_GUI returns the handle to a new TSP_GA_GUI or the handle to
% the existing singleton*.
%
% TSP_GA_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TSP_GA_GUI.M with the given input arguments.
%
% TSP_GA_GUI('Property','Value',...) creates a new TSP_GA_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tsp_ga_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tsp_ga_gui_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 tsp_ga_gui
% Last Modified by GUIDE v2.5 25-Feb-2021 15:15:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @tsp_ga_gui_OpeningFcn, ...
'gui_OutputFcn', @tsp_ga_gui_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 tsp_ga_gui is made visible.
function tsp_ga_gui_OpeningFcn(hObject, eventdata, handles, varargin)
global hnds
global r nn dsm asz G
global startf
% Choose default command line output for tsp_ga_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes tsp_ga_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
hnds=handles;
startf=false; % start flag
asz=10; % area size asx x asz
nn=str2num(get(handles.nn,'string')); % number of cities
ps=str2num(get(handles.ps,'string')); % population size
r=asz*rand(2,nn); % randomly distribute cities
% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities
dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
r1=r(:,n1);
for n2=n1+1:nn
r2=r(:,n2);
dr=r1-r2;
dr2=dr'*dr;
drl=sqrt(dr2);
dsm(n1,n2)=drl;
dsm(n2,n1)=drl;
end
end
% start from random closed pathes:
G=zeros(ps,nn); % genes, G(i,:) - gene of i-path, G(i,:) is row-vector with cities number in the path
for psc=1:ps
G(psc,:)=randperm(nn);
end
update_plots;
% --- Outputs from this function are returned to the command line.
function varargout = tsp_ga_gui_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 randomize.
function randomize_Callback(hObject, eventdata, handles)
global r nn dsm asz
nn=str2num(get(handles.nn,'string')); % number of cities
r=asz*rand(2,nn); % randomly distribute cities
% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities
dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
r1=r(:,n1);
for n2=n1+1:nn
r2=r(:,n2);
dr=r1-r2;
dr2=dr'*dr;
drl=sqrt(dr2);
dsm(n1,n2)=drl;
dsm(n2,n1)=drl;
end
end
update_plots;
% --- Executes on button press in circle.
function circle_Callback(hObject, eventdata, handles)
global r nn dsm asz
nn=str2num(get(handles.nn,'string')); % number of cities
r=zeros(2,nn);
% circle
al1=linspace(0,2*pi,nn+1);
al=al1(1:end-1);
r(1,:)=0.5*asz+0.45*asz*cos(al);
r(2,:)=0.5*asz+0.45*asz*sin(al);
% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities
dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
r1=r(:,n1);
for n2=n1+1:nn
r2=r(:,n2);
dr=r1-r2;
dr2=dr'*dr;
drl=sqrt(dr2);
dsm(n1,n2)=drl;
dsm(n2,n1)=drl;
end
end
update_plots;
function nn_Callback(hObject, eventdata, handles)
update_plots_nn_ps;
% --- Executes during object creation, after setting all properties.
function nn_CreateFcn(hObject, eventdata, handles)
% hObject handle to nn (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 ps_Callback(hObject, eventdata, handles)
update_plots_nn_ps;
% --- Executes during object creation, after setting all properties.
function ps_CreateFcn(hObject, eventdata, handles)
% hObject handle to ps (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
三、執行結果
四、備註
版本:2014a
完整程式碼或代寫加1564658423