Week 4 - Genetic Algorithm
STALAGMITE FUNCTION TO FIND GLOBAL MAXIMA
AIM :
A program in matlab to optimise the stalagmite function to find the global maxima by using genetic algorithm.
EXPLANATION :
stalagmite is a type of speleothem that rises from the floor of a limestone cave due to the dripping of mineralized solutions and the deposition of calcium carbonate. Also an upward-growing mound of mineral deposits that have precipitated from water dripping onto the floor of a cave.

Genetic algorithm :
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.
Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions.
Stalagmite function :
the given formula which gives the fitness function of two inputs of x & y.
Program to slove stalagmite function :
function [results] = stalgamite(inputs)
x = inputs(1);
y = inputs(2);
f1x = (sin((5.1*pi*x) +0.5))^6;
f2x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f1y = (sin((5.1*pi*y) +0.5))^6;
f2y = exp(-4*log(2)*((y-0.0667)^2/0.64));
results = -((f1x)*(f2x)*(f1y)*(f2y));
end
The function is created to find the value of f(x,y) which always shows the value for local minima, so is multiplyed by (1-) to find the global maxima.
Implementation of the function into the main program,
clear all
close all
clc
% defining_search_space
x = linspace(0,1,150);
y = linspace(0,1,150);
num_iteration = 50;
% creating_2d_mesh
[xx,yy] = meshgrid(x,y);
% evalvating_stalgamite_function
for i = 1:length(xx)
for j = 1:length(yy)
inputs(1) = xx(i,j);
inputs(2) = yy(i,j);
f(i,j) = stalgamite(inputs);
end
end
% case_1
tic
% study1_statistical_behaviour
for i = 1:num_iteration
[inputs,fopt(i)] = ga(@stalgamite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time1 = 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('unbound inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function minimum')
% case_2
tic
% study2_statistical_behaviour_with_upper & lower_bounds
for i = 1:num_iteration
[inputs,fopt(i)] = ga(@stalgamite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time2 = 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')
% case_3
% increasing_GA_iteration
options = optimoptions('ga');
options = optimoptions(options,'populationsize',250);
tic
% study3_statistical_behaviour_buunded_inputs_with_increase_iteration
for i = 1:num_iteration
[inputs,fopt(i)] = ga(@stalgamite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time3 = 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 increasing iteration')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function minimum')
In this program we are solving the stalagmite by using ga syntax.
GA Syntax :
x = ga(fun , nvars , A , B , [ ] ,[ ] , lb , ub , nonlcon , intcon , options)
where, fun = given function.
nvars = total number of variables.
A = Linear inequality constraints specifed as real matrix. example, A*x <= B
B = Linear inequality constraints specifed as real vector.
Aeq = Linear inequality constraints specifed as real matrix in equation form. example, Aeq*x = Beq
Beq = Linear inequality constraints specifed as real vector in equation form.
lb = lower bound.
ub = upper bound.
nonlcon = non linear equation.
intcon = integer constrained.
options = specified as the output of optimoptions or a structure.
to create a 2D array for x,y we are using meshgrid command.
To study the satistical behaviour of stalagmite we assigning the value for number of iteration. And in the for loop we calculated the final output of x & y by using stalagmite function which is formulated seperately by ga syntax. then we make a plot by using surfc command which gives 3D plot, and plot3 command used to plot 3 variables in 3d. The subplot which is used to create a secondary plot in the same figure, the tic tok command used to predict the time taken. the shading interp command which remove the gridline on the surface of plots.
As per the 3 study the above process which continuous with different ga syntax.
In the third study we increasing the iteration population by using optimoption command,
example, optimoptions(oldoptions , name , value) which returns a copy of oldoptions with the named parameters altered with the specified values.
Comparing to first study the second study gives better optimum , also in third study by increasing iteration it gives the better optimum than second. we can predict the good optimum if we increase the number of study cases.
RESULT :
CONCLUSION :
Thus the program in matlab to optimise the stalagmite function to find the global maxima by using genetic algorithm for 3 study is sucessfully concluded.