
% Function code genetic algorithm
function [f] = stalagmite_fun(input_vector)
fx = (sin(5.1*pi*input_vector(1)+0.5))^6;
fy = (sin(5.1*pi*input_vector(2)+0.5))^6;
fxx = exp((-4*log(2)*(((input_vector(1)-0.0667)^2)/0.64)));
fyy = exp((-4*log(2)*(((input_vector(2)-0.0667)^2)/0.64)));
f = -(fx.*fy.*fxx.*fyy);
end









% Genetic Algorithm
% Main Code
clear all
close all
clc
% Defining search space
x = linspace(0, 0.6, 150);
y = linspace(0, 0.6, 150);
num_cases = 50;
% Creating 2D Mesh
[xx,yy] = meshgrid(x,y);
% Using For Loop
for i = 1:length(xx) % 150 times
for j = 1:length(yy)
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite_fun(input_vector);
end
end
% Creates three dimensional surface
figure(1)
surfc(x,y,-f)
shading interp % It makes the surface more smoother
colorbar
xlabel('value of x')
ylabel('value of y')
title('Stalagmite Function')
% 1st case
tic
% Using For Loop
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_fun,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
% fopt = -fopt;
time_study1 = toc
% Plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
title('Unbounded Inputs')
xlabel('Value of x')
ylabel('Value of y')
shading interp
hold on
plot3(xopt,yopt,-fopt,'Marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
% 2nd case
tic
% Using For Loop
for i = 1:num_cases
[inputs2,fopt2(i)] = ga(@stalagmite_fun,2,[],[],[],[],[0,0],[0.6,0.6]);
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
% fopt = -fopt;
time_study2 = toc
% Plotting
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs')
xlabel('Value of x')
ylabel('Value of y')
hold on
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','g')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iteration')
ylabel('Function Maximum')