[Grain temperature prediction] Genetic algorithm optimization of BP neural network GA-BP grain temperature prediction research (multiple inputs and single output) (including comparison before optimization) [Including Matlab source code 2404]

?1. Genetic algorithm optimization of BP neural network for grain temperature prediction

Abstract
Aiming at the nonlinear time series problem of temperature parameters in grain storage, a grain temperature prediction model based on genetic algorithm (GA) optimized BP neural network algorithm was proposed, and 10 factors affecting grain temperature were selected (external temperature, external humidity, Warehouse top temperature, warehouse center temperature, warehouse bottom temperature, warehouse top humidity, warehouse center humidity, warehouse bottom humidity, warehouse oxygen concentration, grain humidity) are used as input parameters, and the grain temperature is output after analysis. It has been verified that the GA-BP model has better prediction accuracy and practical effects than the traditional BP neural network, and has certain application prospects in the field of grain temperature prediction.

One of the most important safety indicators of food is the temperature and humidity of food. Excessive temperature and humidity will lead to mildew of food [1]. At present, national grain depots at all levels can only be limited to real-time monitoring and analysis of grain data, and grain temperature information and its changing trends can only rely on subjective judgments by experienced staff [2]. Therefore, if there is a The model can reasonably predict changes in grain temperature, which is of great significance to food security.

In recent years, the use of BP neural network to predict grain temperature has solved many problems that traditional methods cannot solve. However, it also has its own shortcomings. For example, the accuracy of prediction needs to be improved, but adding genetic algorithm (GA) can greatly improve the accuracy of prediction. accuracy, achieving good prediction results [3].

This article uses genetic algorithm to optimize the BP neural network model. After processing the data, set the relevant parameters and number of hidden layers, build the GA-BP model, and propose the corresponding model algorithm until the best model is trained. At the same time, the BP prediction model is used as a comparison, and the fitting effects of different model data predictions are evaluated.

1 Genetic algorithm optimizes BP neural network
1.1 BP neural network (back propagation)

BP network is a nonlinear system, and its high degree of adaptability is its most significant feature. This algorithm takes n samples X=(x1,x2,…,xj,…,xn) of the research object as the input layer nodes of the neural network, and the expected result Y=(y1,y2,…,yj,…,yn) as The corresponding output node calculates the weight and threshold, and calculates the error value between the actual result and the predicted result, see equation (1). The fitness function is a standard that measures whether the error value meets the requirements. For calculation results that do not meet the requirements, the network will perform error backpropagation. Equation (2) is the correction amount of the hidden layer and the output layer. Through repeated iterations, if the error If the expectations are met, the model is successfully established.

In the formula: yi (n) is the expected result; y′i (n) is the predicted result; eta is the learning rate; E is the output value of the hidden layer; Wkj is the output value of the node in the output layer; ΔWkj is the correction amount.

1.2 Genetic algorithm
The basic idea of the genetic algorithm is that a population that needs to be optimized evolves generation by generation based on the survival of the fittest, and the evolved population with better fitness is retained and passed on to the next generation. In this process Crossover and mutation operations are also needed to generate new populations with better fitness. The optimization process is shown in Figure 2.

Figure 2 Schematic flow chart of genetic algorithm optimization BP neural network algorithm

1.2.1 Chromosome coding
The encoding rules use floating-point encoding, which on the one hand can obtain higher-precision network weights and thresholds, and on the other hand can also obtain a wider search range than before [4]. Its length s is shown in Equation (3).

s=n1×n2 + n2×n3 + n2 + n3 (3)

In the formula: n1, n2, n3 are the number of neurons in the input layer, hidden layer and output layer.

1.2.2 Fitness function
In the process of training the BP neural network, the output of the network and the expected output yˉ(k) are selected as training samples, y(k) is used as the output generated after training, and the weights and thresholds after training are used as the selected weights and Threshold, the indicator of the genetic algorithm is used as the indicator of the optimized algorithm [5], and the sum of squared errors between the predicted value and the actual value is calculated, see equation (4).

Among them: k is the logarithm of the input and output sampling data, N is the number of network output nodes, yˉ(k) is the expected output value of the k-th node, and y(k) is the predicted output value of the k-th node. In order to avoid the division by zero phenomenon, a value ζ approaching zero is introduced here, and the fitness function is shown in equation (5).

1.2.3 Selection
Assume the population size is n, fi is the fitness, and this fitness is for individual i in the population, then the probability of i being selected is shown in Equation (6).

1.2.4 Crossover
The method of arithmetic crossover is commonly used in floating-point crossover individuals. Assume that arithmetic crossover is performed between individuals xtA and xtB. The new individual [6] generated after the operation is shown in equation (7).

In the formula: α is a random number uniformly distributed in the interval [0,1].

1.2.5 Mutation
In order to prevent the occurrence of premature phenomena, based on a small range of random numbers, genetic mutations occur in a small range of probability. The local search ability of the algorithm will also be strengthened in this step. At this time, assuming that the maximum and minimum values of the initial individuals of the population are xmax and xmin respectively, then the mutated gene [7] is shown in equation (8).

xk=xmin + β(xmin + xmax) (8)

In the formula: β is a random number uniformly distributed in the interval [0,1].

2 Establishment of grain temperature prediction model
2.1 Data normalization processing

There are many factors that affect grain temperature, and different factors have different effects on changes in grain temperature. Based on 240 pieces of rice data from a granary in Tianjin in September 2020, 10 input indicators were selected [8]. In order to improve the prediction accuracy, the time period is compressed as much as possible during the data collection process. Data collection is performed every 3 hours in 1 day. The temperature of the grain is used as the output of the model. The training sample data is shown in Table 1.

The entire data is divided into test samples and training samples. The former selects the first 200 copies of the 240 data, and the latter takes the remaining 40 copies. Since the differences in magnitude between all collected data are large, it will have a direct impact on the output of network training. Or indirect influence, therefore, normalizing the data can reduce its influence very well. Let the originally input larger value still fall into the position where the function gradient is larger, which can improve the convergence speed of the model. Assuming that X={Xi} data is normalized to X′={X′i}, the formula is shown in Equation (9).

2.2 Establishment of model parameters
2.2.1 Determination of basic parameters
2.2.1.1 Learning rate

Choosing a learning rate that is too large or too small will have a negative impact on the model. If the learning rate is too large, the system will experience irregular oscillations. When the learning rate is small, the model training time will be significantly longer than before. The final learning rate is determined to be LP.lr=0.01.

2.2.1.2 Target accuracy
The accuracy of model training also has an important relationship with it. According to the corresponding relationship obtained in literature [10], it can be seen that the training time increases as the target accuracy increases. Trial calculation is currently the main method to obtain accuracy. The target accuracy of this article is set to 0.005.

2.2.1.3 Crossover probability
The crossover probability of the genetic algorithm falls between 0 and 1. This article chooses 0.15 as the crossover probability.

2.2.1.4 Mutation probability
In order to ensure the stability of the system, the mutation probability of the algorithm usually falls between 0 and 0.1. This article chooses 0.05 as the mutation probability.

2.2.2 Optimization of genetic algorithm
It can be seen from Figure 3 that the selection algebra has completely converged near the 50th generation, and the error between the output result at this time and the actual value of the actual output result is getting smaller and smaller, indicating that this model has better convergence, and this The model structure is also the most reasonable.

2.2.3 Online training
It can be seen from Figure 4 that although the prediction trends of the two models are relatively consistent, there is also a considerable gap. The curve fitted by the optimized model is closer to the actual curve than the unoptimized curve.

3 Conclusion
This paper proposes a GA-BP grain storage temperature prediction model and conducts simulation verification based on actual rice data in a granary in Tianjin. The results show that the GA-BP model is significantly better than the traditional BP neural network model in terms of prediction accuracy and practical effect, and has higher superiority. However, in future practical applications, more practical issues need to be considered. For example, in regions further south or north, classification needs to be refined to improve the fitness and accuracy of the model.

?2. Part of the source code

%% initialization
clear
close all
clc
warning off

%% read read
data=xlsread(Table 1 Neural Network Training Sample.xls’, Sheet1’, B3:L82’); %%Use the xlsread function to read the corresponding range of data in EXCEL

%Input and output data
input=data(:,1:end-1); The first column to the penultimate column of ?ta are the characteristic indicators
output=data(:,end); The last column of ?ta is the output indicator value

N=length(output); %number of all samples
testNum=20; %Set the number of test samples
trainNum=N-testNum; % calculate the number of training samples

%% Divide training set and test set
input_train = input(1:trainNum,:);
output_train =output(1:trainNum)’;
input_test =input(trainNum + 1:trainNum + testNum,:);
output_test =output(trainNum + 1:trainNum + testNum)’;

%% Data normalization
[inputn,inputps]=mapminmax(input_train,0,1);
[outputn,outputps]=mapminmax(output_train);
inputn_test=mapminmax(apply’,input_test,inputps);

%% Get the number of input layer nodes and output layer nodes
inputnum=size(input,2);
outputnum=size(output,2);
disp(/’)
disp(‘Neural network structure…’)
disp([The number of nodes in the input layer is:’,num2str(inputnum)])
disp([The number of nodes in the output layer is:’,num2str(outputnum)])
disp(‘ ‘)
disp(‘The process of determining hidden layer nodes…’)

%Determine the number of hidden layer nodes
% Use the empirical formula hiddennum=sqrt(m + n) + a, m is the number of input layer nodes, n is the number of output layer nodes, a is generally an integer between 1-10
MSE=1e + 5; % initialization minimum error
for hiddennum=fix(sqrt(inputnum + outputnum)) + 1:fix(sqrt(inputnum + outputnum)) + 10

%Build network
net=newff(inputn,outputn,hiddennum);
% Network parameters
net.trainParam.epochs=1000; % training times
net.trainParam.lr=0.01; % learning rate
net.trainParam.goal=0.000001; % training target minimum error
% network training
net=train(net,inputn,outputn);
an0=sim(net,inputn); %Simulation results
mse0=mse(outputn,an0); %mean square error of simulation
disp(['When the number of hidden layer nodes is',num2str(hiddennum),', the mean square error of the training set is:',num2str(mse0)])

%Update the best hidden layer node
if mse0<MSE
    MSE=mse0;
    hiddennum_best=hiddennum;
end

end

?3. Operation results



?4. Matlab version and references

1 matlab version
2014a

2 References
[1] Guo Lijin, Qiao Zhizhong. Research on grain temperature prediction based on genetic algorithm optimization of BP neural network [J]. Grain and Oil. 2023,36(01)

3 Remarks
Introduction This part is taken from the Internet for reference only. If there is any infringement, please contact us to delete it.