# Functions

|      **Function Name**      |                       **Description**                        | **Input Arguments**                                          | **Return Values**                                            |
| :-------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------- | :----------------------------------------------------------- |
|    _CreatePlantObject()_    | Creates an first order inertia plant model,  and return it as a transfer function object. | - Plant Gain <br />- First-order Coefficient <br />- Delay Coefficient | - Plant Object                                               |
| _CreateInitialPopulation()_ | This function creates a 3xPopSize matrix which is PopSize is triples count of PIDs. | - Population Size                                            | - A matrix contains 3 rows for P, I and D with PopSize column |
|    _SimulatePIDSystem()_    | A function to create a first order inertia system with PID controller, and simulates it using step function. | - Reference Value <br />- PID Gains (3x1 vector) <br />- Plant Model (tf data) | - Y values of the response<br />- X values of the response   |
|   _AnalyseSystemResult()_   | It looks for overshoot error, time to get stability, and end-error for given time of [Y, X] response for a controller system. | - Response Time Vector <br />- Response Amps Vector <br />- Reference Value <br />- Time to Check Error | - Overshoot Error<br /> - Time to Stabilize<br />- Error at the Time |
|     _FitnessFunction()_     | This function converts the overshoot error, stabilization time, and error for given time into a scale to use it as fitness of each individual.  <br />`f(O, S, E) = -(0.5*S + 0.4*O + 0.1*E)` | - Plant Model (tf data) <br />- Reference Value <br />- PID Gains (3x1 vector) | - Fitness Value (Note: as it gets higher, it is better. >0 is quite enough for a controller system.) |
|  _ProcessAndSortFitness()_  | The function returns a matrix, which is sorted and has four row. The first three row is PID  gains, and the last one is the FitnessFunction value. | - PID Population (a matrix of 3xPopSize) <br />- Plant Model (tf data) <br />- Referance Value | - Sorted PID Population (a matrix of 4xPopSize)              |
|     _SelectionMethod()_     | This function returns the pairs of individuals  selected for crossovering in Classic Genetic  Algorithm. | - PID Population <br />- Selection Pair Count                | - Group 1: "Fathers"<br /> - Group 2: "Mothers"              |
|     _PIDtoBinaryGens()_     | The function converts three float number into its chromosome form which has binary gens. | - PID Individiual (3x1)                                      | - Combined One (useful)<br /> - Seperated One (to see decimal and float seperatly) |
|     _BinaryGenstoPID()_     | It converts the binary chromosomes into the decimal values of PID gains. | - Combined Chromosome                                        | - PID Individiual (3x1)                                      |
|      _CGA_Crossover()_      | It applies crossover with given minimum needed chance rate for two chromosomes. | - Chromosome1 <br />- Chromosome2 <br />- Minimum Chance Rate | - Child Chromosome                                           |
|      _CGA_Mutation()_       | It applies mutation with given minimum needed chance rate for the chromosome. | - Chromosome <br />- Minimum Chance Rate                     | - Mutated Chromosome                                         |
|      _CGA_Inversion()_      | It applies inversion for one part of the chromosome with given minimum needed chance rate. | - Chromosome <br />- Minimum Chance Rate                     | - Inverted Chromosome                                        |
| _ClassicGeneticAlgorithm()_ | This function uses Classic Genetic Algorithm to find best PID gains for the given plant. It uses Elite Selection Method for selecting parents to use it in crossovering. | - Plant Model (tf data) <br />- Population Size <br />- Referance Value <br />- Fitness Reached to End Simulation<br />- Maximum Generation Count for the Same Best PID to End the Simulation <br />- Min. Chance of Crossover <br />- Min. Chance of Mutation <br />- Min. Chance of Inversion | - Best PID Individiual<br />- Fitness Value of Best<br />- Total Generation Created |