Optimization of Stalagmite Function using Genetic Algorithm
Title: Optimization of Stalagmite Function using Genetic Algorithm.
Objective : Write a code to calculate the global maxima of a stalagmite function.
Genetic Algorithm : Genetic Algorithm is a function i.e \'ga\', it always tries to minimize the given fitness function. Genetic Algorithm can be used whenever a certain function needs to be minimized.The algorithm repeatedly modifies a population of individual soloutions.
The syntax for Genetic Algorithm is
[input,fval]=ga(fitnesfcn,nvars,A,b,c,d,LB,UB,nonlinear,options)
- fitnesfcn - It means function for which solution value should be satisfied.
- nvars - number of designed variables
- A,B - they are equality constrained
- C,D - they are inequality constrained.
- LB - Lower boundry
- UB - upper boundry
- ninlinear - non-linear constrained
- options - it is used to call all other parameters.
Matlab code to optimise the Stalagmite function.
Function Code :
function[F]= 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));
F=-(f1.*f2.*f3.*f4);
end
Program Code :
clear all
close all
clc
%Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2d Mesh
[xx yy] = meshgrid(x,y);
num_case=150
%Evaluating the Stalagmite function
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
F(i,j)= stalagmite(input_vector);
end
end
%Case 1 unbounded values
tic
for i=1:num_case
[inputs,Fopt(i)]=ga(@stalagmite,2);
xopts(i)=inputs(1);
yopts(i)=inputs(2);
end
study_time_1=toc
%plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
xlabel(\'X data\')
ylabel(\'Y Data\')
plot3(xopt,yopt,Fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
title(\'Unbounded inputs\');
subplot(2,1,2)
plot(Fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')
max_value=(Fopt);
%case 2 bounding
tic
for i=1:num_case
[inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopts(i)=inputs(1);
yopts(i)=inputs(2);
end
study_time_2=toc
%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
xlabel(\'X data\')
ylabel(\'Y Data\')
plot3(xopt,yopt,Fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
title(\'case 2(bounded inputs)\');
subplot(2,1,2)
plot(Fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')
max_value=(Fopt);
%case 3 bounding and Iso increasing the population by 500
options=optimoptions(\'ga\');
options=optimoptions(options,\'populationsize\',500);
tic
for i=1:num_case
[inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopts(i)=inputs(1);
yopts(i)=inputs(2);
end
study_time_3=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
xlabel(\'X data\')
ylabel(\'Y Data\')
plot3(xopt,yopt,Fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
title(\'case3(Bounded inputs with increased in population size)\');
subplot(2,1,2)
plot(Fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')
max_value=(Fopt);
Study 1 : Time taken is 11.92
Study 2 : Time taken is 18.21
Study 3 : Time taken is 137.13
Workspace values
Result : Hence this is the output we have recieved .
link: https://drive.google.com/file/d/19m5_o5-dGAOCLWAPJ70V9ocG_i3HX1_0/view?usp=sharing