All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To calculate the global maxima of a stalagmite function in MATLAB OBJECTIVE: To write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function. To plot graphs for 3 different studies and for F maximum vs no. of iterations. THEORY: Genetic Algorithm…
Damodhar Jangam
updated on 04 Dec 2020
AIM: To calculate the global maxima of a stalagmite function in MATLAB
OBJECTIVE:
THEORY: Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems that otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.
Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation. GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.
In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”. In this way, we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.
Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.
Advantages of GAs: GAs have various advantages that have made them immensely popular. These include −
Limitations of GAs: Like any technique, GAs also suffer from a few limitations. These include −
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
GOVERNING EQUATIONS :
finds a local unconstrained minimum, x
= ga(fun
,nvars
)x
, to the objective function, fun
. nvars
is the dimension (number of design variables) of fun
.
finds a local minimum x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
)x
to fun
, subject to the linear equalities Aeq*x
= beq
and A*x
≤ b
. (Set A=[]
and b=[]
if no linear inequalities exist.) ga
evaluates the matrix product Aeq*x
as if x
is transposed (Aeq*x'
).
defines a set of lower and upper bounds on the design variables, x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
)x
, so that a solution is found in the range lb
≤ x
≤ ub
. (Set Aeq=[]
and beq=[]
if no linear equalities exist.)
minimizes with the default optimization parameters replaced by values in x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
,nonlcon
,options
)options
. (Set nonlcon=[]
if no nonlinear constraints exist.) Create options
using optimoptions
.
MATLAB CODE :
Main code
clearvars
close all
clc
%% Defining global maxima
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creaating a 2D mesh
[xx, yy] = meshgrid(x,y);
%% 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);
input_vector;
f(i,j) = stalagmite(input_vector);
end
end
%% Study1-Statistical behaviour with unbounded input values
num_cases = 50;
for a=1:num_cases
[inputs,fopt_1(a)]=ga(@stalagmite,2);
xopt(a)=inputs(1);
yopt(a)=inputs(2);
end
study1_time=toc
%% plotting our data
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt_1,'marker','o','markerfacecolor','r','markersize',5)
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt_1)
xlabel('Iterations')
ylabel('Function maximum')
saveas(figure(1),'global maxima Study 1.jpg')
%% Study2-Statistical behaviour with upper and lower bounded input values
for b=1:num_cases
[inputs,fopt_2(b)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(b)=inputs(1);
yopt(b)=inputs(2);
end
study2_time = toc
% plotting our data
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt_2,'marker','o','markerfacecolor','r','markersize',5)
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt_2)
xlabel('Iterations')
ylabel('Function maximum')
saveas(figure(2),'global maxima Study 2.jpg')
%% Study3-Statistical behaviour with upper and lower bounded input values and increasing genetic algorithm iterations
option_1 = optimoptions('ga');
option_1 = optimoptions(option_1,'Populationsize',600);
tic
for c=1:num_cases
[inputs,fopt_3(c)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],option_1);
xopt(c)=inputs(1);
yopt(c)=inputs(2);
end
study3_time = toc
%% plotting our data
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt_3,'marker','o','markerfacecolor','r','markersize',5)
title('Bounded inputs with increasing population size')
subplot(2,1,2)
plot(-fopt_3)
xlabel('Iterations')
ylabel('Function maximum')
saveas(figure(3),'global maxima Study 3.jpg')
stalagmite function code
function [f] = stalagmite(input_vector)
% Inputs
p = input_vector(1);
q = input_vector(2);
f1x = (sin(5.1*pi*p + 0.5))^6;
f1y = (sin(5.1*pi*q + 0.5))^6;
f2x = exp(-4*log(2)*(((p-.0667)^2)/.64));
f2y = exp(-4*log(2)*(((q-.0667)^2)/.64));
f = -f1x*f1y*f2x*f2y;
end
Result:
1) Unbounded inputs study
2) Bounded input study
3) Bounded inputs with increased population size
CONCLUSION :
The genetic algorithm repeatedly modifies a population of individual solutions. At each step, 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.
But if the population size is taken too large, the processing time taken by MATLAB also increases.
Reference :
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...
Final Project: Electric Rickshaw modelling
Aim of Project: Creating a MATLAB model of an electric rickshaw (three-wheel vehicle) with three driving cycles and variation temperature and driver rickshaw for 100Km constant speed driving at 45kmph. THEORY: Electric rickshaws (also known as electric tuk-tuks or e-rickshaws or toto or e-tricycles) have…
30 Jul 2021 06:33 AM IST
MBD Simulation on IC Engine Valve Train
Aim: Modelling and analysis of IC Engine Valvetrain for detecting the valve displacement and contact forces between contacts. Objectives: Obtain valve lift for given CAM lift of 3.5 mm & 6 mm at the same speed (1500 RPM) Obtain Plots between contact force of Cam & Push rod, Push rod…
18 Jul 2021 03:16 PM IST
MBD Simulation on a Piston Assembly
Aim: Modelling and assembly of Piston Assembly as well as Calculating Linear displacement of the Piston Head in all three cases of gudgeon offset with help of motion analysis and compare results. Introduction: Piston assembly consists of a piston, crank, connecting rod and piston pin known as a gudgeon pin. Functions…
18 Jul 2021 06:12 AM IST
Planetary Gear
Aim: Modelling Planetary Gear mechanism and calculating angular velocity with fixing by fixing the sun gear, ring gear and Carrier separately with help of motion analysis Introduction: A planetary gear set (also known as an epicyclic gear train ) consists of two gears mounted so that the centre…
17 Jul 2021 02:48 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.