Week 4.1 - Genetic Algorithm
Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Optimization: The activity of enhancing system capabilities and integration of subsystem elements to the extent that all components operate at or above user expectations. In other words an act, process, or methodology of making something (such as a design, system, or decision) as fully perfect, functional, or effective as possible specifically : the mathematical procedures (such as finding the maximum of a function) involved in this.
Stalagmite Function: Stalagmites have thicker proportions and grow up on the bottom of a cavern from the same drip-water source, the mineral from which is deposited after the water droplet falls across the open space in the rock. Not every stalactite has a complementary stalagmite, and many of the latter may have no stalactite above them. Where the paired relation exists, however, continual elongation of one or both may eventually result in a junction and the formation of a column.
Stalagmite function is derived by

1. Understanding genetic algorithm
The genetic algorithm is inspired by the process that drives biological evolution. The process of evolution starts with the selection offittest individuals from a population. Then they produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving.
This process keeps on iterating and at the end, a generation with the fittest individuals will be found. And this is the main working principle for the algorithm because at each step, the genetic algorithm selects randomly from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population evolves toward an optimal solution.
2. The genetic algorithm (ga) can be applied to solve a variety of optimization problems that are not well suited for standard
optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
i. Selection rules select the individuals, called parents, that contribute to the population at the next generation.
ii. Crossover rules combine two parents to form children for the next generation.
iii. Mutation rules apply random changes to individual parents to form children.
Working principle of genetic algorithm
The working of a genetic algorithm is also derived from biology, which is as follows,
1) Initialization.
2) Fitness Assignment.
3) Selection.
4) Cross Over.
5) Mutuation.
6) Stopping Criteria.
Syntax of Genetic Algorithm (ga) in matlab:
x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)
fitnessfun = fitness function
nvars = Number of design variable
Aineq = A matrix for linear inequality constrain
Bineq = B matrix for linear inequality constrain
Aeq = Aeq matrix for linear equality constrain
Beq = Beq matrix for linear equality constrain
lb = Lower bound
ub = upper bound
nonlcon = Non linear constrain function
solver = ga
options = options using create optimotion
[x,fval] = x-value (x,y);fval-min/max value
Main Program
Then num_cases is set which will be used as counter for iterations to 60.
Then a 2D grid is made using meshgrid command is created from x and y input to be outputted to xx and yy.
Then for loop is used to solve the genetic algorithm for every element in the 2D mesh grid.
The length of rows and columns are used as counters using command length().
Then the input vector is declared as ga requires input vector which is each value in the mesh grid
Then output of stalagmite function is stored in matrix.
Then tic command is used to start a timer
Then for loop bounded by the number of iterations is formed.
Then ga code is entered for unbounded statistical study which have the following format
[inputs,fopt] = ga(fun,nvars).
Inputs represent the values of parameters x and y for which the function has the least value .
(fopt)function - The function to be optimized, in this case Stalagmite
(nvars) - the number of design variables, in this case only x and y are the variables to be plotted.
Then smallest (minimum ) value of the function for each iteration is chosen as output and its x and y coordinates, Which are stored
in separate variable.
Then toc command is used to stop the timer and the output is stored in study_time 1.
Then figure 1 is plotted by the following steps:
one column are specified and then the position of each plot is selected.
Then Hold on command is used to prevent plot from being removed.
The surfc command is used to plot contour with xx value and yy value from the mesh grid as input and negative optimum value is
used because we need to maximize the function.
Then x, y ,and z axis are labeled.
Then shading interp is used to smoothen the function plot and to remove the grid lines.
Then plot 3 command is used to plot a point in 3 dimensions, here it will plot -fopt because we need the maximum value of the
function, not the minimum.
Then the graph title is selected,and -fopt value of the function is plotted on the second plot in the second row.
Then its x and y axis are labeled.
Then the same study is made but this time it is bounded (has lower and upper limit) which requires the same commands and
steps except the ga command which have the following format:
[inputs,fopt] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
Inputs, fopt,fun and nvars are the same as Study 1A -
Linear inequality constraints : real matrix, if there are none,then []b - Linear inequality constraints : real vector, if there are none, then
[]Aeq - Linear equality constraints : real matrix, if there are none, then []beq - Linear equality constraints : real vector, if there are
none, then []lb - Lower bounds, specified as a real vector or arrayub - Upper bounds, specified as a real vector or array.
The range of the genetic algorithm is Bounded by introducing values for lower and upper bounds and hence the values of global
maxima and the values of x and y will be within the defined search space in inputs.
Then increasing population size study was done which is the same as before but it had different ga code of:
[inputs,fopt]=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
Inputs, fopt,fun and nvars,A,b,Aeq,beq,lb,ub are the same as Study 1 and Study 2 respectively)
nonlcon - Nonlinear constraints, specified as a function handle or function name.
If there are none,then []IntCon - Integer variables, specified as a vector of positive integers, if there are none, then []Options -
Optimization options, specified as the output of optimoptions or a structure.
The range of the genetic algorithm is Bounded as in Study 2 but to improve the accuracy, the commands optimoptions is
introduced to increase the Population Size.Then total time is computed.
Then using fprintf command the results are printed in the command windows.Then the study time is printed.
Then x,y coordinates of the maximum optimum value are obtained.
Then the optimum value of the function (maximum value) is printed which is calculated from the (negative mean) of the all iterations of the output value ,and the negative sign is used to get the maximum value.



