【TSP】基於matlab GUI混合粒子群演算法求解旅行商問題【含Matlab原始碼 925期】
一、簡介
1 粒子群演算法的概念
粒子群優化演算法(PSO:Particle swarm optimization) 是一種進化計算技術(evolutionary computation)。源於對鳥群捕食的行為研究。粒子群優化演算法的基本思想:是通過群體中個體之間的協作和資訊共享來尋找最優解.
PSO的優勢:在於簡單容易實現並且沒有許多引數的調節。目前已被廣泛應用於函式優化、神經網路訓練、模糊系統控制以及其他遺傳演算法的應用領域。
2 粒子群演算法分析
2.1基本思想
粒子群演算法通過設計一種無質量的粒子來模擬鳥群中的鳥,粒子僅具有兩個屬性:速度和位置,速度代表移動的快慢,位置代表移動的方向。每個粒子在搜尋空間中單獨的搜尋最優解,並將其記為當前個體極值,並將個體極值與整個粒子群裡的其他粒子共享,找到最優的那個個體極值作為整個粒子群的當前全域性最優解,粒子群中的所有粒子根據自己找到的當前個體極值和整個粒子群共享的當前全域性最優解來調整自己的速度和位置。下面的動圖很形象地展示了PSO演算法的過程:
2 更新規則
PSO初始化為一群隨機粒子(隨機解)。然後通過迭代找到最優解。在每一次的迭代中,粒子通過跟蹤兩個“極值”(pbest,gbest)來更新自己。在找到這兩個最優值後,粒子通過下面的公式來更新自己的速度和位置。
公式(1)的第一部分稱為【記憶項】,表示上次速度大小和方向的影響;公式(1)的第二部分稱為【自身認知項】,是從當前點指向粒子自身最好點的一個向量,表示粒子的動作來源於自己經驗的部分;公式(1)的第三部分稱為【群體認知項】,是一個從當前點指向種群最好點的向量,反映了粒子間的協同合作和知識共享。粒子就是通過自己的經驗和同伴中最好的經驗來決定下一步的運動。以上面兩個公式為基礎,形成了PSO的標準形式。
公式(2)和 公式(3)被視為標準PSO演算法。
3 PSO演算法的流程和虛擬碼
二、原始碼
function varargout = PSO(varargin) % PSO M-file for PSO.fig % PSO, by itself, creates a new PSO or raises the existing % singleton*. % % H = PSO returns the handle to a new PSO or the handle to % the existing singleton*. % % PSO('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in PSO.M with the given input arguments. % % PSO('Property','Value',...) creates a new PSO or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before PSO_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to PSO_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 PSO % Last Modified by GUIDE v2.5 12-Jun-2020 22:11:08 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @PSO_OpeningFcn, ... 'gui_OutputFcn', @PSO_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 PSO is made visible. function PSO_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 PSO (see VARARGIN) % Choose default command line output for PSO handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes PSO wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = PSO_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 run. function run_Callback(hObject, eventdata, handles) % hObject handle to run (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) TSP_type = get(findobj('tag','tsp'),'Value'); switch TSP_type case 1 data=load('burma14.txt'); case 2 data=load('ulysses22.txt'); case 3 data=load('bayg29.txt'); case 4 data=load('Oliver30.txt'); case 5 data=load('eil51.txt'); case 6 data=load('st70.txt'); case 7 data=load('pr76.txt'); case 8 data=load('gr96.txt'); case 9 data=load('ch130.txt'); case 10 data=load('ch150.txt'); case 11 data=load('pr226.txt'); end a=data(:,2); b=data(:,3); C=[a b]; %城市座標矩陣 n=size(C,1); %城市數目 D=zeros(n,n); %城市距離矩陣 %L_best=ones(Nmax,1); for i=1:n for j=1:n if i~=j D(i,j)=((C(i,1)-C(j,1))^2+(C(i,2)-C(j,2))^2)^0.5; end D(j,i)=D(i,j); end end Nmax=str2double(get(findobj('tag','N_max'),'string')); m=str2double(get(findobj('tag','m'),'string')); algo_type = get(findobj('tag','algo'),'Value'); switch algo_type case 1 %% 初始化所有粒子 for i=1:m x(i,:)=randperm(n); %粒子位置 end F=fitness(x,C,D); %計算種群適應度 %xuhao=xulie(F) %最小適應度種群序號 a1=F(1); a2=1; for i=1:m if a1>=F(i) a1=F(i); a2=i; end end xuhao=a2; Tour_pbest=x; %當前個體最優 Tour_gbest=x(xuhao,:) ; %當前全域性最優路徑 Pb=inf*ones(1,m); %個體最優記錄 Gb=F(a2); %群體最優記錄 xnew1=x; N=1; while N<=Nmax %計算適應度 F=fitness(x,C,D); for i=1:m if F(i)<Pb(i) Pb(i)=F(i); %將當前值賦給新的最佳值 Tour_pbest(i,:)=x(i,:);%將當前路徑賦給個體最優路徑 end if F(i)<Gb Gb=F(i); Tour_gbest=x(i,:); end end % nummin=xulie(Pb) %最小適應度種群序號 a1=Pb(1); a2=1; for i=1:m if a1>=Pb(i) a1=Pb(i); a2=i; end end nummin=a2; Gb(N)=Pb(nummin); %當前群體最優長度 for i=1:m %% 與個體最優進行交叉 c1=round(rand*(n-2))+1; %在[1,n-1]範圍內隨機產生一個交叉位 c2=round(rand*(n-2))+1; while c1==c2 c1=round(rand*(n-2))+1; %在[1,n-1]範圍內隨機產生一個交叉位 c2=round(rand*(n-2))+1; end chb1=min(c1,c2); chb2=max(c1,c2); cros=Tour_pbest(i,chb1:chb2); %交叉區域矩陣 ncros=size(cros,2); %交叉區域元素個數 %刪除與交叉區域相同元素 for j=1:ncros for k=1:n if xnew1(i,k)==cros(j) xnew1(i,k)=0; for t=1:n-k temp=xnew1(i,k+t-1); xnew1(i,k+t-1)=xnew1(i,k+t); xnew1(i,k+t)=temp; end end end end xnew=xnew1; %插入交叉區域 for j=1:ncros xnew1(i,n-ncros+j)=cros(j); end %判斷產生新路徑長度是否變短 dist=0; for j=1:n-1 dist=dist+D(xnew1(i,j),xnew1(i,j+1)); end dist=dist+D(xnew1(i,1),xnew1(i,n)); if F(i)>dist x(i,:)=xnew1(i,:); end
三、執行結果
四、備註
版本:2014a
完整程式碼或代寫加1564658423