[BP prediction] Optimize BP prediction matlab source code based on Sparrow algorithm

1. Introduction

  1. Introduction to the sparrow search algorithm
    (the following descriptions are not academic terms and are only for your happy reading) The
      sparrow search algorithm is a swarm intelligence optimization algorithm proposed based on the behavior of sparrows foraging and evading predators. The proposed time is 2020, that is, this year. This is a new and hot new algorithm. Related papers and research are still relatively few. There may be some students who are in the process of publishing and need papers due to the epidemic.
      The sparrow search algorithm mainly simulates the foraging process of sparrows. The foraging process of the sparrow group is also a kind of discoverer-follower model, and the detection and early warning mechanism is also superimposed. Among sparrows, individuals with better food are found as discoverers, and other individuals are followers. At the same time, a certain proportion of individuals in the population are selected for detection and early warning. If a danger is found, food is abandoned, safety first.
      The specific implementation of the sparrow search algorithm is actually very similar to the artificial bee colony algorithm, and the basic structure is almost the same, but the search operator has certain differences, which can be said to be an improved algorithm of the artificial bee colony algorithm.
      There are relatively few papers related to the sparrow search algorithm. I only read the original papers. The description of the algorithm is more detailed. However, it can be seen that the papers are arranged in a hurry, and some formulas are too complicated and affect understanding. Below I will simplify some of the formulas based on my own understanding. If there is something wrong, please leave a message.

  2. Algorithm flow
    This time our protagonist is a group of sparrows.
    Insert picture description here
     Although a sparrow is small and complete, each sparrow has only one attribute: location, which represents the location of the food it finds. Each sparrow has three possible behaviors: 1. As a discoverer, continue to search for food, 2. As a follower, follow a discoverer for food, 3. Beware of investigation, and give up food when in danger.
      The position of each sparrow in the D-dimensional solution space is the fitness value.
      There are N sparrows in this group of sparrows. Each generation selects the best PN sparrow in the population as the discoverer, and the remaining N-PN sparrows as the follower.
    2.1 Update the position
    of the discoverer The position update formula of each generation of the discoverer is as follows: The
    Insert picture description here
    above picture is the description of the formula in the text, which is somewhat inaccurate. A more precise description is as follows:
    Insert picture description here
     which represents the d-dimensional position of the i-th individual in the t-th generation , Is a uniform random number in (0,1], Q is a standard normal distribution random number. is a uniform random number in [0,1], ST is the warning threshold, and the value range is [0.5,1.0].
      It can be seen that when it is greater than ST, the discoverer will randomly move to the vicinity of the current position according to a normal distribution. (its value converges to the optimal position).
      What happens when it is less than ST, let's take a look at the function image first , Where the value is 1000. It
    Insert picture description here
    can be seen that as the distribution of x becomes larger, the value range gradually decreases from (0,1) to approximately (0,0.4).
    When the value of X is small, the value is close to 1. The probability of is higher. As X increases, the distribution of values ​​becomes more uniform.
    So when it is smaller than ST, each dimension of the sparrow becomes smaller. Of course, this is not a good strategy. (The value converges to 0 ).

2.2 Update the position
of the follower In the text, the update formula and description of the position of the follower are as follows:
Insert picture description here
First, let’s look at a matrix of size 1*D (1 row and D column), each dimension of which is randomly selected from {-1,1 } To select.
  For a simple example, suppose that X=(x1,x2,x3)=(1,2,3),
Insert picture description here
 because it is a row vector and matrix operation, so the formula j should not appear on both sides of the formula in the text, it means that if the formula j appears The variable is a number, not a vector.
  Simplified, the position update formula is as follows:
Insert picture description here
 where xw is the worst position of the sparrow in the current population, and xb is the most position of the sparrow in the population.
  It can be seen from formula (2) that if i>n/2, its value is the product of a standard normal distribution random number and an exponential function with a natural logarithm as the base. When the population converges, its value conforms to Standard normal distribution random number. (Its value will converge to 0).
  If i<=n/2, its value is the current optimal sparrow's position plus the random addition and subtraction of each dimension of the sparrow's distance from the optimal position, and the sum is divided equally into each dimension. The explanation is a bit convoluted, please understand the formula by yourself. This process can be described as randomly finding a position near the current optimal position, and the variance of each dimension from the optimal position will become smaller, that is, it will not appear in a certain dimension that is very different from the optimal position. The other positions are slightly different. (Its value converges to the optimal position).

2.3 Detection and early warning behavior
When sparrows are foraging for food, some of them will be responsible for alerting. When danger approaches, they will give up the current food, that is, whether the sparrow is a discoverer or a follower, they will give up the current food and move to A new location.
  Each generation will randomly select SD individuals from the population for early warning behavior.
The location update formula is as follows:
Insert picture description here

Among them is a random number conforming to the standard normal distribution, and K is a uniform random number of [-1,1], which is a small number to prevent the denominator from being unique. Compared with the formula in the original text, I removed the original absolute value sign, because the two random numbers are symmetric about the origin center, and the increased absolute value of the denominator prevents the denominator from taking the value 0. Among them is the fitness value of the sparrow in the worst position.
  It can be seen from formula (3) that if the pre-warned sparrow is in the current optimal position, it will flee to a position near itself. The ratio of the difference in poor food; if the sparrow is not in the optimal position, it will escape to the vicinity of the current optimal position. (Its value converges to the optimal position).
The flow chart of the sparrow search algorithm is as follows:
Insert picture description here
Flow chart    It
  can be seen that the flow of the sparrow search algorithm is very simple. However, according to the analysis of the three update formulas above, it can be seen that the update method of the sparrow search algorithm can be roughly divided into two types: 1. Approaching the current optimal position; 2. Approaching the origin.
  Using these two methods to update the position of the sparrow will make the algorithm easy to fall into a local optimum. My intuition tells me that an algorithm with such an update rule is not an excellent algorithm. The specific problems are detailed in the following experiments.

Second, the source code

% 清空环境
clc
clear

%读取数据
load data input output

%节点个数
inputnum=2;
hiddennum=5;
outputnum=1;

%训练数据和预测数据
input_train=input(1:1900,:)';
input_test=input(1901:2000,:)';
output_train=output(1:1900)';
output_test=output(1901:2000)';

%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);

%构建网络
net=newff(inputn,outputn,hiddennum);

% 参数初始化
dim=21;
maxgen=30;   % 进化次数  
sizepop=20;   %种群规模

popmax=5;
popmin=-5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 迭代寻优
x=bestX
%% 结果分析
plot(yy)
title(['适应度曲线  ' '终止代数=' num2str(maxgen)]);
xlabel('进化代数');ylabel('适应度');
%% 把最优初始阀值权值赋予网络预测
% %用麻雀搜索算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);

net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2;

%% 训练
%网络进化参数
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00001;

%网络训练
[net,tr]=train(net,inputn,outputn);

%%预测
%数据归一化
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);
test_simu=mapminmax('reverse',an,outputps);
error=test_simu-output_test;
figure(2)
plot(error)
title('仿真预测误差','fontsize',12);
xlabel('仿真次数','fontsize',12);ylabel('误差百分值','fontsize',12);

 

Complete code or write on behalf of adding QQ1575304183

Past review>>>>>>

[SVM prediction] gray wolf algorithm optimization svm support vector machine prediction matlab source code

[SVM prediction] SVM prediction based on the improved bat algorithm

[ELM prediction] Particle swarm optimization ELM network prediction

[Lssvm prediction] lssvm data prediction matlab source code based on whale optimization algorithm

[Lssvm prediction model] based on bat algorithm improved least squares support vector machine lssvm prediction

[Lssvm prediction] based on the moth fire fighting algorithm improved least squares support vector machine lssvm prediction

[Lstm prediction] based on whale optimization algorithm improved lstm prediction matlab source code

[BP prediction model] BP neural network prediction

[BP prediction] BP neural network based on sparrow optimization

[ANN prediction model] based on the difference algorithm to improve the ANN network prediction matlab source code

[SVM prediction] Matlab source code for stock prediction based on SVM

Guess you like

Origin blog.csdn.net/qq_34763204/article/details/113666525