# Particle Swarm Optimisation in Practice: Multiple Applications in a Digital Microscope System
## Materials and Methods
### Particle Swarm Optimisation  
Particle swarm optimisation [12; 13] is a swarm intelligence metaheuristic for optimising continuous nonlinear problems defined on an \(n\)-dimensional search space \(\mathbf{D}\subset\mathbb{R}^{n}\) (characterised by \(n\) lower and upper bounds, i.e., \(l_{i}\) and \(u_{i}\) with \(i=1,\ldots,n\), in this work). Its popular working mechanism is quite simple, and it is iterated until a stop condition is met--with the most used stop criteria being a fixed number of objective functional calls, usually expressed in terms of maximum number of iterations \(I_{\text{max}}\), failing at improving upon the objective value of the best ever found solution \(\mathbf{x}^{GB}\) for a number \(F_{\text{max}}\) of iterations, or the achievement of a satisfactory level of accuracy for the solution (measured with a threshold \(\tau\)). Each iteration consists of the perturbation, and interaction, of a number of particles \(NP\) associated with their current position in the search space \(\mathbf{x}_{j}\in\mathbf{D}\) (i.e., the candidate solution), their velocity \(\mathbf{v}\in\mathbb{R}^{n}\), and the'personal best' ever explored position \(\mathbf{x}_{j}^{PB}\) (with \(j=1,\ldots,NP\)). Note that a solution is said to be better, or to outperform another one, if its objective function value is lower while facing a minimisation problem--the opposite for a maximisation one. The same implementation of a PSO can be used for both minimisation and maximisation processes by simply multiplying the objective function \(f(*)\) by \(-1\) as needed. A complete iteration requires calling \(f(*)\) for \(NP\) times as each solution \(\mathbf{x}\) (which is initially randomly generated in \(\mathbf{D}\)) must be perturbed to attempt to explore a new position whose functional value outperforms the one of the global best solution \(\mathbf{x}^{GB}\) to update it. This is achieved by applying the following operators to the the velocity and position vectors of the \(NP\) particles:  
\[\mathbf{v}_{j}^{(new)}=w\mathbf{v}_{j}+r_{1}\Big{(}\mathbf{x}_{j}^{PB}-\mathbf{ x}_{j}\Big{)}+r_{2}\Big{(}\mathbf{x}_{j}^{GB}-\mathbf{x}_{j}\Big{)} \tag{1}\]  
\[\mathbf{x}_{j}^{(new)}=\mathbf{x}_{j}+\mathbf{v}_{j}^{(new)} \tag{2}\]  
where \(w\) is the 'inertia' weight (controlling the exploratory step size [14]) while \(r_{1}\) and \(r_{2}\) are random weights uniformly drawn within \([0,\ c_{1}]\) and \([0,\ c_{2}]\), respectively. The acceleration constants \(c_{1}\) and \(c_{2}\) are user-defined to have more pressure towards local best solutions, the global best solution or to achieve a balanced search. More details can be obtained from [15]. Generally employed settings from the literature see \(c_{1}=c_{2}=2\) as good choices for several applications [12], while \(w=0.7\) is a quite established value, even though adaptive formulas for dynamically adjusting \(w\), see, e.g., \(w\in[0.4,0.9]\)[16; 17], as well as for adjusting the acceleration constants, see, e.g., [18; 19; 20], also exist. Note that the velocity vectors, which act more as a displacement vector for the current position, are usually initialised at random within \(\mathbf{D}\) (small values are to be preferred [21]) can then assume any arbitrary value. This may lead to the so-called'velocity explosion' undesired effect, which should be mitigated, e.g., as shown in [22], and by avoiding a too small swarm size [23]. Our implementation controls the velocity value via the 'clamping' method from [22]. Finally, we employ the 'absorbing walls' method in [24] to deal with infeasible solutions that might be generated by the PSO logic during the search for optima. This consists of saturating the infeasible positions of a generic \(j\)th particle (i.e., those components of \(\mathbf{x}_{j}\) falling outside the boundaries) to the closest 'violated' boundary re-initialising the corresponding velocity vector \(\mathbf{v}_{j}\) with zeros.