Week 4.1 - Genetic Algorithm
Stalagmite function:
function [f]= stalagmite_func(input_vector)
x = input_vector(1);
y = input_vector(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);
f = -(term1*term2*term3*term4);
end

genrtic algoritham for stalagmite function:
clear all
close all
clc
% Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; % no. of iterations
[xx,yy]=meshgrid[x,y];
% Evaluating 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_function(input_vector);
end
end
tic
% Case 1 - Statistical Behaviour
for k = 1:n
[inputs1, fopt1(k)] = ga(@stalagmite_func, 2);
xopt1(k) = inputs1(1);
yopt1(k) = inputs1(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surkfc(xx,yy,-f)
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('y values')
title('Statistical Behaviour')
figure(2)
plot(-fopt1)
xlabel('Interations')
ylabel('fopt')
title('Statstical Behaviour')
%Case 2 - Statistical Behaviour - with upper and lower bound
for k = 1:n
[inputs2 , fopt2(k)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
xopt2(k) = inputs2(1);
yopt2(k) = inputs2(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour - with upper and lower bound')
figure(4)
plot(-fopt2)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour - with upper and lower bound')
% Case 3 - Statistical Behaviour - with increasing GA iterations
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',300)
for k =1:n
[inputs3, fopt3(k)] = ga(@stalagmaite_func, 2,[],[],[],[],[0;0],[1;1],[],[],options)
xopt3(k) = inputs3(1);
yopt3(k) = inputs3(2);
end
figure(5)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','makerfacecolor','r')
xlabel('X values')
ylable('Y values')
title('Statistical Behaviour - with increasing GA iterations')
figure(6)
plot(-fopt3)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour - with increasing GA interations')
