Week 4.1 - Genetic Algorithm
EXPLANATION:
AIM:
To find the global maxima for the stalagmite function using genetic algorithm in MATLAB.
INTRODUCTION:
Genetic Algorithm is derived from Charles Darwin's theory of evolution which states that ,"It is not the
strongest of the species that survives, nor the most intelligent, but the one most response to change". The entire concept of
Genetic Algorithm is based on the above famous statement.
Genetic Algorithm:
It is only used to generate minimum solution. So that the value of stalagmite function is took -ve to maximise
the function.
Genetic Algorithm (ga) Syntax:
1. x = ga(@func, nvars),
where "func" is the function name & nvars is the no. of variables.
2. x = ga(@func, nvars, A, B, Aeq, Beq),
A set of lower & upper bounds is given, so that the solution lies in the range'
3. x = ga(@func, nvars, A, B, Aeq, Beq, lb, ub, nonlcon, intcon, options),
A,B - linear inequality constraints
Aeq,Beq - linear equality constraints
lb - lower bound
ub - upper bound
nonlcon - non-linear constraints
intcon - vector of positive integers
options - optimisation options
STALAGMITE FUNCTION:
A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of
materials deposited on the floor from ceiling drippings. Stalagmite may be composed of lava, minerals, sinter, mud, pitch &
amber.
FORMULA:

FUNCTION CODE:
function f = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1 = (sin(5.1*pi*x+0.5))^6;
f2 = (sin(5.1*pi*y+0.5))^6;
f3 = exp((-4*log(2)*(x-0.0667)^2)/0.64);
f4 = exp((-4*log(2)*(y-0.0667)^2)/0.64);
f = -(f1*f2*f3*f4);
end
MAIN CODE:
clear all
close all
clc
%Defining the search space
x = linspace(0,0.5,150);
y = linspace(0,0.5,150);
num_cases = 50;
%creating a 2Dimensional 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
surfc(xx,yy,f);
xlabel('X axis')
ylabel('Y axis')
shading interp
% study1 - Statistical Behaviour
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
* ga - is an inbuilt function & always minise a function,
* @ - defines what function minise,
* 2 - denotes the no. of variables in the function, here it is x & y.
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
toc
% tic toc is used to measure how long is takes to study
figure(1)
subplot(2,1,1) % Multiple plots are same inside the figure window
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');
% study2 - Statistical Behaviour with upper & 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
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('Iterations');
ylabel('Function minimum');
% study3 - Increasing GA Iterations
tic
options = optimoptions('ga')
options = optimoptions(options,'populationsize',300)
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
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('Iterations');
ylabel('Function minimum');
OUTPUT:
STUDY 1:

STUDY 2:

STUDY 3:

This point of the peak is called the Global Maxima.
CONCLUSION:
* Hence by using the genetic algorithm, we were able to find the Global maxima points of the stalagmite function.
* By default in MATLAB, ga calculates the global minima. In order to make it shows the global maxima, we add a
-ve sign to 'f' & 'fopt' everywhere.
* Initially, the time required to run the program is prolong. We can reduce this time by reducing the limits.
Decreasing the populations size will get more accurate results.