## 4 Generic PSO Algorithms in Haskell  
Let us note that Haskell functions are first-class citizens. Thus, they can be used as parameters of other functions. In particular, in order to provide a general scheme to deal with PSO algorithms, we need to use as parameter the concrete fitness function used in the problem. In addition to the fitness function, we also need to consider other parameters like the number of particles to be used, the number of iterations to be executed by the program, the boundaries of the search space, and the parameters \(\omega\), \(\phi p\) and \(\phi r\) described in Section 3. Moreover, in order to implement PSO in a pure functional language like Haskell, we need an additional parameter to introduce randomness. Note that Haskell functions cannot produce side-effects, so they need an additional input parameter to be able to obtain different results in different executions. Taking into account these considerations, the type of the higher-order Haskell function dealing with PSO algorithms is the following:  
Pso :: RandomGen a => a --Random generator  -> WPGparams --\(\omega\), \(\phi\)\(p\) and \(\phi\)\(r\)  -> Int --Number of particles  -> Int --Number of iterations  -> (Position->Double) --Fitness function  -> Boundaries --Search space bounds  -> (Double,Position) --Result:Best fitness and position Note that the type Position should be able to deal with arbitrary dimensions. Thus, the simplest and more general solution to define a position is to use a list of real numbers, where the length of the list is equal to the number of dimensions of the search space. Analogously, the type Boundaries should contain a list of lower and upper bounds for each of the dimensions. Thus, it can be described by a list of pairs of real numbers. Finally, the type WPGparams is even simpler, as it only contains a tuple of three real numbers describing the concrete values of parameters \(\omega\), \(\phi p\) and \(\phi r\):  
type Position = [Double]  type Boundaries = [(Double,Double)]type WPGparams = (Double, Double, Double)  
Once input parameters are defined, it is time to define the body of the function. First, we use function initialize to randomly create the list of initial particles. Its definition is simple, as we only need to create np particles randomly distributed inside the search space defined by the boundaries bo. Once particles are initialized, we use an independent function pso' to deal with all the iterations of the PSO algorithm. As in the case of the main function pso, the auxiliary function pso' will also need a way to introduce randomness in order to have appropriate random values for \(rp\) and \(rg\) in the range (0, 1). This problem is solved by function makeRss, which creates all needed random values. pso sg wpg np it f bo = obtainBest (pso' rss wpg it f bo initPs)  where initPs = initializes g np bo f  rss = makeRss np (randomRs (0, 1) sg)  
--Particle: Best local value, best global value, current position and  --velocity, best local position, best global position  type Particle = (Double,Double,Position,Velocity,Position,Position)  type Velocity = [Double]  
Note that each particle contains its position, its velocity (that will also be randomly initialized), the best solution found by the particle, and the best solution found by any particle. Function pso' has a simple definition. It runs recursively on the number of iterations. If there is not any iteration to be performed then we just return as result the input particles. Otherwise, we apply one iteration step (by using an auxiliary function oneStep) and then we recursively call our generic scheme with one iteration less:  
pso' __ 0 __  pas = pas  pso' (rs:rss) wpg it f bo pas = pso' rss wpg (it-1) f bo  (oneStep rs wpg f bo pas)  
Figure 1 summarizes the flowchart of function pso. First, function initialize creates the initial conditions to start the evaluation, and then function pso' performs the real work.