Optimization of stalagmite function using Genetic Algorithm in MATLAB
In this project, we will determine the global maxima of the stalagmite function in the given search space using genetic algorithm.
The stalagmite function is defined as
So first we will write a program to plot the stalagmite function
%optimisation of the stalagmite function
clear all
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2d 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
%plot
surfc(xx,yy,f)
shading interp
Stalagmite Function
function [f] = stalagmite(input_vector)
ax = input_vector(:,1);
ay = input_vector(:,2);
f1x = (sin(5.1*pi*ax +0.5))^6;
f1y = (sin(5.1*pi*ay +0.5))^6;
f2x = exp((-2.77*((ax-0.0667)^2))/(0.64));
f2y = exp((-2.77*((ay-0.0667)^2))/(0.64));
f = f1x*f2x*f1y*f2y;
end
The output of this program gives us the plot for the stalagmite function
Now come to the second part, about the Genetic algorithm, Genetic algorithm is a MATLAB function which is used to predict the minima of a function, but here we are not concerned with the minima but we are concerned with the maxima so in order to get the maxima we have to modify our Stalagmite function, we will add a negative sign to our stalagmite function thus the maxima will become minima and we can obtain the minima using ga.
Modified Stalagmite function
function [f] = stalagmite(input_vector)
ax = input_vector(:,1);
ay = input_vector(:,2);
f1x = (sin(5.1*pi*ax +0.5))^6;
f1y = (sin(5.1*pi*ay +0.5))^6;
f2x = exp((-2.77*((ax-0.0667)^2))/(0.64));
f2y = exp((-2.77*((ay-0.0667)^2))/(0.64));
f = -f1x*f2x*f1y*f2y;
end
The output of the modified Stalagmite Function
Now we will do several case studies to understand the genetic algorithm
CASE STUDY - 1
The syntax of the genetic algorithm function is
x = ga(@fun,nvars) where fun is the function whose minima we have to find and nvars are the no of variables associated with the function.
The first approach is based on unbound search space, which means the search space is bounded for the function but it is not bounded for the genetic algorithm thus sometimes it detects a local minima and reports it as minima.
let us see by this program in which we will do repeat the program for 50 times and observe the minima obtained by the genetic algorithm
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
caseno = 50;
%creating a 2d 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
%study 1 unbounded
for i = 1:caseno
[ip1 fval(i)] = ga(@stalagmite,2)
xopt(i) = ip1(1);
yopt(i) = ip1(2);
end
%plot
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function minima')
The output of the CASE STUDY - 1
From the output of the 1st case study, we can see that the minima detected by the genetic algorithm are very bad the values are varying abruptly as shown in the plot.
CASE STUDY - 2
In this second case, we will define the upper and lower bound of the search space for the genetic algorithm and let see the results
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
caseno = 50;
%creating a 2d 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
%study 2 bounded
for i = 1:caseno
[inputs fval(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function minima')
The output of CASE STUDY - 2
Now in the output plots, we can see that the results are way more organized than CASE STUDY - 1 but here also we can see that the minima is varying from 0.7 to 1.0, now let me explain that ga finds a local unconstrained minimum to the objective function in place of searching for global minima in the search space.
CASE STUDY - 3
Now in this case 3 we will introduce the option property of ga,
Optimization options, specified as the output of optimoptions or a structure.
we will create options by using optimoptions to define the no of iteration, let's start with 10 iterations and see the results
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
caseno = 50;
%creating a 2d 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
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',10);
for i = 1:caseno
[inputs,fval(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
xlabel('x values')
ylabel('y values')
shading interp
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function minima')
The output of the CASE STUDY - 3
Now in 10 iterations, the results are as bad as case 1 so we will try for 100 iterations this time in
CASE STUDY - 4
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
caseno = 50;
%creating a 2d 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
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',100);
for i = 1:caseno
[inputs,fval(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
xlabel('x values')
ylabel('y values')
shading interp
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function minima')
The output of CASE STUDY -4
Now here we are getting something satisfactory the variation is very close this time this gives us an idea that if we increase the no of iterations the values will converge to a very close range.
CASE STUDY - 5
Now consider for 1000 iterations
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
caseno = 50;
%creating a 2d 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
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',1000);
for i = 1:caseno
[inputs,fval(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
xlabel('x values')
ylabel('y values')
shading interp
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function minima')
The output of the CASE STUDY - 5
Now, this is quite interesting the values obtained are very close but one thing we have to notice that with an increase in no of iterations the time elapsed is increasing.
CASE STUDY - 6
Now last time we will run the iteration for 5000 times though it will take a long time but let us see the results
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
caseno = 50;
%creating a 2d 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
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',5000);
for i = 1:caseno
[inputs,fval(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
xlabel('x values')
ylabel('y values')
shading interp
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function minima')
The output of the CASE STUDY - 6
Clearly we can see that the results are converged to a huge extent so we can say that the results will not change for many no of repetitions
So finally let's obtain the functional minima and the coordinate in the search space where the minima occur.
%optimisation of the stalagmite function
clear
close all
clc
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2d 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
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',5000);
[inputs,fval] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options)
surfc(xx,yy,f)
xlabel('x values')
ylabel('y values')
shading interp
So the minimum functional value is -1.0000 and the x value and y value of minima is (0.0668 , 0.0668)
That means the maxima of the stalagmite function in the search space [(0,0) , (6,6)] is 1.0000 and occurs at (0.0668 , 0.0668)