Genetic Algorithm to find the Global Maxima of a Stalagmite function using Matlab
Optimisation of Stalagmite function to find the global maxima using Genetic Algorithm
Aim & Objectives:
- To understand the concept of Genetic Algorithm (GA)
- To write MATLAB code to optimise the stalagmite function and find the global maxima of the function.
- To investigate on the different statistical behaviour.
- To explain the concept of the genetic algorithm and the syntax for ga in MATLAB.
Stalagmite Function for which the Global Maxima needs to be found:
Genetic Algorithm (GA):
Genetic algorithms are commonly used to generate high-quality solutions to optimisation and search problems by relying on biologically inspired operators such as mutation, crossover and selection. In a genetic algorithm, a population of candidate solutions to an optimization problem is evolved toward better solutions.
GA In Matlab:
In Matlab, 'ga' function calculates the minima for any given function.
Syntax:
x = ga(fun,finds a local unconstrained minimum,nvars)x, to the objective function,fun.nvarsis the dimension (number of design variables) offun.-
finds a local minimumx= ga(fun,nvars,A,b)xtofun, subject to the linear inequalitiesA*x≤b.gaevaluates the matrix productA*xas ifxis transposed (A*x'). -
finds a local minimumx= ga(fun,nvars,A,b,Aeq,beq)xtofun, subject to the linear equalitiesAeq*x=beqandA*x≤b. (SetA=[]andb=[]if no linear inequalities exist.)gaevaluates the matrix productAeq*xas ifxis transposed (Aeq*x'). -
defines a set of lower and upper bounds on the design variables, so that a solution is found in the rangex= ga(fun,nvars,A,b,Aeq,beq,lb,ub)lb≤x≤ub. (SetAeq=[]andbeq=[]if no linear equalities exist.) -
subjects the minimization to the constraints defined inx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)nonlcon. The functionnonlconacceptsxand returns vectorsCandCeq, representing the nonlinear inequalities and equalities respectively.gaminimizes thefunsuch thatC(x)≤0andCeq(x) = 0. (Setlb=[]andub=[]if no bounds exist.) -
minimizes with the default optimization parameters replaced by values inx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)options. (Setnonlcon=[]if no nonlinear constraints exist.) Createoptionsusingoptimoptions. -
orx= ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)requires that the variables listed inx= ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)IntContake integer values. -
finds the minimum forx= ga(problem)problem, whereproblemis a structure. [, for any previous input arguments, also returnsx,fval] = ga(___)fval, the value of the fitness function atx.[also returnsx,fval,exitflag,output] = ga(___)exitflag, an integer identifying the reason the algorithm terminated, andoutput, a structure that contains output from each generation and other information about the performance of the algorithm.-
[also returns a matrixx,fval,exitflag,output,population,scores] = ga(___)population, whose rows are the final population, and a vectorscores, the scores of the final population.
Methodology:
The optimisation of the Stalagmite function and comparison of its statistical behaviour is done in three ways:
- Without upper and lower limits - Unbounded
- With upper and lower limits - Bounded
- By increasing the number of iteration by increasing population size.
Matlab Script:
clear all
close all
clc
% Input Arrays
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% 2D Mesh
[xx,yy] = meshgrid(x,y);
iterations = 50;
% 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
figure(1)
hold on
surfc(xx,yy,-f)
shading interp
[inputs,f_val] = ga(@Stalagmite,2);
% Study 1 Statistical behaviour - Unbounded
for i = 1:iterations
[inputs,f_max(i)] = ga(@Stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
colorbar
plot3(xopt,yopt,-f_max,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical Behaviour - Unbounded inputs')
subplot(2,1,2)
plot(-f_max)
xlabel('Iterations')
ylabel('Function Maxima')
% Study 2 Statistical behaviour - Bounded
for i = 1:iterations
[inputs,f_max(i)] = ga(@Stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
colorbar
plot3(xopt,yopt,-f_max,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical Behaviour - With upper and lower limit (Bounded)')
subplot(2,1,2)
plot(-f_max)
xlabel('Iterations')
ylabel('Function Maxima')
% Study 3 Statistical behaviour - With Increased Population Size
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',250);
for i = 1:iterations
[inputs,f_max(i)] = ga(@Stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
colorbar
plot3(xopt,yopt,-f_max,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical Behaviour - With Increased Population Size')
subplot(2,1,2)
plot(-f_max)
xlabel('Iterations')
ylabel('Function Maxima')
% Stalagmite Function
function [S] = Stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = (sin(5.1*pi*x+0.5))^6;
f1_y = (sin(5.1*pi*y+0.5))^6;
f2_x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f2_y = exp(-4*log(2)*((y-0.0667)^2/0.64));
S = -(f1_x*f2_x*f1_y*f2_y);
end
Explanation of the code:
1. A function named Stalagmite is created
2. Returning function is given a negative sign which helps in finding the global maxima since 'ga' normally finds minima of a function.
3. Input arrays were initialised which defines the search space.
4. Created a 2D Array with the input array values.
5. No. of iteration was set.
6. The stalagmite function is evaluated and checked whether providing the maxima value for a particular set of inputs.
Study-1:
6. Using the syntax x = ga(fun,as [inputs,f_max(i)] = ga(@Stalagmite,2), statistical behaviour of the function without defining upper and lower limit is studied.nvars)
Study-2:
7. Using the syntax as [inputs,f_max(i)] = ga(@Stalagmite,2,[],[],[],[],[0;0],[1;1]), statistical behaviour of the function by defining upper and lower limit is studied.x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
Study-3:
8.Using the syntax as [inputs,f_max(i)] = ga(@Stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options), statistical behaviour of the function by modifying or increasing the population size is studied.x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
9. New population size option is defined using the syntax, options = optimoptions(SolverName) and options = optimoptions(prob,Name,Value) as options = optimoptions('ga') and options = optimoptions(options,'PopulationSize',250) respectively.
10. For all the three studies, surface for the function is created and global maxima values were marked by creating a 3D plot.
11. One more graph is created for '-f_max', which is actually the global maxima value for each set of input plotted against iterations (50).
Results:
Stalagmite function evaluated with ga without limits (unbounded):
Study-1:
Statistical Behaviour-Without limits (Unbounded)
Study 2 :
Statistical Behaviour - Bounded ( With upper and lower limits)
Study 3:
Statistical Behaviour - After increasing the population size
Conclusion:
- In Study-3, the global maxima is close to 1 for almost every iteration except for one where the global maxima value is shown as 0.84 approx.
- Study 3 gives better results when compared to study 1 and study 2.