Week 5 - Genetic Algorithm
Objective of the Project - Writing a MATLAB code to optimize the Stalagmite function using Genetic algorithm and find the function’s global maxima. And also to plot the graph of F maximum v/s iterations for 3 different case studies.
Genetic Algorithm Introduction - A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.


Initial Population -
The population of individuals are maintained within search space. Each individual represents a solution in search space for given problem. Each individual is coded as a finite length vector (analogous to chromosome) of components. These variable components are analogous to Genes. Thus a chromosome (individual) is composed of several genes (variable components).

Fitness Function
The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
Operators of Genetic Algorithms
Once the
initial generation is created, the algorithm evolves the generation using
following operators –
1) Selection Operator: The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
2) Crossover Operator: This represents mating between individuals. Two individuals are selected using selection operator and crossover sites are chosen randomly. Then the genes at these crossover sites are exchanged thus creating a completely new individual (offspring). For example –

3) Mutation Operator: The key idea is to insert random genes in offspring to maintain the diversity in the population to avoid premature convergence. For example –

Genetic Algorithm (ga)Syntax -
x = ga(fun,nvars)-finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun, where, fun- objective function and nvars-number of variables.
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)- defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. When there is no linear inequalities we set Aeq and Beq as null vector.
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) requires that the variables listed in IntCon take integer values.
Where, A and b are A=[] and b=[] if no linear inequalities.lb and ub are lower bound and upper bound. Lower bounds, and upper bound specified as a real vector or array of doubles.
The set of equation used in stalagmite function is shown in below image.

Stalagmite Function -
function [f] = stalagmite(input_vector)
MATLAB code for Global Optimization -
Study #1 - Without Boundary Conditions
Stepwise explanation of the program
- For Global optimization using genetic algorithm, search space created using the linspace command, and meshgrid command is used to transform 1D in to 2D array
- A sub program is created to evaluate stalagmite function and result is stored in f variable. those variables are plotted to show the 3d representation of stalagmite function.
Output 1 -

Study 1 – Statistical behaviour (Without Boundary Conditions)
- In study #1 'ga' solver is iterated for 60 times, and in this case no limits are provided.
- 'ga' solver returns two outputs, one is the function value and other is the coordinates. Value of function is stored in 'fopt(i)' and coordinates are stored in 'xopt' and 'yopt' respectively. This loop is run for 60 times and results are plotted.
- Figure 1 holds two plots and equally dived using subplot command. one showing the 3d representation of stalagmite with optimization results and other showing the function value
- 'Plot3' command is used to plot a set of coordinates connected by line segments and Time taken for 60 iteration is measured using tic' and 'toc' command
- Results were found outside our search space (0 to 0.6) due to no upper and lower limit provided
Output 2 -

Study 2 – Upper and Lower Boundary Conditions
- In study #2 'ga' solver is run with upper and lower limits which will narrow the search space
- Four null vectors are passed in the solver syntax because there is no linear inequality involved in the stalagmite function.
- Again, the loop is run for 60 times and 'Plot3' command is used to plot a set of coordinates connected by line segments and Time taken for 60 iteration is measured using tic' and 'toc' command
Output 3 -

Study 3 – With Increased population
- In study #3 the 'ga' solver is run with the bounded inputs along with the increased population size -120
- Population Size specifies how many individuals are there in each generation. With a large population size, the genetic algorithm searches the solution space more thoroughly, thereby reducing the chance that the algorithm returns a local minimum that is not a global minimum. However, a large population size also causes the algorithm to run more slowly.
- Optimoptions command is used to create optimization options.
- 'Plot3' command is used to plot a set of coordinates connected by line segments and Time taken for 60 iteration with increased population size is measured using tic' and 'toc' command
Output 4 -
Output 5 - Population size further increased to 240

Output -6 - Final results with study time of all cases

Error - Facing following error while solving ga, After add-on the 'Global Optimization Toolbox" I can able to solve the Ga

References -
1.https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html
2.https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html#f6243
3.https://www.youtube.com/watch?v=MLPHLm56inQ&t=3s
4.https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3
5.https://www.geeksforgeeks.org/genetic-algorithms/
6.https://in.mathworks.com/help/gads/ga.html#mw_4a8bfdb9-7c4c-4302-8f47-d260b7a43e26