## 3 Methodology
### Genetic Algorithm  
The Genetic Algorithm (GA) is another metaheuristic algorithm used for locating optimal gains for controllers. The inspiration for this algorithm comes from the theory of evolution and natural selection where the best individuals in an environment will pass genes onto the next generation, compounding into a highly evolved individual well suited to a given task. The process used in GA can be used to tune controller parameters completely from scratch or adapt and improve those selected manually. Firstly, the number of individuals is defined making up the total population along with the maximum number of generations. Usually, the larger the population and the number of generations the more likely the algorithm will find the global minimum. However, creating a bigger population and maximum generation value requires more computational effort making the algorithm slower to run on suboptimal hardware. The population works following a set of predefined 'genetic rules' to minimize a cost function (Fitness Function) producing optimal controller gains for \(K_{r}\) (feedback gains) and \(K_{f}\) (Kalman gains). Each individual in the population is made up of a defined number of chromosomes. The number of chromosomes relates to the values of the matrices to be tuned. For example, a Full State Feedback controller used to tune a system with 3 inputs and 8 states will require a Q matrix of size 8 and an R matrix of size 3. The resulting individual will require 11 (8 + 3) chromosomes to tune the controller. Each chromosome is initially set to a random value for each individual if the algorithm is tuning from scratch. Alternatively, if the algorithm is improving a controller from previous approaches such as manual tuning the values of the control matrices are to be used as the starting point for 60% of the population with the other 40% being set to random to encourage exploration of new values. Each individual is then evaluated using the Fitness Function and ranked on its cost. There are 5 genetic rules to be followed:  
1. Elitism--The top 5% of the population advances to the next generation totally unchanged. 2. Replication--The top 5% of the population advances to the next generation and is replicated 19 times to make up 95% of the new population, this combined with
3. The Elite population makes up the total population. These individuals are modified using genetic rules 3 and 4. 4. Crossover--Two individuals are selected and are snipped at a defined section of their chromosomes; these snipped sections are swapped between the individuals. This process is used to exploit and enhance the existing successful strategies found by the algorithm. 5. Mutation--Chromosomes within the individual are changed to a random value. This promotes diversity in the population and increases the scope for exploration of new values. The new population is then evaluated on the same Fitness Function, as described in Section 3.4, Equations (11)-(13). The resulting costs are used to rank each individual, the new best 5% advances to the next generation. Steps 1-4 are repeated, and the evolution process continues until the maximum generation is reached where the algorithm will print the global optimum. The basic code used for the Genetic Algorithm along with the block diagram can be found in Pseudocode 2. ```  Initialize the control parameters (PopNum, ChromNum, CrossVal,  MutVal, MaxGen)  for\(1\): MaxGen do for each individual Calculate the cost of the individual based on Fitness Function endfor Rank individuals based on cost into ranked_pop Advance top 5% of ranked_pop and copy to match PopNum for\(1\): 0.95\"new_pop Crossover snip and cross at CrossVal Mutate the chromosome MutVal endfor Return Lowest Cost Return Gen Gen = Gen+1 endfor

## 4 Simulation Environment

To validate the proposed algorithms as an improvement to traditional methods for tuning controllers, a simulation environment was developed. This environment contains each of the optimal control structures outlined in Section 3.3, along with the benchmark PID controller.