%Optimize stalagmite function and find global maxima
close all
clear all
clc
% Defining the inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 50; %run ga 50times
% Creating 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
%Case 1 statistical behaviour (Unbounded)
tic %Starting time counter
for i = 1:num_case
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1); %Value of x predicted by ga
yopt(i) = inputs(2); %Value of y predicted by ga
end
study_time = toc % Ending time counter
%Plot
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f) % Plotting stalagmite function
shading interp % interpolated representation
xlabel('x-Axis')
ylabel('y-Axis')
hold on
%Plotting ga result
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title ('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
grid on
xlabel('Iteration')
ylabel('Max Function value')
% Case 2 insert bounding box
tic %Starting time counter
for i = 1:num_case
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc % Ending time counter
% Plot
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
xlabel('x-Axis')
ylabel('y-Axis')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title ('bounded inputs')
subplot(2,1,2)
plot(-fopt)
grid on
xlabel('Iteration')
ylabel('Max Function value')
% Case 3 increase population size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);
tic % starting time counter
for i = 1:num_case
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc % Ending time counter
% Plot
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
xlabel('x-Axis')
ylabel('y-Axis')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title ('Bounded with increase population size')
subplot(2,1,2)
plot(-fopt)
grid on
xlabel('Iteration')
ylabel('Max Function value')
% Case 4 increase population size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',600);
tic % starting time counter
for i = 1:num_case
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study4_time = toc % Ending time counter
% Plot
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
xlabel('x-Axis')
ylabel('y-Axis')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title ('Bounded with increase population size')
subplot(2,1,2)
plot(-fopt)
grid on
xlabel('Iteration')
ylabel('Max Function value')
display(input) % Display x and y co-ordinates
% Creating Stalagmite function
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin((5.1*pi*x)+0.5)).^6;
f1y = (sin((5.1*pi*y)+0.5)).^6;
t1 = ((x - 0.0667).^2)/0.64;
t2 = ((y - 0.0667).^2)/0.64;
f2x = exp(-4.*log(2)*t1);
f2y = exp(-4.*log(2)*t2);
f = -(f1x * f1y * f2x * f2y);
end