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…
Dipin K D
updated on 22 Nov 2022
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 based on the studies done for Genetic Algorithm.
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.
MATLAB Program :
First in order to define stalagmite function, we need to write a different script defining the 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
Now we can proceed for coding the Genetic Algorithm for this stalagmite function.
clear all
close all
clc
%define our search space
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);
n = 50;
%creating a 2D mesh
[xx yy] = meshgrid(x,y);
%evaluating a stalagmite
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 Unbond 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 bonds
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 bonds')
plot3(xopt,yopt,-fopt,'marker','o','markersize','5','markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Interations')
ylabel('Function maximum')
%study3 :Statistical Behaviour with increasing iterations of GA
option = optimoptions('ga');
option = optimoptions(option,'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 iteration')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')
1. Then the ‘x’ array and ‘y’ array is defined using ‘linspace’ command to create values at 150 equal intervals.
2. A 2D mesh of x and y array is created by using ‘meshgrid’ command and the number of iterations or cases that the code should perform is defined. This is given by n = 50.
3. To evaluate the stalagmite function, ‘for’ loop is used where ‘i’ is initialized and for every value of ‘i’ another ‘for’ loop is used to initialize ‘j’.
4. The input vector values of ‘xx’ and ‘yy’ are defined and the stalagmite function for these input vector is defined. This will form a basic stalagmite function and we can plot it separately if needed.
5. After preparing the inputs needed for the function, next step is to obtain the results for 3 cases of the genetic algorithm. In first case or study, the maximum value of function is found out with no restrictions, no bounds and no limits.
6. This is done using ‘for’ loop where we use ‘ga’ syntax to give values for inputs and optimum max value. The stalagmite function is called in ‘ga’ syntax and number of variables ‘nvars’ are 2.
7. to know the time that is taken to solve the loop for 50 iterations, ‘tic’ and ‘toc’ command is used before and after the loop respectively.
8. Then the function created in the loop is plotted by using ‘surfc’ command for ‘xx, yy and function output’. To neglect the gridlines on the surface and obtain a smooth and clean plot, ‘shading interp’ command is used. The ‘subplot(2,1,1)’ is used to plot multiple plots on same figure which also divides figure in grid of 2x1 and axes to be placed at 1.
9. The ‘plot3’ command plots the 3D points at ‘xopt, yopt and optimum value of function’ with ‘o’ shaped and red ind this procedure is same as Study 1. The only difference is in the ‘ga’ syntax. This time the algorithm is provided lower limits and upper limits as [0;0] and [1;1] respectively.
After plotting the results for Study 2 or the bounded case, it is observed that the results are not same every time the program terminates. A solution to this problem is to increase the number of cases or iterations.
So for Study 3, the number of iterations are increased to 300. So for this study, the syntax, “options = optimoptions(‘SolverName’,’Name’,‘Value’)” is used which returns with named parameter ‘Populations’ with the altered values ‘300’.
10. The syntax for ‘ga’ contains ‘option’ term and the rest of the code is same as for Study 1 and 2.
RESULTS :Study 1 – Statistical Behaviour.
Study 2 – Statistical Behaviour with upper and lower bounds
Workspace values -
[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
Workspace values
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...
Project 2 - Rankine cycle Simulator
Aim: To calculate the performance of a Steam turbine Engine and plot the T-S and H-S diagram of a Rankine Cycle. Objective: To calculate the performance of a Steam turbine Engine.To plot the T-S and H-S diagram of a Rankine Cycle. Theory: Rankine Cycle: A rankine cycle is an idealised Thermodynamic cycle which governs…
23 Nov 2022 08:08 AM IST
Week 5 - Genetic Algorithm
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…
22 Nov 2022 07:46 PM IST
Week 4.1 - Solving second order ODEs
AIM: To solve an ODE and use the calculated information to animate the motion of the pendulum via matlab THEORY Pendulum have many applications and were utilized often before the digital age. They are used in clocks and metronomes due to regularity of their period. A…
12 Nov 2022 03:45 AM IST
Week 3.2 - 2R Robotic Arm Challenge
Aim To simulate the forward kinematics of a 2R Robotic Arm in matlab Introductiion Robots are progammable machine like some human capabilities.They are used in many industeries. These elemens can be arranged in different ways and can vary in different size. Thus robots are available in a wide variety of types in their…
02 Nov 2022 06:56 PM 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.