# Carbon Quota Allocation Prediction for Power Grids Using PSO-Optimized Neural Networks
## 3 Materials and Methods
### Model Building and Optimization
#### 3.2.2 BPNN Module
The total number of particles in the population is \(u\), vector \(r_{i}=[r_{i1},r_{i2},\ldots,r_{iD}]^{T}\) indicates the position of the \(i\)th particle, vector \(v_{i}=[v_{i1},v_{i2},\ldots,v_{iD}]^{T}\) represents the velocity of the \(i\)th particle, vector \(k_{i}=[k_{i1},k_{i2},\ldots,k_{iD}]^{T}\) represents the optimal position searched by the particle itself, vector \(g_{i}=[g_{i1},g_{i2},\ldots,g_{iD}]^{T}\) represents the optimal location searched for the entire population, \(z\) is the particle numbering, \(z\in(1,2,\ldots,u)\). The velocity and position of the PSO are expressed by the following equation: Initially, the PSO algorithm calculates the fitness value of each particle using the fitness function and iteratively updates it to search for the optimal solution. In each iteration, particle \(r_{i}\) updates its position and velocity based on its individual optimal value \(k_{i}\) and the global optimal value \(g_{z}\). The velocity and position of the particles in PSO are expressed by the following equations. Figure 4.0  
Figure 4.1  
where \(n\) is the number of iterations; \(w\) is the inertial weight, which is used to control the convergence and searchability of the algorithm; \(c_{1}\) and \(c_{2}\) are the acceleration weights that push the particle to the optimal position of the individual and the group, respectively; \(rand(x)\) is the random number generator; the generated random number is between \([0,1]\); \(k_{i}(n)\),\(g_{z}\) is the current best and global best position in the PSO algorithm. The steps of Particle Swarm Optimization (PSO) to optimize the BP neural network are as follows (Figure 5):  
Step 1: Use MATLAB software v.2023a to read the data of the carbon emission index of the electric power industry and set the Q parameters of the algorithm. Step 2: Initialize the particle swarm and velocity: the particles represent a BP neural network mouth model, and the velocity is used to control the movement of particles in the search space. The position of each particle represents the weight and deviation of the neural network Q, and the speed represents the search strength in a certain direction. Step 3: Initialize the global optimal solution and the individual optimal solution: the global optimal solution is the optimal solution in the whole particle swarm, i.e., the optimal neural network model mouth; the individual optimal solution indicates the optimal solution found by each particle. Step 4: Calculate the fitness: for each particle in the particle swarm, calculate its fitness value according to the indicator weights and deviations. The fitness function is the error of the BP neural network. Step 5: Update velocity and position: update the velocity and position of the particles according to their own optimal solutions and the optimal solutions in the population,as well as some weight coefficients. Specifically, the new velocity and position can be calculated by the velocity and position update formula. Step 6: Update the global optimal solution and the individual optimal solution: after each iteration, the global optimal solution and the individual optimal solution are updated according to the fitness value. If the fitness of a particle exceeds the fitness of the global optimal solution, it is taken as the new global optimal solution; if the fitness of a particle exceeds the fitness of its individual optimal solution, it is taken as the new individual optimal solution. Step 7: Repeat Iteration: Repeat steps 4 and 5 until the condition of stopping iteration is reached. The conditions for stopping iteration can be set according to the actual situation, such as reaching the maximum number of iterations, the fitness value reaching a preset threshold or the fitness value becoming stable. Step 8: Output the optimal prediction result of carbon emissions.