PROGRAM TO CALCULATE THE GLOBAL MAXIMA OF A STALAGMITE FUNCTION USING GENETIC ALGORITHM
Objective 1: Genetic algorithms are a type of optimization function, used to find the optimal solutions to a given problem that maximizes or minimizes a particular function. We can mutate two or more functions and by using Genetic Algorithm to create a new function, using random variables.
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
f = get_stalagmite(x,y);
function [f] = get_stalagmite(x,y)
[xx,yy] = meshgrid(x,y);
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
end
function [f] = stalagmite(input_vector);
x=input_vector(1);
y=input_vector(2);
term_1=(sin(16.02*x + 0.5))^6;
term_2=(sin(16.02*y + 0.5))^6;
term_3 = exp((-4*log(2)*(x-0.0667)^2)/0.64);
term_4 = exp((-4*log(2)*(y-0.0667)^2)/0.64);
f = term_1*term_2*term_3*term_4;
end
%Study1 - Statistical behavior
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(`Function Minimum\')
tic
%Study2- Statistical behaviour - with upper and lower bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,`marker`,`o`,`markersize`,5,`markerfacecolor`,`r`)
title(`Bounded Inputs`)
subplot(2,1,2)
plot(fopt)
xlabel(`Iteration`)
ylabel(`Functions Minimum`)
%Study3_time Increasing GA iterations
options = optinoptions(`ga`)
options = optinoptions[options,`PopulationSize`,170);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iteration\')
ylabel(\'Functions Minimum\')