Week 4 - Genetic Algorithm
OPTIMIZING STALAGMITE FUNCTION USING GENETIC ALGORITHM
AIM - To optimize stalagmite function using Genetic Algorithm in MATLAB
OBJECTIVE:-
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Explain the concept of genetic algorithm(ga) and also explain the syntax for ga in MATLAB
- Plot graph for F maximum vs no. of iterations.
THEORY:
- 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.
- 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.
- This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.
- A Stalagmite function is a mathematical model that helps in determining maxima and minima on a graph that appears like stalagmites.
- The stalagmite function basically works with certain formulas which are as shown below:
- These formulas have to be defined seperately to obtain the output of stalagmite function as they serve a criteria to obtain maximas and minimas.
- Stalagmite function provides the criteria to obtain fittest individuals from the group of individuals which is the general procedure of Genetic Algorithm.
- MATLAB provides an inbuilt function called 'ga' in order to obtain the offsprings as discussed above in Genetic Algorithm concept.
- The syntax of 'ga' function is given by,
- It finds a local unconstrained minimum,
'x', to the objective function,'fun'. Here, 'nvars' is the dimension (number of design variables) of the function.
CODES:-
clear all
close all
clc
%Creating search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Creating 2dimensional mesh
[X, Y] = meshgrid(x,y);
%Evaluating stalagmite function
for i = 1:length(X)
for j = 1:length(Y)
input_vector(1) = X(i,j);
input_vector(2)= Y(i,j);
f(i,j) = stalagmite_function(input_vector(1),input_vector(2));
end
end
surfc(X,Y,f)
shading interp
title('Genetic Algorithm')
xlabel('Iterations')
ylabel('Iterations')
zlabel('Values of Stalagmite Function')
[x, fval] = ga(@stalagmite_function,2);
CODE EXPLANATION:-
- As explained above to perform genetic algorithm, we need individuals from which we would select the fittest ones. So, we start defining such individuals as 150 equally spaced values from 0 to 0.6 along x & y direction.
- Then, with the help of meshgrid command we created matrices of x and y values as 'X' and 'Y'.
- In this way, we created all the individuals. Now, we would select the fittest individual from this group by a function which is defined as 'stalagmite_function'. We one by one take each value from the matrices with the help of 'for' loop and apply the 'stalagmite_function'. It is defined with some codes given by,
function [S] = stalagmite_function(X,Y)
f1_x = (sin(5.1.*pi.*X + 0.5)).^6;
f1_y = (sin(5.1.*pi.*Y + 0.5)).^6;
t1 = (X - 0.0667).^2;
t2 = (Y - 0.0667).^2;
f2_x = exp((-4*log(2).*t1)/0.64);
f2_y = exp((-4*log(2).*t2)/0.64);
[S] = f1_x.*f1_y.*f2_x.*f2_y;
end
- It just includes formulas shown above in stalagmite function. It takes inputs as 'X' and 'Y', which are the individuals taken one by one in 'for' loop.
- Then, the stalagmites formed are plotted with the code 'surfc' or the surface counter with shading interruption.
- At last, the 'ga' function is applied to perform mating and producing offsprings by genetic algorithm.
- On, executing the program, following graph is obtained.
ERRORS ENCOUNTERED:-
1)
- Here, 'f', which is the value of stalagmite function, is a constant. So, genetic algorithm cannot be performed with it.
- The correction required is 'f(i,j)' instead of 'f'. Then only 'f' would include different values, which shows different fitness values of individuals, from which fittest ones are selected.
2)
- This is not the required graph. It occured because the stalagmite function produced a value which is the only maximum.
- In the codes, the sine is taken as degree which resulted in the above graph. So, the correction would be to take sine as radian or use 'sin()' instead of 'sind()'.
CONCLUSION - Thus, we have performed Genetic Algorithm in MATLAB with the help of Stalagmite Function.