*3.3. PSO Algorithm*

The PSO algorithm presents a model of flight patterns of birds and their social behavior for the optimization model, which was proposed by J. Kennedy and R. Eberhart in 1995 [29,30]. Its ties artificial life to the behavior of animal groups, such as bird flocking, fish schooling and swarming theory [30]. The simple explanation of the PSO model can be explained as follows. Each single bird is pointed in the Cartesian Coordinate System (CCS). Their initial location and velocity are assigned randomly. Then, the algorithm is executed with "the nearest proximity velocity match rule"; thus, every bird has the same speed as their closest neighbor. Since iteration maintains in the same direction, all the points will have the same velocity. Because of the simplicity of this structure and not exactly the same as in real situations, a random variable is added to the speed point. In each iteration, aside from meeting "the nearest proximity velocity match", each speed will be added with a random variable that provides convergence to the real case. In this model, every bird can find their maximum points. These can only be local maximum points. After every bird meets, in other words, birds finish their movement on the coordinate system, all the maximum points will be found. The highest value of these maximum points is the global maximum point [31,32].

In this study, PSO is used to find the minimum points, meaning minimum cost. Particles represent PV and BESS module counts (or sizes), and they are initialized randomly in the CCS. *nPop* is the swarm size, and it is defined as 50, which means 50 particles. The maximum iteration, *MaxIt*, count is set to 100. The inertia coefficient is set to 1. There are two acceleration coefficients, and both of them are selected as 2.5. Each particle's velocity is zero at the beginning. The objective function, which is explained in the previous section, is called in every iteration to calculate the particle's total cost. Each particle's cost is compared with each other's and the best cost, which is the minimum, is saved as the global best cost. In every iteration, PSO generates random PV and BESS sizes, and their costs are compared with the global best. The lowest value is saved as the new global best. At the end of all iterations' location, which means PV and BESS sizes, of the global best cost is the optimal point [16,33]. Each PV and BESS has a position, and these positions have a velocity. The velocity of the *kth* particle is:

$$\boldsymbol{\upsilon}\_{k,new}^{j} = \boldsymbol{w}\boldsymbol{\upsilon}\_{k,old}^{j} + c\_1 \boldsymbol{r}\_1 (\boldsymbol{\mathfrak{x}}\_{k,pbest}^{j} - \boldsymbol{\mathfrak{x}}\_k^{j}) + c\_2 \boldsymbol{r}\_2 \tag{7}$$

where *v j <sup>k</sup>*,*new* refers to the recent velocity of the *k*th particle at *j*th iteration, the *w* refers to the inertia weight, *v j <sup>k</sup>*,*old* refers to previous velocity of the *k*th particle at the *j*th iteration, *c*<sup>1</sup> and *c*<sup>2</sup> are the acceleration constants, and *r*<sup>1</sup> and *r*<sup>2</sup> pair are randomly determined numbers between 0 and 1. The position of the *k*th particle is renewed as below [16]:

$$
\mathfrak{x}\_{k,new}^{j} = \mathfrak{x}\_{k,old}^{j-1} + \mathfrak{v}\_{k,new}^{j} \tag{8}
$$

where *xj*−<sup>1</sup> *<sup>k</sup>*,*old* is the previous position of the *k*th particle from the past iteration [16]. The position *x* is the size of PV and BESS, and in this study, their minimum value is *VarMin =* 1 and maximum value is *VarMax* = 50. Figure 5 shows the flowchart of the applied PSO algorithm.

**Figure 5.** Proposed PSO algorithm flowchart.
