2.2. Prediction Model Based on Neural Network
A neural network model is composed of an input layer, hidden layer, and output layer, and possesses the characteristics of self-organized learning, strong adaptability, and excellent prediction ability, so it is widely used in various fields [
40,
41,
42,
43].
A BP neural network is a feedforward neural network; its learning process is divided into two parts: forward transmission and backward transmission. In the forward transmission, the information is transferred from the input layer to the hidden layer and finally to the output layer, and in which the state of each neuron is only related to the neurons connected to it in the previous layer. If the deviation between the output of the neural network and the real target value is large, reverse learning occurs. The learning rule is to use the steepest descent method to continuously adjust the weight and threshold of the network through back propagation to minimize the sum of the square of errors of the network, so that the predicted output of the BP neural network can approach the expected output. The structure of a three-layer BP neural network is shown in
Figure 1.
Suppose the input x is a real vector, , ; the output y is a real vector, , ; the hidden layer has m nodes, and its output u is a real vector, , ; the weight from the input layer to the hidden layer is , and the threshold is ; the weight from the hidden layer to the output layer is , and the threshold is ; and they are all real numbers.
The neuron output
in the hidden layer and the neuron output
in the output layer are, respectively:
where the activation function
can select different functions according to the actual situation.
2.3. Neural Network Prediction Model Based on Particle Swarm Optimization Algorithm
For the neural network model, if the error back-propagation algorithm is used to train the model, it easily falls into the local extremum problem because of the local search characteristic of the algorithm. The PSO algorithm is an evolutionary algorithm proposed by Kennedy and Eberhart that imitates bird foraging behavior through particles. It relies on the memory of the bird’s own position and the information exchange mechanism within the swarm, so that the bird can quickly locate the target position [
44,
45,
46]. The PSO algorithm can greatly reduce the possibility of falling into the local optimal solution, and has the characteristics of easy implementation and fast convergence.
In the PSO algorithm, each particle has its own position, velocity, and memory, and serves as a point in the search space. After initializing the particle population, the fitness value of each particle is calculated. In each iteration, each particle updates its own historical best position and global best position found in the entire population. The goal of each particle is to search the space of the feasible solution so that the optimal solution can be found. At the beginning of the algorithm, the initial particles may be far away from the optimal solution and scattered in the search space. However, with the continuous iteration of the algorithm, the particle can track the global optimal position of the population and the individual historical optimal position by exchanging these information points with each other, so that it can constantly update its velocity and position, and gradually move closer to the optimal solution.
The key of the PSO algorithm is to update the velocity and position of individual particles. In the iterative process of the algorithm, the particle constantly adjusts its velocity and position through the individual historical optimal position and the global optimal position, so that the particle can always move closer to the optimal solution. The update equations for the velocity and position of the particles are as follows:
where
k is the current number of iterations;
and
are the random numbers in the range of [0, 1];
and
are the velocity and position of the
ith particle in the
kth iteration;
and
are the historical optimal position of the
ith particle and the global optimal position of all particles in the
kth iteration;
and
are learning factors, and they are non-negative real numbers; when the value of
is large, most of the particles in the population will fluctuate in the local space; when the value of
is large, it will cause the phenomenon of premature convergence. Generally,
, and their range is [0, 4].
is the inertia weight, which represents the influence of the previous velocity of the particle on the velocity of the particle at the present moment. If
is larger, the global optimization performance of the particle is enhanced, which reduces the risk of the algorithm falling into the local optimal value; if
is smaller, the local optimization performance of the particles is enhanced, and accurate optimization can be achieved.
In the velocity update Equation (3), the first term, , reflects the trend of particles flying in the original direction and plays a role in balancing global optimization and local optimization. The second term, , expresses the particle moving towards its previously optimal position, so that the particle has the ability of global optimization. The third term, , reflects the phenomenon that the information exchange within the population makes the particles move towards the global optimal position in the search space, and reflects the ability of cooperation among particles in the population. The position update Equation (4) expresses the information that the particles are moving closer to the optimal position.
In order to better optimize the parameters of the neural network, this section selects the PSO algorithm to solve this problem. In the PSO algorithm, each individual particle achieves the optimal value in the search space through continuous iterative search and information exchange within the swarm. The flow of the neural network prediction model based on PSO algorithm (PSO–NN) is shown in
Figure 2.
Step 1: Data segmentation. The dataset is divided into the training set (80%) and the test set (20%).
Step 2: Determine the optimal neural network structure. For each candidate neural network structure, the training set is used to train it, and the optimal neural network structure is selected according to the output results.
Step 3: Initialize the relevant parameters for the neural network and PSO algorithm.
Step 4: According to the fitness function calculation equation, the fitness function value of each particle is calculated:
where
N is the number of samples,
is the actual data of the sample, and
is the predicted data of the model.
For each particle, the fitness value of its current position is compared to the fitness value corresponding to the historical optimal position and the global optimal position , respectively. If the current position is better than , it means that the current position is closer to the optimal solution, so the current position is assigned to ; similarly, if the current position is better than , the current position is assigned to .
Step 5: Update the velocity and position of the particle according to update Equations (3) and (4).
Step 6: The neural network model is trained with the training set, and the parameters of the neural network are updated by constantly updating the historical optimal position and the global optimal position .
Step 7: Judge whether the training has terminated; when the training reaches the maximum number of iterations K, or makes a small number of changes in a certain number of iterations, the algorithm terminates; otherwise, the algorithm continues and returns to Step 4.
Step 8: The trained neural network prediction model is tested on the test samples.
2.4. Neural Network Prediction Model Based on PSO Algorithm with Adaptive Inertia Weights
With the increase in the number of iterations, the exploration space of the PSO algorithm gradually decreases, which will lead to a decline in the search ability of the algorithm at the later stage, resulting in premature convergence and an inability to find the global optimal value. This section addresses the problem of the PSO algorithm easily falling into the local optimal value by improving it using adaptive inertia weight. Basing the PSO algorithm on adaptive inertia weight enables it to jump out of the local optimal value in time. The specific calculation method is shown in Equations (5) and (6):
where
is the smoothness of the inertia weight change; should the value of
change significantly, it indicates that the inertia weight
is less smooth. In order to prevent a significant change in the value of
, it is improved in exponential form.
In order to find the global optimal value quickly, it is also necessary to maintain the diversity of the particle population. In the early stage of the algorithmic search, the value of is larger, which improves the search speed. When the particles are close to the global optimal value, the change in the value is small, finding the global optimal value more accurately and quickly; the algorithm can, therefore, be prevented from falling into the local optimal value, thereby improving its optimization efficiency.
Based on the above technology, the flow of the neural network prediction model based on the PSO algorithm with adaptive inertia weight (AIWPSO–NN) proposed in this section is shown in
Figure 3.
Step 1: Data segmentation. The dataset is divided into the training set (80%) and the test set (20%).
Step 2: Determine the optimal neural network structure. For each candidate neural network structure, the training set is used to train it, and the optimal neural network structure is selected according to the output results.
Step 3: Initialize the relevant parameters of the neural network and PSO algorithm.
Step 4: According to the fitness function calculation formula, the fitness function value of each particle is calculated.
Step 5: Update the historical optimal position and the global optimal position .
Step 6: Update the velocity and position of particles based on adaptive inertia weight Equations (5) and (6).
Step 7: The neural network model is trained with the training set, and the parameters of the neural network are updated by constantly updating the historical optimal position and the global optimal position .
Step 8: Judge whether the training has terminated. When the training reaches the maximum number of iterations K, or makes a small number of changes in a certain number of iterations, the algorithm terminates; otherwise, the algorithm continues and returns to Step 4.
Step 9: The trained neural network prediction model is tested on the test samples.
2.5. Neural Network Prediction Model Based on Improved PSO Algorithm
When dynamic adaptive inertia weights are used, the particles gradually approach the global optimal value with the increasing number of algorithm iterations; however, with the progress of the algorithm, it is possible that the optimal value does not change. Assuming that the optimal value does not change in the continuous
G generation, it shows that the algorithm may fall into the local optimal value, resulting in premature convergence. To solve this problem, an improved method combining the dynamic adaptive inertia weight and an escape strategy is proposed. The escape strategy is to generate a new generation of particle population using a mutating operation that increases the diversity of particle population and improves the possibility of particles jumping out of the local optimal value. The calculation method for the escape strategy is shown in Equation (7):
With the continuous iterative search of the PSO algorithm, the particle population gradually approaches the global optimal value. When the global optimal value of the G generation does not change, the particle aggregation is high and the survival density is too small, so the escape strategy is proposed to obtain a new search space.
Based on the adaptive inertia weight and escape strategy, the flow of the neural network prediction model based on improved particle swarm optimization algorithm (IPSO–NN) proposed in this section is shown in
Figure 4.
Step 1: Data segmentation. The dataset is divided into the training set (80%) and the test set (20%).
Step 2: Determine the optimal neural network structure. For each candidate neural network structure, the training set is used to train it, and the optimal neural network structure is selected according to the output results.
Step 3: Initialize the relevant parameters of neural network and PSO algorithm.
Step 4: According to the fitness function calculation formula, the fitness function value of each particle is calculated.
Step 5: Update the historical optimal position and the global optimal position .
Step 6: If the optimal value does not change in continuous G generations, go to Step 7; otherwise, go to Step 8.
Step 7: The escape strategy is used to generate a new generation of particle population, then go to Step 9.
Step 8: Update the velocity and position of particles based on adaptive inertia weight Equations (5) and (6).
Step 9: The neural network model is trained with the training set, and the parameters of the neural network are updated by constantly updating the historical optimal position and the global optimal position .
Step 10: Judge whether the training has terminated. When the training reaches the maximum number of iterations K, or makes a small number of changes in a certain number of iterations, the algorithm terminates; otherwise, the algorithm continues and returns to Step 4.
Step 11: The trained neural network prediction model is tested on the test samples.
2.6. Objective Function and Evaluation Index
In this paper, the yield of greenhouse tomatoes is taken as the output value of the prediction model. The difference between the predicted value of the model and the actual value of the sample is taken as the objective function; the parameters of the prediction model are optimized based on this objective function. The mean square error (
MSE), mean absolute error (
MAE), and coefficient of determination (
R2) were selected as the evaluation indexes of the output results for the above four different greenhouse tomato yield predictive models; the
MSE value,
MAE value, and
R2 value were used to verify the effectiveness of the greenhouse tomato yield predictive model [
47,
48,
49].
(1) The objective function refers to the difference between the predicted value of the model and the actual value of the sample, expressed as follows:
where
N is the number of samples,
is the actual data of the sample,
is the predicted data of the model.
(2) The
MSE is the average value of the sum of the square of the difference between the predicted value of the model and the actual value of the sample;
MSE is calculated as shown in Equation (9):
Based on Equation (9), it can be seen that the smaller the MSE value, the better the accuracy of the predictive model.
(3)
MAE is defined as the average of the absolute error between the predicted value of the model and the actual value of the sample. The
MAE better reflects the actual situation of predictive errors; it is calculated as shown in Equation (10):
(4)
R2 is used to measure the degree of fitting between the prediction model and the actual data; it is expressed as shown in Equation (11):
where the range of the
R2 value is [0, 1], and the larger the
R2, the closer
R2 is to 1. This indicates a better fitting effect between the prediction model and the actual data.