clear all
close all
clc
% Defining our search engine
X = linspace(0, 0.6, 150);
Y = linspace(0, 0.6, 150);
num_spaces=50; % No of iteration for which the function will evaluate the fitness
% Creating a 2D mesh
[XX,YY] = meshgrid(X, Y);
% Evaluating the stalgamite 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); % Values of function in given search space
end
end
surfc(XX, YY, -f); % Generating the surface
shading interp % To get Smooth plot
[X, fval] = ga(@stalagmite, 2);
%study-1(unbound)
tic % Calculating the study time through tic and toc command
for i= 1:num_spaces
% Calculating the Optimized value of the function
[results, fopt(i)] = ga(@stalagmite,2);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time1 = toc % displaying the time taken to evaluate
figure(1)
subplot(2, 1, 1)
surfc(XX, YY, -f)
shading interp
hold on
plot3(xopt, yopt, -fopt, 'marker', 'o')
subplot(2, 1, 2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% %study-2 (bounded)
tic
for i=1:num_spaces
[result, fopt(i)] = ga(@stalagmite, 2, [], [], [], [], [0, 0], [0.6, 0.6])
xopt(i) = result(1)
yopt(i) = result(2)
end
study_time2 = toc
figure(2)
subplot(2, 1, 1)
surfc(XX, YY, -f)
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')
%study-3(increased initial population)
tic
options=optimoptions('ga')
% increasing population size
options=optimoptions(options, 'populationsize', 250)
for i = 1:num_spaces
[results, fopt(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0],[0.6;0.6], [], [], options)
xopt(i) =result(1);
yopt(i) = result(2);
end
study_time3 = toc
figure(3)
subplot(2, 1, 1)
surf(XX, YY, -f)
shading interp
hold on
plot3(xopt, yopt, -fopt, 'marker', 'o')
subplot(2, 1, 2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
STALAGMITE FUNCTION
function A = 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);
A = -(f1*f2*f3*f4);