All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: Write a code to optimize the stalagmite function and find the global maxima of the function. Plot the graphs for different studies to understand the behavior of the genetic algorithm and for Fmax vs no. of iterations. OBJECTIVES: understand concepts of genetic algorithm write a code to optimize stalagmite function…
Bharath Gaddameedi
updated on 02 May 2022
AIM: Write a code to optimize the stalagmite function and find the global maxima of the function. Plot the graphs for different studies to understand the behavior of the genetic algorithm and for Fmax vs no. of iterations.
OBJECTIVES:
Stalagmite function: it is based on a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings.
source: https://en.wikipedia.org/wiki/Stalagmite
the mathematical function of a stalagmite function is f(x,y)=f1,x⋅f2,x⋅f1,y⋅f2,y
f1,x=[sin(5.1⋅π⋅x+0.5)]6
f1,y=[sin(5.1⋅π⋅y+0.5)]6
f2,x=exp(−4ln(2)⋅((x−0.0667)20.64))
f2,y=exp(−4ln(2)⋅((y−0.0667)20.64))
function for stalagmite in MATLAB
function f = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = [sin(5.1*pi*x + 0.5)]^6;
f1y = [sin(5.1*pi*y + 0.5)]^6;
f2x = exp(-4*log(2)*((x-0.0667)^2)/0.64);
f2y = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f = -f1x*f1y*f2x*f2y;
end
in the function definition the negative sign is added to invert the stalagmite function
The first figure is without a negative sign and the second one is with a negative sign hence it is inverted.
Optimization
Engineers are often encountered with problems to identify a few appropriate design solutions and then deciding which one best meets the need of the client. This decision-making process is known as optimization.
engineers, most of the time use optimization to get the best design parameters rather than using a brute force approach.
Genetic algorithm
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals 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.
there are 3 key principles that need to be in place for evolution to happen
Heredity: there must be a process in place by which children receive the properties of their parents.
Variation: there must be a variety of traits present in the population or a means with which to introduce variation.
Selection: there must be a mechanism by which some members of a population have the opportunity to be parents and pass down their genetic information and some do not. This is referred to as "survival of the fittest".
The basic layout of the genetic algorithm
Setup
Step 1: Initialize: Create a population of N elements, each with randomly generated DNA.
DRAW
Step 2: Selection: Evaluate the fitness of each element of the population and build a mating pool.
Step 3: Reproduction: Repeat N times
Step 4: replace the old population with the new population and return to step 2.
Function for accessing the stalagmite function
function [f] = get_stalagmite(x,y)
%creating a 2 dimensional mesh
[xx,yy] = meshgrid(x,y);
f = zeros(1,1); %for preallocation
%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
end
Main code for Optimization
clear
close all
clc
%input arrays
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
f = get_stalagmite(x,y);
num_cases = 50;
xopt = zeros(1,10);
yopt = zeros(1,10);
fopt = zeros(1,10);
%study 1-statistical behaviour
tic
for i = 1:num_cases
[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(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
%study 2-statistical behaviour with upper and lower bound
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
%study 3-increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options, 'PopulationSize',150);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
Explanation:
x
= ga(fun
,nvars
)
find a local unconstrained minimum, x
, to the objective function, fun
. nvars
is the dimension (number of design variables) of fun
.x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
)
defines a set of lower and upper bounds on the design variables, x
, so that a solution is found in the range lb
≤ x
≤ ub
. (Set Aeq=[]
and beq=[]
if no linear equalities exist.)x
= ga(fun
,nvars
,A
,b
,[],[],lb
,ub
,nonlcon
,IntCon
)
or x
= ga(fun
,nvars
,A
,b
,[],[],lb
,ub
,nonlcon
,IntCon
,options
)
requires that the variables listed in IntCon
take integer values.Study 1
GA is based on random sampling and doesn't know the defined search space is the result space. So the values obtained from the ga is outside the search space as we can see from the above graph. The function value is not constant and fluctuates a lot.
Study 2
In the second study, the search space is restricted by using the bounds in the syntax we can see that the results are within the range. Still, the optimum value is not obtained with accuracy. The range of f value is narrowed.
Study 3
In study 3 we have increased the ga iterations. And used the options command in the syntax and defined the population size.
We have found the optimum values by giving more constraints and the f value is within the precision of 10^-7.
CONCLUSION
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 1- Mixing Tee
…
08 Jul 2023 06:07 PM IST
Week 2 Challenge : Surface meshing on a Pressure valve
Surface meshing on a Pressure valve Aim: Check the geometrical errors on the pressure valve and perform topology cleanup. Meshing it for three target lengths. Objectives: Mesh it for target lengths 1mm, 3mm, and 5mm.…
10 Apr 2023 03:12 AM IST
Week 7 - Simulation of a 1D Super-sonic nozzle flow simulation using Macormack Method
Simulation of a 1D Super-sonic nozzle flow simulation using Macormack Method Aim: To simulate the isoentropic flow through a quasi 1D subsonic-supersonic nozzle using conservation and non-conservation forms of governing…
19 Dec 2022 08:52 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.