All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the…
Harsh Sharma
updated on 07 Nov 2022
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.
Classical Algorithm |
Genetic Algorithm |
Generates a single point at each iteration. The sequence of points approaches an optimal solution. |
Generates a population of points at each iteration. The best point in the population approaches an optimal solution. |
Selects the next point in the sequence by a deterministic computation. |
Selects the next population by computation which uses random number generators. |
The genetic algorithm uses three main type of rules to create the next population from the present selected populations.
The basic steps in creating a genetic algorithm is as follows.
STALAGMITE FUNCTION: Stalagmite function is a mathematical model which is used to find out the local minima and maxima. It is three-dimensional function. It has a lot of local minima or maxima, and one global maxima or minima. The name stalagmite is actually a type of rock formation generally in the lime caves. Which are formed due to the accumulation of material deposits on the floor from the ceiling drippings.
Syntax for defining GA(Genetic Algorithm).
The ‘ga’ syntax in matlab used to find the minimum function using genetic algorithm. There are several syntax of ‘ga’. We can change the syntax based upon the problem.
1 ‘ga’ Syntax.
X = ga(fun,nvars)
Where
X = Variables
.fun = Function file name.
.nvars = Numbers of variables.
2 ‘ga’ Syntax.
X = ga(fun,nvars,A,B,Aeq,Beq,lb,bu)
Where
A,B = Linear inequality constraints.
Aeq,Beq = Linear equality constraints.
Lb,ub = Lower & Upper bound.
If there are no linear inequality and equality constraints, then A,B,Aeq,Beq, are set to be ‘[ ]’.
3 ‘ga’ Syntax.
X = ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,IntCon,options)
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 ‘[ ]’.
function [f] = stalgamite(input_vector)
x = input_vector(1)
y = input_vector(2)
f1 = (sin((5.1*pi*x)+0.5))^6;
f2 = (sin((5.1*pi*y)+0.5))^6;
f3 = exp(-4*log(2)*(((x-0.0667)^2)/0.64));
f4 = exp(-4*log(2)*(((y-0.0667)^2)/0.64));
f = -(f1*f2*f3*f4); % Negative sign is given to the final out put, because we need the
% inverted stalagmite plot which gives the function maxima.
end
Now we can start the ‘ga’ Main code.
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50;
[xx,yy] = meshgrid(x,y);
%f = stalgamite(x,y);
for i = 1:length(x);
for j = 1:length(y);
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalgamite(input_vector);
end
end
figure(12)
surfc(xx,yy,-f);
shading interp
[inputs, fval] = ga(@stalgamite, 2)
% Study 1 : Statistical Behaviour.
tic
for i = 1:n;
[inputs,fopt(i)] = ga(@stalgamite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('x- axis')
ylabel('y-axis')
title('Statistical Behaviour with Unbound inputs')
plot3(xopt,yopt,-fopt,'Marker','o','MarkerFaceColor','b','MarkerSize',5);
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iteration')
ylabel('Function Maximum')
% Study 2 - Statistical Behaviour with upper & lower bound
tic
for i = 1:n;
[inputs, fopt(i)] = ga(@stalgamite,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
surf(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical Behaviour with upper & lower bounds')
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'markerfacecolor','b')
subplot(2,1,2)
plot(-fopt)
xlabel('No. Of iteration')
ylabel('Function Maximum')
% Study 3 - Statistical Behaviour with Increasing Iteration Of GA
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',300);
tic
for i = 1:n
[inputs, fopt(i)] = ga(@stalgamite,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
surf(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','b')
subplot(2,1,2)
plot(-fopt)
xlabel('No. Of iteration')
ylabel('Function Maximum')
Code Summery.
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'markerfacecolor','b')
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',300);.
Study no 1: Statistical Behaviour with unbound limits.
In the above plot, it can be observed how the random sampling of the genetic algorithm generates the new optimum values for the function every time the code run. Large no of blue dots shows values of co-ordinates of x, y. varies each time iteration runs.
Study no 2: Statistical Behaviour with Upper and lower bound.
In above results, the numbers of blue dots is less as compared to the previous one. This is the result of providing the upper and lower bound to the function.
Study no 3: Statistical Behaviour with increasing iterations.
In this study, the numbers of the iterations is increased to the 300 to converge and find out the global maxima of the function. Here we can see only one blue dot on the stalagmite function and that is the required global maxima point.
There are no major errors, i get while coading. Some errors i got, Which discription is given below;
01. one error came while coading. It was that, i had used the plot command in place of 'plot 3' command.
02. Second error is that, while programing the function command. I had used sind and cosd in place of sin or cos. That's why i am getting a plane plot. Not a stalagmite plot.
https://in.mathworks.com/discovery/genetic-algorithm.html
https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html
https://in.mathworks.com/help/gads/some-genetic-algorithm-terminology.html
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 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
Deriving 4th order approximation of a 2nd order derivative using Taylor Table Method. Taylor series formula is a representation of any function as an infinite sum of terms. These terms are calculated from the values of function’s derivatives at a single point. The derivative of a function f at the point x is defined…
03 Sep 2024 02:20 PM IST
Week 7 - CHT Analysis on a Graphics card
Challenge No-6 Graphics Card Simulation. Aim – Perform a steady state conjugate heat transfer analysis on a model of a graphics card. Challenge Objective – 01. Run the simulation by varying the velocity from 1m/sec to 5m/sec for at least 3 velocities and discuss the results. 02. …
18 Mar 2024 05:10 PM IST
Week 5 - Rayleigh Taylor Instability
Challenge no- 5 Rayleigh Taylor Instability Aim – The Aim is to understand the Rayleigh Taylor instability phenomena and how it takes place. Objectives – 1. What are some practical CFD models that have been based on the mathematical analysis of Rayleigh Taylor waves? In…
10 Mar 2024 11:39 AM IST
Week 4 - CHT Analysis on Exhaust port
Challenge – CHT Analysis on Exhaust Port. Objective 1 – Conjugate heat transfer refers to the combined analysis of both fluid flow and heat transfer in systems where there are solid structure interacting with the fluid flow, leading to heat transfer between the solid structure and the surrounding fluid. In…
29 Feb 2024 06:13 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.