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 program to optimise the stalagmite function using genetic algorithm in matlab. Objective:- Write a program to optimise the stalagmite and find the global maxima of function. Explain the concept of genetic algorithm and also the syntax for “ga” in matlab. Plot graphs for all 3 studies and for…
SIDDHESH PARAB
updated on 08 Aug 2021
Aim :- Write a program to optimise the stalagmite function using genetic algorithm in matlab.
Objective:-
THEORY :-
Optimization:-
Genetic Algorithm (GA):-
Fig. Flowchart of working of Genetic Algorithm
The following outline summarizes how the genetic algorithm works:
The algorithm begins by creating a random initial population. In the initialisation, first we have to decide the population size. Higher the population size, we will get more combinations, hence we get more accurate results.Population is the number of sample data points taken to perform the genetic algorithm operation.
Fitness function measures how good your solution is. Fitness function takes the solution as input and produces the
suitability of the solution as the output. This evaluates how close the solution is to the optimal solution of the desired problem.
The idea is to give preference to the individuals with good fitness scores and allow them to pass there genes to the successive generations. The fitness value of the given function will be computed, the fittest value will have the most probability in the Roulette wheel as shown below.
In a roulette wheel selection, the circular wheel is divided with respect to the Fitter individual. A fixed point is chosen on the wheel circumference as shown and the wheel is rotated. The region of the wheel which comes in front of the fixed point is chosen as the parent. For the second parent, the same process is repeated. In this way, Fitter individual have a higher chance of mating and propagating their feature to the next generation.
This represents mating between individuals. Two individuals are selected using selection operator and crossover sites are chosen randomly. Then the genes at these crossover sites are exchanged thus creating a completely new individual (offspring).
It is the process of taking two parents and producing the childs from them by interchanging the genes.
eg.
Mutation is carried out to maintain the genetic diversity from one generation of a population of chromosomes to the
next. In this process, the genes will be replaced by some random integers.
If mutation is not performed, the chromosomes will become to similar to each other,convergence takes place soon and results in local minima.
eg.
The algorithm terminates if the population has 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.
OPTIMISATION OF STALGAMITE FUNCTION WITH USAGE OF SYNTAX OF 'ga' :-
Stalgamite function is carried out in three different studies. They are listed below;
Study 1 : Caliberation under UNBOUNDED STATISTICAL BEHAVIOUR
The range of the genetic algorithm is unbounded and hence the values of global maxima along with the values of x & y are
defined out of the search space.
Syntax:- [x,y] = ga(fun,nvars)
[inputs, fopt1(i)] = ga(@stalagmite,2) -------(% Syntax used in the program)
In the program x & y represents [input, fopt(i)]
fun - The function to be optimised (i.e) stalagmite
navrs - The no.of variables present (i.e. 2 (since x & y there are two variables))
Study 2 : Caliberation under BOUNDED STATISTICAL BEHAVIOUR between upper bounds and lower bounds
The range of the genetic algorithm is bounded by introducing the values of lower & upper bounds and hence the global maxima along with values of x & y is defined within search space.
Syntax:- [x,y] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
In the program x ,y ,fun ,nvars is same as study 1
A = Linear inequality constraints
b = Linear inequality constraints
Aeq = Linear equality constraints
beq = Linear equaltiy constraints
lb = Lower bounds
ub = Upper bounds
(NOTE : For the above syntax A, b, Aeq, beq '[]' is used in place of them since there are no values for them.
Therefore, syntax used in the program is ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
Study 3 : Caliberation under BOUND STATISTICAL BEHAVIOUR WITH OPTIMISED OPTIONS BY INCREASING THE NUMBER OF ITERATIONS
To improve accuracy the command optimoptions is introduced for increasing the population. Apart from which everything is
similar to study 2.
Syntax :- [x,y] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlon,IntCon,options)
In the program x ,y ,fun ,nvars ,A ,b ,Aeq ,beq ,lb ,ub is same as study 1 & 2
nonlcon = Non-Linear constraints
intCon = Integer variables
options = Optimisation options (Syntax : options = optimoptions(SolverName))
(NOTE : For the above syntax A, b, Aeq, beq '[]' is used in place of them since there are no values for them same as study 2.
Therefore, syntax used in the program is ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
Significance of Iterations:-
STALAGMITE FUNCTION :-
It is a mathematical function which helps us in determining the Global minima & maxima by implementing a 3D model.
Function for determining the global maxima & minima is given as under;
Program for determining stalagmite function:-
function [f] =stalagmite(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);
end
Main Code :-
% To evaluate global maxima and statistical behaviour of stalagmite function using genetic algorithm
clear all;
close all;
clc;
% Creating and storing the data in an array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Running the code for number of iterations/repeatation
no_of_iterations = 50;
%Creating the 2D mesh array
[xx,yy] = meshgrid(x,y);
% Evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
inputvector(1) = xx(i,j);
inputvector(2) = yy(i,j);
f(i,j) = stalagmite(inputvector);
end
end
surf(xx,yy,-f);
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Function Value');
title('Stalagmite Function');
shading interp;
[x_output, fval] = ga(@stalagmite,2);
%%Study 1 - Caliberation under UNBOUND STATISTICAL BEHAVIOR
tic; % tic command for calculating iteration time
for i = 1:no_of_iterations
[inputs, fopt1(i)] = ga(@stalagmite,2);
xopt1(i) = inputs(1);
yopt1(i) = inputs(2);
end
study_time_1st_case = toc;
%Plotting
figure(2);
subplot(2,1,1);
hold on;
surf(x,y,-f);
shading interp;
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Function Value')
hold on;
plot3(xopt1, yopt1, -fopt1, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
title('UNBOUNDED STATISTICAL BEHAVIOR');
subplot(2,1,2);
plot(-fopt1);
xlabel('Number of Iterations');
ylabel('Function Maximum');
%%Study 2 - Calibaration under BOUNDED STATISTICAL BEHAVIOR
tic;
for i = 1:no_of_iterations
[input, fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt2(i) = input(1);
yopt2(i) = input(2);
end
study_time_2_2nd_case = toc;
%Plotting
figure(3);
subplot(2,1,1);
hold on;
surf(x,y,-f);
shading interp;
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Function Value');
hold on;
plot3(xopt2, yopt2, -fopt2, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
title('STATISTICAL BEHAVIOR WITH UPPER AND LOWER BOUNDS');
subplot(2,1,2);
plot(-fopt2);
xlabel('No. of Iterations');
ylabel('Function Maximum');
%%Study 3 - Calibaration under BOUND STATISTICAL BEHAVIOR WITH OPTIMISED OPTIONS
tic;
options = optimoptions('ga');
options = optimoptions(options,'populationsize',250);
for i = 1:no_of_iterations
[input, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3(i) = input(1);
yopt3(i) = input(2);
end
study_time_3rd_case = toc;
%Plotting
figure(4);
subplot(2,1,1);
hold on;
surf(x,y,-f);
shading interp;
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Function Value');
hold on;
plot3(xopt3, yopt3, -fopt3, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r');
title('BOUNDED STATISTICAL BEHAVIOR WITH OPTIMISED OPTIONS BY INCREASING ITERATIONS');
subplot(2,1,2);
plot(-fopt3);
xlabel('No. of Iterations');
ylabel('Function Maximum');
Explanation of the code:-
Output :-
figure 1
Study 1:- Caliberation under UNBOUNDED STATISTICAL BEHAVIOUR
Study 2:- Caliberation under STATISTICAL BEHAVIOUR WITH UPPER AND LOWER BOUNDS
Study 3:- Caliberation under BOUNDED STATISTICAL BEHAVIOUR WITH OPTIMISED OPTIONS BY INCREASING ITERATIONS
Workspace :-
Results of all the 3 studies along with the main stalagmite function and output values in workspace is shown above.
In study 1, 2 & 3
Top Plots : Here the surf command with location of global maxima with respect to each iteration for each studies is shown.
Bottom Plots : The value of Global Maxima varying over different Iterations is plotted for each study.
RESULTS:-
By re-running the program the results of studies 1 & 2 varied unlike study 3 which was ideal for every run.
So from the above results, in order to find the Global maxima, population has to be increased to get the desired output, so
we can conclude that study 3 is the most accurate one.
Study 2 & 1 is followed by that in decreasing order.
Time Taken for Study 1 = 2.7595
Time Taken for Study 2 = 4.596283200000000
Time Taken for Study 3 = 21.117639200000000
REFERENCES:
1.https://in.mathworks.com/discovery/genetic-algorithm.html
2. https://in.mathworks.com/help/gads/genetic-algorithm.html
3. https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.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...
Project 2-Highway Assistant-Lane Changing Assistant
Aim :- Model Based Development of Highway Assistant – Lane Changing Assistant Objective :- To develop one specific requirement of Highway Assistant – Lane Changing Assistant algorithm. Please note that the whole Highway Assistant – Lane Changing Assistant is a very huge algorithm & only one small…
14 Feb 2022 04:19 PM IST
Project 1- Traffic Jam Assistant Feature
Aim :- Traffic Jam Assistant Feature Objective :- This model must be developed in MATLAB Simulink as per MBD guidelines according to the requirements given. Tag the requirements to the Simulink model, Requirements 1 & Requirement 2 are tagged into their corresponding subsystems. Creation of Simulink Data Dictionary…
05 Feb 2022 06:55 PM IST
Design of an Electric Vehicle
AIM:- Create a MATLAB model of an electric car that uses a battery and a DC motor. Choose suitable blocks from the Powertrain block set. Prepare a report about your model including the following: ABSTRACT:- In this project, we going to build the MATLAB model of ELECTRIC CAR by using a DC motor and a suitable battery for…
01 Feb 2022 06:47 AM IST
Project 2 Adaptive Cruise Control
Aim :- Develop a MATLAB Simulink Model for Adaptive Cruise Control feature used in Automotive Vehicle Objective:- To develop a Adaptive Cruise Control feature as per the Requirement Document using MATLAB Simulink. While Developing the Adaptive Cruise Control Simulink Model, we have follow all the MBD related processes…
22 Jan 2022 08:13 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.