A list,

Particle swarm Optimization (PSO) is an evolutionary computation technology. It comes from the study of predation behavior in flocks of birds. The basic idea of particle swarm optimization algorithm is to find the optimal solution through the cooperation and information sharing among individuals in the group. The advantage of PSO is that it is simple and easy to implement without many parameters adjustment. It has been widely used in function optimization, neural network training, fuzzy system control and other applications of genetic algorithms.

2. Analysis of particle swarm optimization

2.1 Basic Ideas

Particle swarm optimization (PSO) simulates a bird in a flock by designing a massless particle that has only two properties: speed and position. Speed represents how fast the bird moves, and position represents the direction of the bird. Each particle separately searches for the optimal solution in the search space, and records it as the current individual extreme value, and shares the individual extreme value with other particles in the whole particle swarm, and finds the optimal individual extreme value as the current global optimal solution of the whole particle swarm. All particles in a swarm adjust their speed and position based on the current individual extremum they find and the current global optimal solution shared by the whole swarm. The following GIF vividly shows the process of the PSO algorithm:



2 Update Rules

PSO initializes as a group of random particles (random solutions). Then find the optimal solution through iteration. At each iteration, the particle updates itself by tracking two “extreme values” (PBest, GBest). After finding these two optimal values, the particle updates its velocity and position by using the formula below.



The first part of formula (1) is called [memory term], which represents the influence of the magnitude and direction of the last speed. The second part of Formula (1) is called [self cognition term], which is a vector pointing from the current point to the particle’s own best point, indicating that the particle’s action comes from its own experience. The third part of Formula (1) is called [group cognition term], which is a vector from the current point to the best point of the population, reflecting the cooperation and knowledge sharing among particles. The particle is determined by its own experience and the best experience of its companions. Based on the above two formulas, the standard form of PSO is formed.



Formula (2) and Formula (3) are regarded as standard PSO algorithms.

3. Process and pseudocode of PSO algorithm

Ii. Source code

%该程序已在MATLAB2014a运行通过
 
function varargout = gui(varargin)
% GUI MATLAB code for gui.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to 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 gui
 
% Last Modified by GUIDE v2.5 13-Sep-2015 19:51:50
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @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 gui is made visible.
function gui_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 (see VARARGIN)
% Choose default command line output for gui
handles.output = hObject;
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = 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;
 
 
 
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
 
input = get(handles.edit1,'String'); 
input = str2num(input);
 
% --- 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 doubl
% --- Executes during object creation, after setting all properties.
input = get(handles.edit2,'String'); 
input = str2num(input);
 
 
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.
input = get(handles.edit3,'String'); 
input = str2num(input);
 
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
 
 
% --- 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)
C = 30;
theta = 2;%C为最小二乘支持向量机的正则化参数,theta为高斯径向基的核函数参数,两个需要进行优化选择调试
NumOfPre = 1;%预测天数,在此预测本季度最后七天
Time = 24;
Data = xlsread('a23.xls');%此为从excel表格读数据的命令,表示将表格的数据读到Data数组中,省略表格中的第一行第一列文字部分 可输入你要预测的表格名称
[M N] = size(Data);%计算读入数据的行和列 M行N列
for i = 1:3
    maxData = max(Data(:,i));
    minData = min(Data(:,i));
    Data1(:,i) = (Data(:,i) - minData)/(maxData-minData);%对温度进行归一化处理
end
for i = 4:5
    Data1(:,i) = Data(:,i);
end
for i = 6:N
    Data1(:,i) = log10(Data(:,i)) ;%对负荷进行对数处理 温度和负荷的预处理 可采用不同的方法 
end
Dim =  M - 2 - NumOfPre;%训练样本数%训练样本数
Input = zeros(M-2,12,Time);%预先分配处理后的输入向量空间
y = zeros(Dim,Time);
for i = 3:M 
    for j = 1:Time
        %%选取前一天温度、同一时刻的负荷,前两天的负荷,当天的温度作为输入特征
        x = [Data1(i-1,1:5), Data1(i-1,j+5), Data1(i-2,j+5),Data1(i,1:5)];
        Input(i-2,:,j) = x;
        y(i-2,j) = Data1(i,j+5);
    end
end
Dist = zeros(Dim,Dim,Time);%预先分配距离空间
for i=1:Time
    for j=1:Dim
        for k=1:Dim
            Dist(j,k,i) = (Input(j,:,i) - Input(k,:,i))*(Input(j,:,i) - Input(k,:,i))';
        end
    end
end
Dist1 = exp(-Dist/(2*theta));%RBF
for i=1:Time
    H = Dist1(:,:,i) + eye(Dim)/C;%最小二乘支持向量的H矩阵
    f = -y(1:Dim,i); 
    Aeq = ones(Dim,1)';
    beq = [0];
    option.MaxIter=1000;
    [a,fval]=quadprog(H,f,[],[],Aeq,beq);%,[],[],[],option);
    b = 0;
    for j = 1:Dim
        b(j) = y(j,i) - a(j)/C - a'* Dist1(:,j,i);%求每个输入特征对应的b
    end
    b = sum(b)/Dim;%求平均b,消除误差
    for j = Dim + 1:M-2
        for k = 1:Dim
            K(k) = exp(-(Input(j,:,i) - Input(k,:,i))*(Input(j,:,i) - Input(k,:,i))'/(2*theta));%预测输入特征与训练特征的RBF距离
        end
        Pre(j-Dim,i) = sum(a'*K') + b;  %求解预测值   
    end
end
Len = M  - (Dim + 3) + 1;%预测的天数 取本季度最后Len天
Pre = 10.^Pre;
for i = 1:Len
    %figure 
    axes(handles.axes1);
    plot(1:Time,Data(i+Dim+2,6:N),'-or',1:Time,Pre(i,:),'-vk');%画出每一天的预测值和真实值
    hold on
    scatter(1:Time,Data(i+Dim+2,6:N),'o')
    scatter(1:Time,Pre(i,:),'v')
    legend('实际值','预测值','location','southeast')
    hold off
end
Acu = (Pre - Data(Dim+3:M,6:N))./Data(Dim+3:M,6:N);%相对误差
save Acu.mat Acu
s=0;
for i=1:Time
    s=abs(Acu(1,i))+s;
end
acu=s/Time;
save acu.mat acu;
Result=[C,theta,acu];
disp(Result);
 
Copy the code

3. Operation results

Fourth, note

Complete code added QQ1575304183