Week 4 - Genetic Algorithm
Aim : TO find global maxima for stalagmite function.
Theory :
Genetic Algorithm is a search heuristic inspired by Charles Darwin's theory of Evolution. This algorithm reflects theory of natural selection where the fittest individual is selected to reproduce in order to produce offspring of next generation.
There are 5 phases of genetic algorithm
1. Initial population
2. FIttest function
3. Selection
4. Crossover
5. Mutation
Stalagmite Function Code
function [f] = stalagmite(input_vector)
x= input_vector(1);
y= input_vector(2);
term1 = (sin(5.1*pi*x + 0.5))^6;
term2 = exp(-4*log(2)*(x-0.0667)^2/(0.64));
term3 = (sin(5.1*pi*y + 0.5))^6;
term4 = exp(-4*log(2)*(y-0.0667)^2/(0.64));
f = -1*(term1*term2*term3*term4);
end
% Negative sign is to find maxima as GA defines minima
Case 1 : Unbound inputs
Case 1 : Unbound Inputs
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% Creating a 2D mesh
[xx yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1)= xx(i,j);
input_vector(2)= yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end
% iteration for genetic algorithm
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i)= inputs(1)
yopt(i)= inputs(2)
end
toc
% plotting
figure(1)
subtitle('Genetic Algorithm unbound')
subplot(2,1,1);
hold on
surfc(xx,yy,-f)
shading interp
title('Genetic Algorithm unbound')
plot3(xopt,yopt,-fopt,'marker','o','markersize',3);
xlabel('x axis');
ylabel('y axis');
zlabel('f');
subplot(2,1,2);
plot(-fopt);
xlabel('iterations');
ylabel('Global maxima');
Case 2 : Bound Inputs
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% Creating a 2D mesh
[xx yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1)= xx(i,j);
input_vector(2)= yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end
% iteration for genetic algorithm
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); %bound inputs
xopt(i)= inputs(1)
yopt(i)= inputs(2)
end
toc
% plotting
figure(1)
subtitle('Genetic Algorithm bound inputs')
subplot(2,1,1);
hold on
surfc(xx,yy,-f)
shading interp
title('Genetic Algorithm bound inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',3);
xlabel('x axis');
ylabel('y axis');
zlabel('f');
subplot(2,1,2);
plot(-fopt);
xlabel('iterations');
ylabel('Global maxima');
Case 3 : Bound inputs with increased population
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% Creating a 2D mesh
[xx yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1)= xx(i,j);
input_vector(2)= yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end
% define population
options = optimoptions ('ga')
options = optimoptions(options ,'populationsize',250);
% iteration for genetic algorithm with increased population
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); %bound inputs
xopt(i)= inputs(1)
yopt(i)= inputs(2)
end
toc
% plotting
figure(1)
subtitle('Genetic Algorithm bound inputs with increased population')
subplot(2,1,1);
hold on
surfc(xx,yy,-f)
shading interp
title('Genetic Algorithm bound inputs with increased population')
plot3(xopt,yopt,-fopt,'marker','o','markersize',3);
xlabel('x axis');
ylabel('y axis');
zlabel('f');
subplot(2,1,2);
plot(-fopt);
xlabel('iterations');
ylabel('Global maxima');
Conclusion : Thus the stalagmite function has been used by the genetic algorithm to find the global maxima with a series of steps
References
https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html
https://www.researchgate.net/post/How-can-you-find-the-max-function-with-Genetic-Algorithm