Week 4.1 - Genetic Algorithm
AIM-
- Genetic Algorithm
OBJECTIVE-
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Make sure that your code gives the same output, even if it is made to run several times.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
THEORY-
- Genetic Algorithm is based on random sampling which means it start with random number of inputs and based upon input we evaluate function.
- When we run GA code, we will get different results.
- Stalagmite, mathematical model helps to generate local minima and maxima values.
- Optimiza is used to calculate Global maxima value.
- Meshgrid is used to convert 1D array into 2D array.
- GA is used to calculate minimum value and for calculate maxima value, we need to put minus sign to stalagmite equation.
- Command Tic-Toc, used to calculate total time require to run code.
PROGRAM-
A) Genetic Algorithm
clear
close all
clc
% defining
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% Creating a 2D mesh
[xx, yy]= meshgrid(x,y);
% evaluating the stalagmite function
for i=1:length(xx)
for j=1:length(xx)
input_vector(1)= xx(i,j);
input_vector(2)= yy(i,j);
f(i,j)= stalagmite(input_vector);
end
end
surfc(x, y, -f)
xlabel('X value')
ylabel('Y value')
shading interp
[inputs, fval]= ga(@stalagmite,2)

B) STALAGMITE FUNCTION
function z= stalagmite(inputs)
x=inputs(1);
y=inputs(2);
term1 = (sin(5.1*pi*x+0.5)).^6;
term2 = (sin(5.1*pi*y+0.5)).^6;
term3= exp(-4*log(2)*(x-0.0667)^2/0.64);
term4= exp(-4*log(2)*(y-0.0667)^2/0.64);
z = -term1*term2*term3*term4;
end
C) MAIN GA STATISTICS-
clear
close
clc
% defining variables
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
% Creating a 2D mesh
[xx, yy]= meshgrid(x,y);
for i=1:length(xx)
for j=1:length(xx)
input_vector(1)= xx(i,j);
input_vector(2)= yy(i,j);
f(i,j)= stalagmite(input_vector);
end
end
tic
% % study 1- satistical behavior
for i= 1: num_cases
[inputs, fopt(i)]= ga(@stalagmite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt, yopt, fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
% % Study 2- Satistical behavior with upper anf lower bounds
tic
for i= 1: num_cases
[inputs, fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time= toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt, yopt, fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('Function minimum')
% % Study 3
options= optimoptions('ga');
options= optimoptions(options,'PopulationSize',170);
tic
for i= 1: num_cases
[inputs, fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time= toc
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt, yopt, fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('Function minimum')
% [inputs,fval]= ga(@stalagmite,2)



ERRORS-
- To calculate maxima value, we need to put minus sign to main function equation.

CONCLUSION-
- Genetic Algorithm is simple command to calculate minima and/or maxima value from global population data.