## 3 Introduction to Particle Swarm Optimization
First, there is an initialization where all \(sw_{i}\in sw\) (first For loop) are initialized as it is described next. In initPosition(\(i\)), the position \(x_{i}\) of particle \(sw_{i}\) is randomly chosen within the range \([bot,up]\), where \(bot\) and \(up\) are the lower and upper bounds of the search space (note that \(bot\) and \(up\) can be different for each dimension); this is usually called the _dynamic range_, and particles cannot move out of these limits. Additionally, we compute \(x_{i}^{fitness}=f(x_{i})\). In the initBestLocal(\(i\)) phase, we set \(p_{i}=x_{i}\) and \(p_{i}^{fitness}=x_{i}^{fitness}\). The next instruction, initBestGlobal(), is only executed when \(i=1\) and initializes \(g=x_{1}\) and \(g^{fitness}=x_{1}^{fitness}\). If improvedGlobal()\(=True\), that is, if \(p_{i}^{fitness}<g^{fitness}\), then the best global solution is updated in updateBestGlobal(\(i\)), where the following operations are performed: \(g=p_{i}\) and \(g^{fitness}=p_{i}^{fitness}\). Finally, in the initVelocity(\(i\)) phase, the particle's velocity is randomly established in the range \([-|up-bot|,|up-bot|]\) for each of the dimensions, but usually it is limited by a fixed value, \(v_{max}\), which cannot be exceeded. After the initialization, the core of the PSO method is executed until the endingCondition() is satisfied (While loop). The endingCondition() can limit the number of iterations performed, the time consumed, or the fitness adequation. In the body of the While loop (the For loop), all particles are updated. The first step in the For loop, createRnd(rp,rg), creates two random values (\(rp\) and \(rg\)) in the range (0, 1) that will be used in the next phase. Then, in updateVelocity(\(i\),rp,rg), the velocity of \(sw_{i}\) is updated in the following way:  
\[v_{i}=\omega\cdot v_{i}+\phi p\cdot rp\cdot(p_{i}-x_{i})+\phi g\cdot rg\cdot(g -x_{i})\]where \(\omega\) is a parameter called _inertia weight_ (introduced in [25]) which influences the convergence of the algorithm: For instance, an excessive inertia value can make the particle systematically exceed the optimum from a side to another in each movement, whereas a low value could make it approach the optimum at a too low pace; \(\phi p\) and \(\phi r\) are called the cognitive and social parameter, and make the method be less predictable and more flexible. \(\phi p\) controls the influence on the movement of the own knowledge of the particle, while \(\phi r\) controls the influence of the global knowledge. All of these parameters are experimentally set (see [29; 36; 37; 38] for further details) and control the behavior and efficacy of the PSO method. The next step, updatePosition(_i_), updates the position of \(sw_{i}\), adding its new velocity to its current position: \(x_{i}=x_{i}+v_{i}\), and updates the fitness value by setting \(x_{i}^{\mathit{fitness}}=f(x_{i})\). If \(x_{\mathit{max}}\) is exceeded, then we set \(x_{i}=x_{\mathit{max}}\). At the end of the loop, if improvedLocal() \(=True\), that is, if \(x_{i}^{\mathit{fitness}}<p_{i}^{\mathit{fitness}}\), then the best local solution is updated in updateBestLocal(_i_) as follows: \(p_{i}=x_{i}\) and \(p_{i}^{\mathit{fitness}}=x_{i}^{\mathit{fitness}}\). In the last step, if improvedGlobal() is satisfied then the global solution is updated in the updateBestGlobal(_i_) phase as we explained above.