## 3 Methodology
### Particle Swarm Optimization Algorithm  
The Particle Swarm Optimization, also known as PSO, is a heuristic algorithm that can be used to tune the matrices of a controller based on maximum and minimum user-defined values. This method has been detailed by many authors as shown in Table 1. The inspiration for this algorithm comes from the behavior of flocks of birds or schools of fish. The number of particles (individuals) is first defined, the larger the number of particles, the more likely the swarm is to find the global optimum in the set number of iterations. The maximums and minimums for the values to be tuned are held within the W max and W min variables. The W variable represents the inertia weight and is the distance the particle can search within an iteration.  
The \(C_{1}\) and \(C_{2}\) values relate to the particles individual and social behavior respectively. If \(C_{2}\) is set high, there will be a high level of \"communication\" between the particles in the swarm. Based on Equation (9), a greater emphasis will be placed on the personal best over the group best. The swarm will converge quickly, but possibly on a local minimum instead of the global minimum. If \(C_{1}\) is set high, then the emphasis is placed on the individual in the swarm without communication between particles, the exploration space is large, but the particles do not work as a \"team\" to find the global minimum. Balancing the \(C_{1}\) and \(C_{2}\) values is key to the success of the algorithm. The elements for \(r_{1}\) and \(r_{2}\) introduce randomness into the calculation to change the result each time. \(P_{best}\) and \(G_{best}\) correspond to the particles optimum and group optimum, respectively. The calculation for \(v_{i}\) below uses these values to derive the velocity of the \(i^{\prime}th\) particle in the swarm, then update its position \(x\) using its previous position and the newly calculated velocity.  
\[v_{i} = Wv_{i}+C_{1}r_{1}(P_{best,i}-x_{i})+C_{2}r_{2}(G_{best}-x_{i}) \tag{9}\] \[x_{i} = x_{i}+v_{i} \tag{10}\]  
Figure 9: Comparison of varying Vd and Vn matrix values.  
A Fitness Function is required for testing the gains chosen by the algorithm, this can be configured as a cost function, better gains will result in a drop in the overall cost function of the controller. The Fitness Function used is Eigenstructure Assignment, where the eigenvalues of the devised controller in the closed loop system are taken, as shown in Equation (11), the observer component (Lo.C) is removed for Full State Feedback controller. This fitness function was selected as it incorporates both eigenvectors and eigenvalues in the equation relating to the speed and shape of response respectively directly correlating to the settling time and % overshoot of the closed loop system. With each iteration of the algorithm, the poles of the closed loop system are moved further into the negative domain, stabilizing the system.  
\[[V,D,W]\ =\ \det((A-B.K+Lo.C)-\lambda\ I) \tag{11}\]  
The left and right eigenvalues (\(V\), \(W\)) are taken to evaluate S in Equation (12). In Equation (13), the absolute sum of \(S\) is taken as the fitness value, large values show a poor performance of the algorithm while small values show good performance.  
\[S\ =\ \frac{W^{T}V}{||W||_{2}||V||_{2}} \tag{12}\]  
\[J\ =\ ||\sum S| \tag{13}\]  
The code for the calculation of optimal values using PSO algorithm is defined in Pseudocode 1.  
```
Initialize the control parameters (N,c1,c2,Wmin,  Wmax and MaxIter) Initialize pop of N particles for 1: MaxIter do for each particle Calculate the objective of the particle based on Fitness Function Update P_best if better than previous Update G_best if better than previous endfor Update the inertia weight (W) for each particle Update the velocity (v) Update the position (x) endfor Return G_best as the best estimation of the global optimum endfor
```  
**Pseudocode 1**.: **Particle Swarm Optimisation**