# On-Demand Charging Management Model and Its Optimization for Wireless Renewable Sensor Networks
## 4 Implementation of Proposed ERMS-PSO
### Particle Swarm Optimization (PSO)  
The PSO is made up of particles, which are members of the population. Each particle represents a bird in a flock, a swarm of fish or a potential result of an optimization problem. PSO starts by setting with (nPop) the number of particles and every particle in an \(n\)-dimension solution space. The autocorrelation of particle positions, the average velocity of each particle per iteration and the fitness of the search are three factors that describe the movement pattern in a particle swarm. These variables represent swarm movement patterns and how they affect inertia weight and velocity coefficients (\(\omega\) and \(c\)). The PSO algorithm's general steps are described below, with the input number of particles m, n number of dimensions of the solution space (_dim_), inertia weight \(\omega\), learning coefficients \(c_{1}\) and \(c_{2}\) with cognitive and social components, respectively. Step 1: In the n-dimensional space, create m particles and assign each one an initial position \(X_{i}\) and an initial velocity \(V_{i}\) In Equations (20) and (21), we define the position \(X_{i}\) and the velocity \(V_{i}\) vectors of the ith particle as follows:  
\[X_{i}=(X_{i,1},\ X_{i,2}\ldots,\ X_{i,dim}) \tag{20}\]  
\[V_{i}=(V_{i,1},\ V_{i,2}\ldots,\ V_{i,dim}) \tag{21}\]  
Step 2: Based on each particle's position, we calculate the fitness function \(f\) as shown in Equation (22). We define one cycle (\(Cyc\)) time as the interval between two charging requests, and one cycle time is a summation of the vacation time, the charging time and the travel time for all the nodes visited. Therefore, the number of cycles can be defined as the number of charging requests received during the lifetime (total time) of the network. \[ \begin{array}{c}f=\left(\frac{Cyc}{10^{2\epsilon_{10}Cyc}}-1^{-8}\right)^{2} +\left(e_{min}-1^{-8}\right)^{2}+\left(e_{thresh}-1^{-8}\right)^{2}+\\ \left(\frac{1}{\tau_{vac}}-1^{-8}\right)^{2}+\left(\frac{D_{total}}{10^{2 \epsilon_{10}D_{total}}}-1^{-8}\right)^{2}\end{array}  \tag{22}\]  
\[ \begin{array}{c}\text{s.t}\qquad E_{min}<e_{min}<e_{thresh},\qquad E_{min}=0. 05*E_{max}\\ e_{min}<e_{thresh}<E_{max},\qquad E_{max}=10.8\ KJ\end{array} \]  
where \(Cyc\) is the time as the interval between two charging requests and \(D_{total}\) is the total distance traveled by the WMCD throughout all cycles. Step 3: Update each particle's velocity and position as follows:  
Every iteration, the position \(X_{i}\) and velocity \(V_{i}\) of each particle in the swarm are updated through Equations (23) and (24) as follows:  
\[ \begin{array}{c}V_{i}(iter+1)=\omega\cdotr\cdot V_{i}(iter)+c_{1}\cdot r_{1 }(PBest_{i}(iter))-X_{i}(iter)\\ \qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad+c_{2 }\cdot r_{2}(Speest_{i}(iter)-X_{i}(iter))\end{array}  \tag{23}\]\[X_{i}(iter+1)=X_{i}(iter)+V_{i}(iter+1) \tag{24}\]  
where \(r_{j}\) is a number between [0, 1] and \(j\) [0, 1, 2], generated randomly. Every iteration, the particles in the swarm move closer to the solution of the problem and update their positions towards \(P\!Best\) and \(G\!Best\). Equations (23) and (24) generate additional position and velocity vectors for the next iteration (iter + 1). The inertia weight \(\omega\) is a learning coefficient associated with the previous velocity, while learning coefficients \(c_{1}\) and \(c_{2}\) are associated with the cognitive and social components, respectively. Step 4: Then, we check if the ending condition has been met; the algorithm is terminated if this is the case; otherwise, the algorithm goes back to Step 2. The PSO ending condition could be that it has completed the maximum number of iterations or that the global optimal position's fitness function value does not increase by a certain amount.