All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM : To study about Genetic Algorithm and to calculate the global maxima of stalagmite function using Genetic Algorithm in MATLAB. OBJECTIVE : To study about Genetic Algorithm and Stalagmite function. To write a code in Matlab to optimise the stalagmite function and find the global maxima of the function. To plot graphs…
Dhanu Manthri
updated on 08 Aug 2022
AIM : To study about Genetic Algorithm and to calculate the global maxima of stalagmite function using Genetic Algorithm in MATLAB.
OBJECTIVE :
THEORY :
Concept of Genetic Algorithm –
The Genetic Algorithm works on the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation. It is a method for solving both constrained and unconstrained optimization problems that is based on natural selection. The algorithm begins by creating a random initial population of individual selections and it keeps on modifying this population repeatedly creating a sequence of new population. The algorithm uses the individuals in the current generation to create the next population. The genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population evolves toward an optimal solution.
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
The basic steps in creating a genetic algorithm is as follows,
The flowchart for Genetic Algorithm is given below,
What is a Stalagmite Function?
Stalagmite function is a mathematical model which helps to generate local minima and maxima. It is a three dimensional function and the way the function is designed is that it has a lot of local maxima or minima and one global maxima or minima. The name stalagmite is actually a type of rock formation that arises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings.
The stalagmite function is given as,
Syntax for defining ‘ga’ (Genetic Algorithm) in matlab –
The ‘ga’ syntax in matlab is used to find the minimum of function using genetic algorithm. There are several ways to define the ‘ga’ function depending on the problem being solved. The syntax used for this study is shown below.
where,
x = optimal solution/variables;
fun = Objective function;
nvars = Number of variables;
Sometimes ‘[x,fval]’ is used instead of ‘x’ where ‘fval’ gives objective function value at the solution
where,
A, B = Linear inequality constraints
Aeq, Beq = Linear equality constraints
If there are no linear equalities and inequalities, then A, B, Aeq, Beq are set to ‘[ ]’.
where,
nonlcon = Nonlinear constraints
IntCon = Integer Values
options = Optimization options
If there are no nonlinear constraints and integer values, the nonlcon and IntCon are set to ‘[ ]’.
MATLAB Program :
First in order to define stalagmite function, we need to write a different script defining the stalagmite function.
%Define Stalagmite Function
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
t1 = (sin((5.1*pi*x)+0.5))^6;
t2 = (sin((5.1*pi*y)+0.5))^6;
t3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
t4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f = -(t1*t2*t3*t4);
end
The negative sign in the function will invert the stalagmite plot which will eventually give the function maxima.
Now we can proceed for coding the Genetic Algorithm for this stalagmite function.
clear all
close all
clc
%Definie our search space
x = linspace(0,0.6,150); %x array
y = linspace(0,0.6,150); %y array
n = 50; %number of cases
%Creating a 2D mesh
[xx yy] = meshgrid(x,y);
%Evaluating a 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 : Statistical Behaviour
tic
for i = 1:n
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical Behaviour with Unbound inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')
%Study2 : Statistical behaviour with upper and lower bounds
tic
for i = 1:n
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical behaviour with upper and lower bounds')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')
%Study3 : Statistical Behaviour with increasing iterations of GA
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);
tic
for i = 1:n
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical Behaviour with increasing iterations')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')
Here in this code,
RESULTS :
Study 1 – Statistical Behaviour.
In above plot, it can be observed how the random sampling of the genetic algorithm generates a new optimum value for the function every time the code is run. Although this falls in line with the definition of Genetic Algorithm. Large number of red dots shows values of co-ordinates of x, y varies each time iteration runs.
Study 2 – Statistical Behaviour with upper and lower bounds.
The above results show the convergence of x and y values. The number of red dots are less compared to previous plot. This is the result of providing upper and lower bounds to the function.
Study 3 – Statistical Behaviour with increasing iterations of GA.
In this study, the number of iterations is increased to 300 to converge and find the global maxima of the function. Here we can see that there is only one red dot on the stalagmite function and that is the required global maxima point.
On rotating the plot, we can clearly see the stalagmite function along with the global maxima value as x = 0.06683 and y = 0.06683.
Also the graph of ‘Function maximum vs No. of Iterations’ results that the value of optimum function ‘fopt’ is approximately equal to 1.
Workspace Values –
ERRORS :
*No major errors were faced while coding.
REFERENCES :
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week 14 challenge
AIM : To assemble the Butterfly Valve by using the sub parts created and apply the GD&T to the Butterfly Valve assembly drawing.Introduction:Butterfly Valve:A butterfly valve is a valve that isolates or regulates the flow of a fluid. The closing mechanism is a disk that rotates. A butterfly valve is from…
08 Nov 2024 01:47 PM IST
Week 12- Final project
DEVELOPEMET OF DOOR TRIM PANEL AIM: To develop a door trim panel following the design…
28 Mar 2024 05:54 AM IST
Week 11 - Project - A pillar Design with Master Section
A PILLAR DESIGN WITH MASTER SECTION AIM: To create the A Pillar Design with the master section given as the…
14 Mar 2024 06:16 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.