Week 4.1 - Genetic Algorithm
AIM : Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
FUNCTION PROGRAM : function stalagmite.
function[f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
term1 = pi*5.1*x;
term2 = pi*5.1*y;
term3 = ((x-0.0667)^2/0.64);
term4 = ((y-0.0667)^2/0.64);
term5 = (-4)*log(2);
xf1 = (sin(term1+0.5)).^6;
yf1 = (sin(term2+0.5)).^6;
xf2 = exp(term5*term3);
yf2 = exp(term5*term4);
f = -(xf1.*yf1.*xf2.*yf2);
end
global maxima of the function :-
clear all
close all
clc
% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 150;
% Creating a 2dimensional mesh
[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(input_vector);
end
end
tic
% study 1 - satistical behaviour
for i = 1: num_cases
[inputs,f_opt(i)]=ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(j) = inputs(2);
end
study1_time = toc
% ploting the graph
figure(1)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-f_opt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-f_opt)
title('unbounded inputs')
xlabel('iteration')
ylabel('function minimum')
tic
% study 2 -satistical behaviour - with upper and lower bounds
for i = 1:num_cases
[inputs,f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(j) = inputs(2);
end
study_time_2 = toc
% ploting the graph
figure(2)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-f_opt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-f_opt)
title('bounded inputs')
xlabel('iteration')
ylabel('function minimum')
% study 3 - increased GA iteration
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',170);
tic
for i = 1:num_cases
[inputs,f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs(1);
yopt(j) = inputs(2);
end
study_time_3 = toc
% ploting the graph
figure(3)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-f_opt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-f_opt)
title('bounded inputs')
xlabel('iteration')
ylabel('function minimum')
output :-
GRAPH 1]
GRAPH 2]

GRAPH 3]

workspace output :-
