一、粒子群算法簡介
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 = GUI_PSO(varargin)
% GUI_PSO MATLAB code for GUI_PSO.fig
% GUI_PSO, by itself, creates a new GUI_PSO or raises the existing
% singleton*.
%
% H = GUI_PSO returns the handle to a new GUI_PSO or the handle to
% the existing singleton*.
%
% GUI_PSO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_PSO.MyFirstText with the given input arguments.
%
% GUI_PSO('Property','Value',...) creates a new GUI_PSO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_PSO_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stopf. All inputs are passed to GUI_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 GUI_PSO
% Last Modified by GUIDE v2.5 20-May-2015 09:59:43
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_PSO_OpeningFcn, ...
'gui_OutputFcn', @GUI_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 GUI_PSO is made visible.
function GUI_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 GUI_PSO (see VARARGIN)
% Choose default command line output for GUI_PSO
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_PSO wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_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;
function popsize_Callback(hObject, eventdata, handles)
% hObject handle to popsize (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 popsize as text
% str2double(get(hObject,'String')) returns contents of popsize as a double
% --- Executes during object creation, after setting all properties.
function popsize_CreateFcn(hObject, eventdata, handles)
% hObject handle to popsize (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 stopf_Callback(hObject, eventdata, handles)
% hObject handle to stopf (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 stopf as text
% str2double(get(hObject,'String')) returns contents of stopf as a double
% --- Executes during object creation, after setting all properties.
function stopf_CreateFcn(hObject, eventdata, handles)
% hObject handle to stopf (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 c1_Callback(hObject, eventdata, handles)
% hObject handle to c1 (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 c1 as text
% str2double(get(hObject,'String')) returns contents of c1 as a double
% --- Executes during object creation, after setting all properties.
function c1_CreateFcn(hObject, eventdata, handles)
% hObject handle to c1 (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 c2_Callback(hObject, eventdata, handles)
% hObject handle to c2 (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 c2 as text
% str2double(get(hObject,'String')) returns contents of c2 as a double
% --- Executes during object creation, after setting all properties.
function c2_CreateFcn(hObject, eventdata, handles)
% hObject handle to c2 (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 w1_Callback(hObject, eventdata, handles)
% hObject handle to w1 (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 w1 as text
% str2double(get(hObject,'String')) returns contents of w1 as a double
% --- Executes during object creation, after setting all properties.
function w1_CreateFcn(hObject, eventdata, handles)
% hObject handle to w1 (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
三、運作結果
四、matlab版本及參考文獻
1 matlab版本
2014a
2 參考文獻
[1] 包子陽,餘繼周,楊杉.智能優化算法及其MATLAB執行個體(第2版)[M].電子工業出版社,2016.
[2]張岩,吳水根.MATLAB優化算法源代碼[M].清華大學出版社,2017.