All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Genetic Algorithm: - 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. …
MANAPRAGADA BHANU MURTHY
updated on 23 Jan 2022
Genetic Algorithm: -
Five phases are considered in a genetic algorithm.
Initial Population: -
Fitness Function: -
Selection: -
Crossover: -
For example, consider the crossover point to be 3 as shown below.
The new offspring are added to the population.
Mutation: -
Termination: -
The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
Syntax of GA: -
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)
finds a local minimum 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'
).
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)
subjects the minimization to the constraints defined in nonlcon
. The function nonlcon
accepts x
and returns vectors C
and Ceq
, representing the nonlinear inequalities and equalities respectively. ga
minimizes the fun
such that C(x)
≤ 0
and Ceq(x) = 0
. (Set lb=[]
and ub=[]
if no bounds exist.)
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon, options)
minimizes with the default optimization parameters replaced by values in options
. (Set nonlcon=[]
if no nonlinear constraints exist.) Create options
using optimoptions
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon, intcon)
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon, intcon, options)
x = ga(problem)
finds the minimum for problem
, a structure described in problem.
[x, fval] = ga(_)
for any previous input arguments, also returns fval
, the value of the fitness function at x
.
[x, fval, exitflag, output] = 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.
[x, fval, exitflag, output, population, scores] = ga(_)
also returns a matrix population
, whose rows are the final population, and a vector score, the scores of the final population.
Program for creating stalagmite Function: -
%% Creating a function to obtain global maxima for the given Objective Function
function [F_x] = stalagmite_func(input_vector)
% x,y are the design variables
x = input_vector(1);
y = input_vector(2);
% F_x is the objective function where F_1, F_2, F_3, F_4 are the components of the function
F_1 = (sin(5.1*pi*x+0.5))^6;
F_2 = (exp(-4*log(2)*((x-0.0667)^2)/0.64));
F_3 = (sin(5.1*pi*y+0.5))^6;
F_4 = (exp(-4*log(2)*((y-0.0667)^2)/0.64));
F_x = -(F_1*F_2*F_3*F_4); % -ve sign used to minimize the function
end
Program to obtain global Minima of the function: -
%% Substituting the Inputs to get the gobal maxima
clear all
close all
clc
%% Inputs
% taking the Inital values of x, y using linspace
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Giving number of iterations
num_iter = 50;
%% Converting the x, y values into 2d Array using Meshgrid
[xx yy] = meshgrid(x,y);
%% Case 1: substituting the x, y values in function Using for loop without ga
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_func(input_vector);
end
end
% Plotting
figure(1)
surfc(xx,yy,f)
shading interp
%% Case 2: applying GA algorithm for the function and substituting the x, y values
tic
for i = 1:num_iter
[results, fopt(i)]= ga(@stalagmite_func,2);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time = toc
% Plotting the optimum value of x, optimum value of y & function optimum
figure(2)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('maximum Function')
%% Case 3: Giving the lower bounds & upper bounds
tic
for i = 1:num_iter
[results, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time1 = toc
% Plotting the optimum value of x, optimum value of y & function optimum
figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('maximum Function')
%% Case 4: Initializing the population size using optimoptions for GA
options = optimoptions('ga','PopulationSize',300)
tic
for i = 1:num_iter
[results, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time2 = toc
% Plotting the optimum value of x, optimum value of y & function optimum
figure(4)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('maximum Function')
Explanation of code: -
Plotting: -
Case 1
Case 2: -
Case 3: -
Case 4: -
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 1 - Parsing NASA thermodynamic data
Aim: - Write a Matlab program that extracts 14 co-efficient and to calculate the entropy, enthalpy, and specific heats for all the species in the given NASA Thermodynamic data file data. Also, to calculate the molecular weight of each species. File Parsing: - Parsing, which is the process of identifying…
03 May 2022 06:52 PM IST
FINAL INDEPENDENT PROJECT
Final Independent Project: - Topic: - Multi-objective optimal design of cycloid speed reducer based on genetic algorithm. Platform used to Solve the problem: - Matlab Introduction: - Cycloid speed reducers have found wide applications in the automation field as industry robots, machine tools, automatic machinery and other…
10 Apr 2022 10:32 AM IST
Project 2 - Rankine cycle Simulator
Aim: To create a Rankine Cycle simulator using Matlab. Objective: To calculate the state points of the Rankine cycle. To plot the T-S, H-S Diagrams of Rankine cycle. Theory: The Rankine cycle is an idealized thermodynamic cycle describing the process by which certain heat engines, such as steam turbines…
03 Apr 2022 07:27 PM IST
Week 4.1 - Genetic Algorithm
Genetic Algorithm: - 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. …
23 Jan 2022 07:40 AM 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.