Week 4.1 - Genetic Algorithm
AIM:
Try to understand the concept of genetic algorithm by referring to varius sources before you attend this challenge. Look out for information regarding stalagmite function and ways to optimise it.
STALAGMITE:
Stalagmite is an upward-growing mound of mineral deposite that have precipitated from water dripping onto the floor of a curve. Most stalagmites have rounded or flattened tips. A stalagmite is a type of rock formation that rises from the floor of a curve due to the accumlation of material deposited on the floor from celling drippings. stalagmites are typically composed of calcium carbonate, but many consist of lava, mud, peat, pitch, sand and amberat

X and Y are going to be the inputs of the co-ordinates for which the global maximum and lical maximum will be calculated.
Then to find the global maximum while using the genitic alogorithm we need to include negative('-ve') sign while evaluating the function ,plotting the 3d model the function values and also in optimum value while plotting in plot3 command.
INTRODUCTION OF GENETIC ALGORITHM:
The genetic algorithm is a method for sloving both constrained and unconstrained optimization problem that are based on natural selection, the process that drives bological evolution. The genetic algorithm repeatedly modifies population of individual solution. At each step, the genetic algorithm selects individuals at random from the currend population to be parents and use them to produce the children for the next generation. Over successive generation, the population evolves twoward an optimal solution. You can apply the genetic algorithm to slove a variety of optimization problems that are nor well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm canaddress problems or mixed integer programming, where some componends are restricted to be integer-valued.
The geneitc algorithm uses three main types of rules at each step to create the next generation from the next generation from the current population
selection rules:
Selection the individulas, called parents, that contribute to the population at the next generation.
crossover rules:
Crossover rules combine two parents to from children for the next generation.
Mutation rules:
Mutation rules apply random changes to individual parents to from children
The above principle is used in the implementation of the 'ga' function. A random set of values are chosen from a swarm of values of a function for the purpose of achieving a higher generation of values. Then the next set of values are chosen from the generation of values. A similar operation is performed until a threshold is achived and the function returns the minimum function value and the corresponding dependent value

'ga' syntax in matlab
Following syntex are used to find 'ga' in matlab.
- x=ga(fun,nvars,) : This is find a local unconstrained minimum,x,to the objective function,fun,nvars is the dimension [number of variables]of fun.
- x=ga(fun,nvars,A,b,) : This is find the local minimum x to fun,subject to linear inequlietes A*x ≤ b. ga evaluate the matrix product A *x as if x is transposed(A*x').
- x=ga(fun,nvars,A,b,Aeq,beq,) : This find local minimum x to fun, subject to the linear equlites Aeq*x = beq and a*≤b(se: A =[] and b=[] if non linear equlites exist) ga evaluates the matrix product Aeq*x as if x is transopsed (Aeq*x')
- x=ga(fun,nvars,A,b,Aeq,beq,lb,up): This defind as set of lower and upper bounds on the design variables,x,so that a solution found in the range lb ≤ x≤ up. (set Aeq = [] and beq=[] if no linear equlites exist)
- x=ga(fun,nvars,A,b,Aeq,beq,lb,up,nonicon,) : This subject the minimization to the constrains defind in nonicon. The function nonicon accepts x and returns vectors c and ceq, represnding the nonlinear inequlites and equlites respectively. ga minimizes the fun such that c(x) ≤ 0 and ceq(x) =0. (set lb =[] and up[] if no bounds exist.
- x=ga(fun,nvars,A,b,Aeq,beq,lb,up,nonicon,option): This subject the minimization to the constrains defind in nonicon. The function nonicon accepts x and returns vectors c and ceq, represnding the nonlinear inequlites and equlites respectively. ga minimizes the fun such that c(x) ≤ 0 and ceq(x) =0. (set lb =[] and up[] if no bounds exist.
- x=ga(fun,nvars,A,b,[],[],lb,up,nonicon,lntcon) or x=ga(fun,nvars,A,b,[],[],lb,up,nonicon,lntcon,option): This is requried that the variables listed in lntcon take integer values.
- x = ga (problem): This is find the minimum for problem, where problem is a structure.
- [x,fval] = ga(_ _): For any previous input arguments, also returns fval, the value of the fitness function at x
- [x,fval,exitflag,output]=ga(_ _): This also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.
- [x,fval,exitflag,output,population,scores]=ga(_ _): This is also returns a matrix population, whose row are the final population, and a vector scores, the scores of the final population.
INPUT ARGUMENTS:
- fun- Objective function (given by function handle or by function name)
- nvars- Number of variables (Given by positive integer)
- A- Linear inequlity constrains(Give by real matrix)
- b- Linear inequlity constrains(Give by real matrix)
- Aeq- Linear equlity constrains(Give by real matrix)
- beq- Linear equlity constrains(Give by real matrix)
- lb- lower pounds (Real vector or array)
- up- upper pounds (Real vector or array)
- nonlcon- Nonlinear constrains
- options- Optimization options
- lntcon- integer variables
- problem- Problem description
OUTPUT ARUGUMENTS:
- x- Solution (real vector)
- fval- Objective function value at solution (real number)
- exitflag- Reason ga stopped (integer)
- output- Information about optimization process (structure)
- population- Final population(matrix)
- scores- Final scores (column vector)
PROGRAM
STALAGMITE FUNCTION
OUTPUTS


STEPS:
- Since X and Y values are initialized and another value num_cases is initialized to any number which is used to iterate the 'ga' function so many times.
- A meshgrid is created using X and Y values using 'meshgrid' function which gives two sruare matrices.
- A new matrix is assigned to the 'stalagmite' function whose inputs are set fo coordinates which are generated by keeping this in two for loops. The outer for loop navigates the first squrare matrix generated by meshgrid and second for loops navigates the second square matrix. This generates a ste of values in the 3rd dimension.
- Now a similar operation as the previous step is performed using stalagmite function. Stalagmite funciton generate values in the positive Z direction.
- Now ga function is implemented to stalagmite because stalagmitegenerates values in the negative Z direction. The ga function returns the minimum of the stalagmite1 function and their corresponding dependent variables
- The minimum value by van multiplied with -1 to get positive number and ca be plot on the function surface of stalagmite functionand hence the maximum and their coresponding X and Y values can shown. The ga function is iterated several times to geta range of maximum values and most maximum values is choosen.
- The similar procedure is followed in study 2 where the limiting values of Xand Y axes are shown on the plot.
- In the 3rd study, the number of experimental subjects which the ga function picks at a time is increased to increase the accuracy