Week 4 - Genetic Algorithm
GENETIC ALGORITHUM USING STALAGMITE FUNCTION IN MATLAB
AIM : To perform genetic algorithum of a given Stalagmite function using Matlab.
OBJECTIVE :
- To understand the concept of genetic algorithm.
- To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- To plot graphs for all 3 studies and for F maximum vs no. of iterations.
INTRODUCTION :
GENETIC ALGORITHUM :
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
STALAGMITE FUNCTION USED IN OUR STUDY :
'GA', is basically used for calculating the global minima values and to get the global maxima value we are using a negative sign '-' above our function. i.e -f .
MATLAB CODE FOR THE SAME :
%matlab code to calculate stalagmite function
%Defining the search space of GA
x = linspace(0,.6,150);
y = linspace(0,0.6,150);
%Number of iterations
num_cases = 50;
%calculating Stagalmite function
[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) = get_stalagmite(input_vector);
end
end
%Study 1 - statistical behaviour with unbounded points
tic
for i = 1: num_cases
[inputs,fopt(i)] = ga(@get_stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time1 = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical Behaviour with unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
%Study 2 - statistical behaviour with upper and lower bound points
tic
for k = 1: num_cases
[inputs2,fopt2(k)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt2(k) = inputs2(1);
yopt2(k) = inputs2(2);
end
study_time2 = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour with upper and lower bound')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('Function maximum')
%Study 3 - statistical behaviour by increasing the size of GA
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
for m = 1: num_cases
[inputs3,fopt3(m)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3(m) = inputs3(1);
yopt3(m) = inputs3(2);
end
study_time3 = toc;
% For plotting
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markerface',5,'markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour with increased iterations')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iterations')
ylabel('Function maximum')
Program for calling the Stalagmite Function :
%Program to get stalagmite function
function [f] = get_stalagmite(input_vector)
%Inputs
x = input_vector(1);
y = input_vector(2);
% Solution of the function f(x,y) = (f_1,x)*(f_2,x)*(f_1,y)*(f_1,y)
%sf1 = f1,x
%sf2 = f1,x
%sf3 = f2,x
%sf4 = f2,y
sf1 = (sin((5.1*pi*x) + 0.5))^6;
sf2 = (sin((5.1*pi*y) + 0.5))^6;
sf3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
sf4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f = -(sf1*sf2*sf3*sf4);
end
EXPLAINATION OF THE CODE :
The search space of ga is defined using the linspace command. Here we have taken 150 values of x and y array between 0 and 0.6.
num_cases is used to determine the number of iterations in our study. For the first two studies we used 50 iterations and for the final study to get precise value we used 100 iterations.
meshgrid command creates 2D arrays for x and y .
For loop is used to give values to the get_stalagmite function for the 50 values of the iterations.
Then we wrote different syntax for different studies as mentioned.
tik and toc command is used for calculating the time for different studies.
figure command is used to open a figure window . subplot command is used for plottiong multiple command in a single plot.
title command is used to give title to the plot and xlabel and ylabel to label the axis.
In all the 3 case studies the code changes a bit as required for a given output. For the first two studies the code is bit similar , but for the last study to get more precise value of the global maxima we increased the number of iterations and also the population size, which is as shown in the code.
RESULTS AND PLOTS :
Case 1 : Statistical Behaviour with unbounded inputs.
Case 2 : Statistical behaviour with upper and lower bound
Case 3 : Statistical behaviour with increased number of iterations with GA
STUDY TIME FOR DIFFERENT STATISTICAL DATA :
Study_time1 =4.0079sec
Study_time2 =6.4234sec
Study_time3 = 65.6310sec
CONCLUSION :
Thus we can conclude that the ga syntax for all the 3 studies are different and also the study time differs(specially in 3rd case)
Study_time1 =4.0079sec Study_time2 =6.4234sec Study_time3 = 65.6310sec
We can see that as the number of iterations and Population size increases the precision in the value of global maxima increases and also for this it takes a bit long time. This is the reason for which the 3rd study took a comparatively more time than the rest two.
The maximum value obtained is 0.9959.