Optimization of stalagmite function with genetic algorithm
AIM
Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
THEORY
To find out the maxima of stalagmite function using genetic algorithm we have to inverse the solution of stalagmite fuction andto make the result as desired there are three ways to modify the genetic algorithm.
Genetic Algorithm is based on evolution theory by charles darwin. Like evolution this algorithm also pick some random solution from given samples (called parent) and then try to crossover the parents and mutate to get the better offspring or you can say that to get desired qualities in the offspring.We continue this process of crossover and mutation until we get our desired result.By this algorithm the optimization is quicker than the classical way.
OBJECTIVE
To show the three studies of genetic algorithm and find the maxima of stalagmite function.
BODY
Stalagmite function code(get_stalagmite.m)
This function can create stalagmite function and I change the sign of solution of stalagmite unction so that we get graph opposite to stalagmite surface so that we can find out maxima using genetic algorithm.This function is created for just showing surface of stalagmite function.
function [z]= get_stalagmite(a,b)
[x y]=meshgrid(a,b);
z1=(sin(5.1*pi.*x)+0.5).^6;
z2=(sin(5.1*pi.*y)+0.5).^6;
z3=exp(-4*log(2).*((x-0.0667).^2)./0.64);
z4=exp(-4*log(2).*((y-0.0667).^2)./0.64);
z=-(z1.*z2.*z3.*z4);
end
Stalagmite function code(stalagmite.m)
This function is created for genetic algorithm.
function [z]= stalagmite(value)
x=value(1);
y=value(2);
z1=(sin(5.1*pi*x)+0.5)^6;
z2=(sin(5.1*pi*y)+0.5)^6;
z3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
z4=exp(-4*log(2)*((y-0.0667)^2)/0.64);
z=-(z1*z2*z3*z4);
end
Main Code
clc;
clear all;
close all;
% creating stalagmite function
x=linspace(0,1,150);
y=linspace(0,1,150);
l=get_stalagmite(x,y);
%normal behaviour/statistical behaviour
tic
for i=1:50
[z f(i)]=ga(@stalagmite,2,[],[],[],[]);
xx(i)=z(1);
yy(i)=z(2);
end
%plotting
subplot(2,1,1)
hold on
surf(x,y,l)
shading interp
plot3(xx,yy,f,'marker','o','color','r')
xlabel('X axis');
ylabel('Y axis');
zlabel('values of f')
subplot(2,1,2)
plot(f)
xlabel('iteration');
ylabel('f values')
time=toc
tic
%ga with upper and lower bound
for i=1:50
[z f(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xx(i)=z(1);
yy(i)=z(2);
end
%plotting
subplot(2,1,1)
hold on
surf(x,y,l)
shading interp
plot3(xx,yy,f,'marker','o','color','r')
xlabel('X axis');
ylabel('Y axis');
zlabel('values of f')
subplot(2,1,2)
plot(f)
xlabel('iteration');
ylabel('f values')
time=toc
%ga with increased population
tic
options=optimoptions(@ga,'populationsize',150);
for i=1:50
[z f(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],options);
xx(i)=z(1);
yy(i)=z(2);
end
%plotting
subplot(2,1,1)
hold on
surf(x,y,l)
shading interp
plot3(xx,yy,f,'marker','o','color','r')
xlabel('X axis');
ylabel('Y axis');
zlabel('values of f')
subplot(2,1,2)
plot(f)
xlabel('iteration');
ylabel('f values')
time=toc
Explanation
The code is divide into three parts showing three different method to reach maximum value of stalagmite function.
The first part called normal behaviour contain no constrain. i.e I run the ga for 50 times and by default its population is 50.
The output of first run is given below
As you can see that, the stalagmite values(f) is keep changing during iteration. We need more convergence to get our desired value(minimum value).
The second part called behaviour under bounds. In this the ga perform under given boundary condition so that we can get to our result quickly.
Syntax-[x fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
The output of second run
As you can see that the values of f doesnot change over long time except few iteration. In this way we can find out minimum value but still the range of minimum values are quite high so this can give us local maxima.
Therefore, here we have third run in which we increas the population size of ga to 150 so that the range of maximum values decreases.
Syntax-ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],options)
The output of this run is
In this run we can easily see that the range gets decreses of maximum values i.e our accuracy increases but it also takes lots of times too.
CONCLUSION
Genetic Algorithm is good method to optimize the function. It can be widely used searching operations and other data sciences branches where lots of data are there.