All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
INTRODUCTION: Genetic algorithm is a heuristic method that is inspired by Darwin\'s theory of natural evolution. Genetic algorithm is commonly used to generate optimizations problems & search problems. To make it do this it requires genetic representation fitness function The genetic algorithm solves optimization…
Sai Sharan Thirunagari
updated on 07 Feb 2020
INTRODUCTION:
Genetic algorithm is a heuristic method that is inspired by Darwin\'s theory of natural evolution. Genetic algorithm is commonly used to generate optimizations problems & search problems. To make it do this it requires
The genetic algorithm solves optimization problems and search problems by relying on bio-inspired operators as
Genetic algorithm is a built-in function in Matlab but the algorithm works as follows
The syntax for the genetic algorithm in MATLAB is
x=ga(fun,nvars), finds 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), finds a local minimum x to fun, subject to the linear inequalities A⋅x≤B. ga evaluates the matrix product A*x as if x is transposed (A*x\').
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,Aeq,Beq,lb,ub,nonlcon,option), subjects the minimization to the constraints defined in nonlcon. minimizes with the default optimization parameters replaced by values in options.
[x,fval]=ga(_), for any previous input argument, also return fval, the value of fitness function at x (minimum value).
[x,fval,exitflag,output,population,scores]=ga(_), also returns exitflag , an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm. also outputs population, whose are the final population, and a vector scores, the scores of the final population.
Here Stalagmite function is used as the fitness function and genetic algorithm is used to optimize the stalagmite function.
EXPLANATION OF THE PROGRAM:
Defining the Stalagmite function.
The stalagmite function is first defined to optimize it by using a genetic algorithm.
Stalagmite function takes in two inputs x,y and gives one output f . It is represented as
f(x,y)=f1x⋅f2x⋅f1y⋅f2y
f1x=[sin(5.1⋅π⋅x+0.5)]6
f2x=e−4ln(2)⋅(x−0.667)20.64
f1y=[sin(5.1⋅π⋅y+0.5)]6
f2x=e−4ln(2)⋅(y−0.667)20.64
The genetic algorithm tries to find the minimum value of the function but we need to calculate the maximum value by optimizing the function for that I need to finally define the stalagmite function as
f(x,y)=−[f1x⋅f2x⋅f1y⋅f2y]
so that we get the maximum value by finding the minimum value calculated by genetic algorithm.
Use of genetic algorithm to optimize the stalagmite function.
This program is divided into three cases to understand how Genetic algorithm works and how it is used to optimize a function.
DEFINING STALAGMITE FUNCTION:
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
%formula of stalagmite function
f_1x = [sin(5.1pi*x+0.5)].^6;
f_2x = exp(((-4*log(2))*((x-0.0667).^2)/(0.64)));
f_1y = [sin(5.1*pi*y+0.5)].^6;
f_2y = exp(((-4*log(2))*((y-0.0667).^2)/(0.64)));
%for maximizing we need to add \'-\' to the resulting function
f = -(f_1x.*f_2x.*f_1y.*f_2y);
end
PROGRAM TO OPTIMIZE THE STALAGMITE FUNCTION USING GENETIC ALGORITHM
clear all
close all
clc
%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Creating 2D mesh
[xx,yy] = meshgrid(x,y);
%number of iterations
iterations = 50;
%Evaluating 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
%Optimizing stalagmite function using ga
%case 1
tic
for i=1:iterations
[inputs,fval(i)] = ga(@stalagmite,2);
x_out(i)=inputs(1);
y_out(i)=inputs(2);
end
case1_time = toc
%plotting surface of the stalagmite function
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_out,y_out,fval,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\')
xlabel(\'x-axis\')
ylabel(\'y-axis\')
title(\'stalagmite function optimized using ga\')
subplot(2,1,2)
plot(fval)
xlabel(\'iterations\')
ylabel(\'fval\')
%Optimizing stalagmite function using ga by giving lower and upper bounds
%case 2
tic
for i=1:iterations
[inputs,fval(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
x_out(i)=inputs(1);
y_out(i)=inputs(2);
end
case2_time = toc
%plotting surface of the stalagmite function
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_out,y_out,fval,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\')
xlabel(\'x-axis\')
ylabel(\'y-axis\')
title(\'stalagmite function optimized using ga with lower bounds and upper bounds\')
subplot(2,1,2)
plot(fval)
xlabel(\'iterations\')
ylabel(\'fval\')
%Optimizing stalagmite function using ga by giving lower and upper bounds
%and also by increasing the population
%case 3
options = gaoptimset(\'populationsize\',300);
tic
for i=1:iterations
[inputs,fval(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
x_out(i)=inputs(1);
y_out(i)=inputs(2);
end
case3_time = toc
%plotting surface of the stalagmite function
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_out,y_out,fval,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\')
xlabel(\'x-axis\')
ylabel(\'y-axis\')
title(\'stalagmite function optimized using ga by increasing population along with bounds\')
subplot(2,1,2)
plot(fval)
xlabel(\'iterations\')
ylabel(\'fval\')
RESULTS:
For case 1 it is clear that minimum values and fitness values are different(or scattered) for each iteration and some are not even close to the surface of the stalagmite function.
In case 2 as the lower bound and upper bound are given to ga it only takes minimum values in that range which gives..
In case3 population size is increased which gives the value.
END OF PROGRAM......
Resources: -
https://towardsdatascience.com/how-to-define-a-fitness-function-in-a-genetic-algorithm-be572b9ea3b4
https://in.mathworks.com/help/gads/ga.html
https://in.mathworks.com/help/gads/vary-mutation-and-crossover.html
https://en.wikipedia.org/wiki/Genetic_algorithm
https://en.wikipedia.org/wiki/Mutation_(genetic_algorithm)
https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)
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...
Centrifugal pump design and analysis
AIM: Analysis and simulation of the centrifugal pump and obtaining pressure ratio, mass flow rate. INTRODUCTION: Centrifugal pumps are used to transport fluids by the conversion of rotational kinetic energy to the hydrodynamic energy of the fluid flow. The rotational energy typically comes from an engine or electric…
19 Aug 2020 07:11 PM IST
Modelling and simulation of flow through a flowbench
AIM: To obtain a plot of lift vs mass flow rate of flow through a flow bench. INTRODUCTION: An air flow bench is a device used for testing the internal aerodynamic qualities of an engine component and is related to the more familiar wind tunnel. It is used primarily for testing…
23 Jul 2020 02:11 PM IST
Flow over an airfoil
AIM:To run the Flow simulations on NACA0017 airfoil at an angle of attacks 0, 2, 4, 6, 8, 10 degrees. And compare the change in lift force and drag force at these angles of attacks. INTRODUCTION:An airfoil or aerofoil is the cross-sectional shape of a wing, blade (of a propeller, rotor,…
14 Jul 2020 03:39 PM IST
Flow over a cylinder
AIM: To Understand and Simulate the Transient Flow over a Cylinder in SolidWorks and observe the variation of velocity and pressure in a domain for three different Reynolds Numbers. INTRODUCTION:Flow over a cylinder is a classical problem for understanding the flow over an obstacle. The geometry of the cylinder is simple…
03 Jul 2020 04:13 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.