All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To optimise Stalagmite Function and find Global Maxima of the Function using MATLAB INTRODUCTION TO GENETIC ALGORITHM A genetic algorithm (GA) is a search and optimization method which works by imitating the evolutionary principles and chromosomal processing in natural genetics. Genetic Algorithm begins…
Vyshakh Raju
updated on 23 Feb 2020
AIM:
To optimise Stalagmite Function and find Global Maxima of the Function using MATLAB
INTRODUCTION TO GENETIC ALGORITHM
clear all
close all
clc
a = linspace(0,0.8,150);
b = linspace(0,0.8,150);
%creating input matrix
[aa,bb] = meshgrid(a,b);
%obtaining the output from the function
for i = 1: length(a)
for j = 1:length(b)
inputs(1) = aa(i,j);
inputs(2) = bb(i,j);
P(i,j) = STALAGMITE(inputs);
%P(i.j) is a matrix of output from the STALAGMITE function
end
end
figure(1)
surfc(aa,bb,-P)
shading interp
title(\'FUNCTION PLOT\')
colorbar
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
%fixing total number of cases.
nc = 50;
%case 1 - unbounded condition
for i = 1: length(a)
for j = 1:length(b)
inputs(1) = aa(i,j);
inputs(2) = bb(i,j);
P(i,j) = STALAGMITE(inputs);
%P(i.j) is a matrix of output from the STALAGMITE function
end
end
tic
for k = 1:nc
[inputsS1, fnS1(k)] = ga(@STALAGMITE,2);
xnS1(k) = inputsS1(1);
ynS1(k) = inputsS1(2);
end
Runtime_unbound = toc;
%ploting the surface plot and marking the maxima
figure(2)
subplot(2,1,1)
surfc(aa,bb,-P)
%-ve for P since ga gives the minima and hence inverting for maxima.
shading interp
colorbar
title(\' UN-BOUND OR UN-CONSTRAINED\')
hold on
plot3(xnS1,ynS1,-fnS1,\'o-\',\'Markerfacecolor\',\'r\',\'Markersize\',4)
%using -fnS1 since the we have to plot the maxima points
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
subplot(2,1,2)
plot(-fnS1)
%using -fnS1 since the we have to plot the fitness points
xlabel(\'No. of iterations\')
ylabel(\'Function MAX\')
%CASE 2 x = ga(fun,2,A,b,Aeq,beq,lb,ub) - providing boundaries
for i = 1: length(a)
for j = 1:length(b)
inputs(1) = aa(i,j);
inputs(2) = bb(i,j);
P(i,j) = STALAGMITE(inputs);
%(i.j) is a matrix of output from the STALAGMITE function
end
end
tic
for o = 1:nc
[inputsS2, fnS2(o)] = ga(@STALAGMITE,2,[],[],[],[],[0;0],[0.4;0.4]);
xnS2(o) = inputsS2(1);
ynS2(o) = inputsS2(2);
end
Runtime_bound = toc;
%ploting the surface plot and marking the maxima
figure(3)
subplot(2,1,1)
surfc(aa,bb,-P)
%-ve for P since ga gives the minima and hence inverting for maxima.
shading interp
colorbar
title(\' BOUND OR CONSTRAINED\')
hold on
plot3(xnS2,ynS2,-fnS2,\'o-\',\'Markerfacecolor\',\'r\',\'Markersize\',5)
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
subplot(2,1,2)
plot(-fnS2)
%using -fnS2 since the we have to plot the fitness points
xlabel(\'No. of iterations\')
ylabel(\'Function MAX\')
%CASE 3 Bounded with fixed Population
for i = 1: length(a)
for j = 1:length(b)
inputs(1) = aa(i,j);
inputs(2) = bb(i,j);
P(i,j) = STALAGMITE(inputs);
%(i.j) is a matrix of output from the STALAGMITE function
end
end
tic
options = optimoptions(\'ga\')
options = optimoptions(options,\'populationsize\',250)
for o = 1:nc
[inputsS3, fnS3(o)] = ga(@STALAGMITE,2,[],[],[],[],[0;0],[0.4;0.4],[],options);
xnS3(o) = inputsS3(1);
ynS3(o) = inputsS3(2);
end
Runtime_pop_bound = toc;
%ploting the surface plot and marking the maxima
figure(4)
subplot(2,1,1)
surfc(aa,bb,-P)
%-ve for P since ga gives the minima and hence inverting for maxima.
shading interp
colorbar
title(\' BOUND OR CONSTRAINED with Population\')
hold on
plot3(xnS3,ynS3,-fnS3,\'o-\',\'Markerfacecolor\',\'r\',\'Markersize\',6)
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
subplot(2,1,2)
plot(-fnS3)
%using -fnS3 since the we have to plot the fitness points
xlabel(\'No. of iterations\')
ylabel(\'Function MAX\')
type = [{\'unbound\';\'Bounded\';\'Bounded with options\'}];
time = [Runtime_unbound;Runtime_bound;Runtime_pop_bound];
RESULTS = table(type,time,\'VariableNames\',{\'STUDY TYPE\',\'STUDY TIME\'})
2) Function Definition
%Defining the Function
function [OP] = STALAGMITE(inputs)
%defining x and y
x = inputs(1);
y = inputs(2);
f1x = (sin(((5.1*pi)*x)+(0.5)))^6;
f2x = exp((-4*log(2))*(((x - 0.0667)^2)/0.64));
f1y = (sin(((5.1*pi)*y)+(0.5)))^6;
f2y = exp((-4*log(2))*(((y - 0.0667)^2)/0.64));
%providing the output from the function
OP = -1*f1x*f2x*f1y*f2y;
%-1 is multiplied to maximise the inputs or to obtain a max. value.
end
Code Explanation:
Line 4 - 7: Input array and matrix is defined and generated.
Line 10 - 17: using for loop to obtain output of the function for each value of the input matrix
Line 18 - 25 : Creating the plot of the given function using surfc command, -P is taken to plot the maximum values.
Line 28 : Fixing the numbe of iterstions required.
Line 30 - 37 : using for loop to obtain output of the function for each value of the input matrix
Line 39 - 43 : using genetic algorithm for the function to get maximised output. also tic - toc command used to find out the time taken for the current study.
Line 46 -53 : Creating the plot of the given function and condition using surfc command, -P is taken to plot the maximum values.
Line 54 - 63: plotting the points at which the function is having the maximum ( -fnS1 used to plot the maximum value, where fnS1 contains the minimum value)
Line 65 - 73 : using for loop to obtain output of the function for each value of the input matrix
Line 75 to 80 :using genetic algorithm for the function with boundaries to get maximised output. also tic - toc command used to find out the time taken for the current study.
Line 83 - 90 : Creating the plot of the given function and condition using surfc command, -P is taken to plot the maximum values.
Line 91 - 99 :plotting the points at which the function is having the maximum ( -fnS2 used to plot the maximum value, where fnS2 contains the minimum value)
Line 101 - 109 :using for loop to obtain output of the function for each value of the input matrix
Line 112-113 : defining options to get the global maxima value( defining the population size)
Line 114 - 119 :using genetic algorithm for the function with boundaries and population size to get maximised output. also tic - toc command used to find out the time taken for the current study.
Line 122 -129 : Creating the plot of the given function and condition using surfc command, -P is taken to plot the maximum values.
Line 130 - 139: plotting the points at which the function is having the maximum ( -fnS3 used to plot the maximum value, where fnS3 contains the minimum value)
Line 139 - 141: Taking the time taken to a table to display as output.
Function description:
initially x and y are defined as inputs to the functions respectively.
Then the definition of function is done.
The result of the function is assigned to a variable. The negative sign is required since Genetic algorithm minimises a function , so the negative of the function helps in creating the maximum values.
S
yntax For GA :
Case 1
produces minimum x for function \'fun\', having \'nvars\' number of design variables.
CASE 2
x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
)
In this case, lower and upper boundary is specified where the solution lies in between the boundaries. Here since linear inequalities are not considered, replace A,b, Aeq, beq by [].
Case 3
x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
,nonlcon
,options
)
\'nonlcon\' represents the non linear inequalities which is not considered,hence replaced by [] in the syntax. The options variable is used for optimisation within a specified size. options can be defined using optimoptions command.
RESULT:
1) Function Plot
2) Case 1 - UN-BOUNDED Condition
3) Case 2 - Function with Lower and Upper Boundaries
4) Case 3 - Function with Boundaries and Specified population size
5) Time To Study
Errors Faced:
1) Error : x and y value not defined for the function
Correction: x and y defined within the function
Screen Shot:
2) Error : x and y value wrongly defined for the function
Correction: x and y defined as per need of the function
Screen Shot:
3) Error : wrong description of command population size
Correction: \'size\' changed to \'populationsize\'
Screen Shot:
4) Error : synatx error while using the ga command with boundaries and options
Correction: missing value as per syntax was provided
Screen Shot:
CONCLUSION:
The maxima for the function was obtained by inversing the function output using Genetic Algorithm operation. Through the process of selection, cross-over and mutation, the Global Maxima for the function is produced. Time for each study reveals the amount of processing required for obtaining the Global Maxima starting from a set of Local Maximas.
REFERENCES:
https://link.springer.com/
https://www.neuraldesigner.
https://in.mathworks.com/discovery/genetic-algorithm.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...
Radar Mast & Final Assembly of Yacht
Modelling a Yacht with Solidworks OBJECTIVE: To model different parts of a Yacht To assemble each parts of yacht to create the full model INTRODUCTION: This project is focused in partwise modelling of the Yacht and then assembly of the parts together inorder to obtain the full Yacht model DESIGN METHODOLOGY:…
14 Jun 2021 10:52 AM IST
Photo Realistic Rendering
MODELLING OF AN RT66 AMERICAN CHOPPER OBJECTIVE: To model each parts of an RT66 Chopper Assemble the each parts of Chopper to obtain full Scale model of RT66 Chopper Render the Chopper model to a realistic View INTRODUCTION: This projuct is focused on modelling of an American Chopper model part wise,…
07 Jun 2021 04:07 PM IST
Advanced Sheet Metal Design Using NX Cad Challenge_7_ Metal bracket-II
DESIGNING A METAL BRACKET WITH NX SHEET METAL APPLICATION OBJECTIVE: To create a Metal bracket with respect to the given 2-D Drawing with NX sheet Metal Application. INTRODUCTION: This work focuses on designing a Metal Bracket with NX Sheet metal Application with respect to a given 2-D Drawing for dimensions.…
30 May 2021 01:35 PM IST
Advanced Sheet Metal Design Using NX Cad Challenge_6_Bracket
BRACKET DESIGN USING NX SHEET METAL APPLICATION OBJECTIVE: To design a Bracket with the specified dimensions and contour using NX CAD Sheet Metal Application. INTRODUCTION: This work is focused to design and create a Bracket as per specified dimensions and contour in the 2-D drawing. DESIGN METHODOLOGY: PROCEDURE:…
30 May 2021 06:34 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.