# Optimization of PV and Battery Energy Storage Size in Grid-Connected Microgrid
## 3 Proposed Algorithm
### 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 [PERSON] and [PERSON] 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. \(n_{Pop}\) 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 \(k^{th}\) particle is:  
\[v^{j}_{k,new}=wv^{j}_{k,old}+c_{1}r_{1}(x^{j}_{k,pbest}-x^{j}_{k})+c_{2}r_{2} \tag{7}\]  
where \(v^{j}_{k,new}\) refers to the recent velocity of the \(k\)th particle at \(j\)th iteration, the \(w\) refers to the inertia weight, \(v^{j}_{k,old}\) refers to previous velocity of the \(k\)th particle at the \(j\)th iteration, \(c_{1}\) and \(c_{2}\) are the acceleration constants, and \(r_{1}\) and \(r_{2}\) pair are randomly determined numbers between 0 and 1. The position of the \(k\)th particle is renewed as below [16]:  
\[x^{j}_{k,new}=x^{j-1}_{k,old}+v^{j}_{k,new} \tag{8}\]where \(x_{k,old}^{j-1}\) 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 \(Var_{Min}\) = 1 and maximum value is \(Var_{Max}\) = 50. Figure 5 shows the flowchart of the applied PSO algorithm.