All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Stalagmite Coding as Genetic Algorithm: clear all close all clc % Defining our search space x = linspace(0,0.6,150); % Value of x varies from 0 to 0.6 into 150 terms. y = linspace(0,0.6,150); % Value of y varies from 0 to 0.6 into 150 terms. f_output = zeros; % Here f_output is defined as zero matrix because in for loop…
Ashish Mishra
updated on 11 Jul 2021
clear all
close all
clc
% Defining our search space
x = linspace(0,0.6,150); % Value of x varies from 0 to 0.6 into 150 terms.
y = linspace(0,0.6,150); % Value of y varies from 0 to 0.6 into 150 terms.
f_output = zeros; % Here f_output is defined as zero matrix because in for loop f_output is a function of stalagmite.
% Creating a 2D mesh
[xx, yy] = meshgrid(x, y); % Defines value of xx & yy as meshgrid(x,y).
% Evaluating the stalagmite function in loop
for i = 1:length(xx) % Length command is used as xx matrix is of 150*150.
for j = 1:length(yy)
f(1)=xx(i,j);
f(2)=yy(i,j);
tic % This is used to start the evaluation of time for f_output.
f_output(i,j) = stalagmite_func(f); % The value of f_output is obtained as stalagmite functions value.
end
end
toc % This is used to end the evaluation of time for f_output.
figure(1) % This figure is to get a general mesh surface plot.
surfc(xx,yy,f_output,'MeshStyle','none') % This is 3D surface plot with solid edge & solid face colors.
xlabel('No. of Iterations') % Define x-axis label.
ylabel('Functions maximum value') % Define y-axis label.
zlabel('Global maximum function') % Define z-axis label.
title('Global maxima v/s Iterations') % Define title of a plot.
hold on % Stops & hold writing of commands till this point.
[inputs, fopt] = ga(@stalagmite_func,2); % Here genetic algorithm is defined outside "for" loop to put a hold on the looping of GA.
fxopt = inputs(1);
fyopt = inputs(2);
% Study1: Evaluation Stalagmite function as Genetic Algorithm without initial and final boundary conditions
% No. of Iterations
num_max = 50; % " Iterations are used to create a lot of samplings as GA is a set of random numbers. "
for i = 1:num_max % Here direct "num_max" is used as 1*1 matrix is available.
tic
[inputs, fopt(i)] = ga(@stalagmite_func,2); % This is a operating command for Genetic Algorithm as "[x,fval] = ga(fun,nvars)".
fxopt(i) = inputs(1); % Value of x is a function of GA as (i) in loop for 50 iterations.
fyopt(i) = inputs(2); % Value of y is a function of GA as (i) in loop for 50 iterations.
end
studytime1 = toc; % This is a time study for Study1.
figure(2) % This figure will give surface plot for Study 1.
subplot(2,1,1) % Divides the current figure into an m-by-n grid and creates axes in the position specified by p.
surfc(xx,yy,f_output,'Meshstyle','none')
shading interp % Varies the color in each line segment and face by interpolating the colormap index.
hold on
plot3(fxopt,fyopt,fopt,'Marker','o','Markersize',5,'MarkerFaceColor','r') % This create point markers in surface plot.
subplot(2,1,2)
plot(fopt) % Creates 2D plot for fopt.
title('Independent GA Plot')
xlabel('No. of Iterations')
ylabel('Minimum Function')
% Study2: Evaluation Stalagmite function as Genetic Algorithm with initial and final boundary conditions
% No. of Iterations
num_max = 50;
for i = 1:num_max
tic
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
% Stalagmite function is provided by Initial & Final boundary
% conditions as "[x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)".
fxopt(i) = inputs(1);
fyopt(i) = inputs(2);
end
studytime2 = toc; % This is a time study for Study2.
figure(3) % This figure will give surface plot for Study 2.
subplot(2,1,1)
surfc(xx,yy,f_output,'Meshstyle','none')
shading interp
title('Global maxima v/s Iterations as Bounded')
hold on
plot3(fxopt,fyopt,fopt,'Marker','o','Markersize',5,'MarkerFaceColor','r')
axis([-0.2 0.6 -0.2 0.6 -1 0]) % Defines axis coordinates for figure 3.
subplot(2,1,2)
plot(fopt)
title('Dependent GA Plot')
xlabel('No. of Iterations')
ylabel('Minimum Function')
% Study3: Evaluation Stalagmite function as Genetic Algorithm with initial and final boundary conditions with Population size
% No. of Iterations
num_max = 50;
options = optimoptions('ga'); % Create optimization options and returns a set of default option for Solvername.
options = optimoptions(options, 'PopulationSize',300); % Create optimization options and returns options with the named parameters altered with the specified values.
for i = 1:num_max
tic
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
% Stalagmite function is provided by Initial & Final boundary
% conditions as "[x,fval] = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)".
fxopt(i) = inputs(1);
fyopt(i) = inputs(2);
end
studytime3 = toc; % This is a time study for Study2.
figure(4) % This figure will give surface plot for Study 3.
subplot(2,1,1)
surfc(xx,yy,f_output,'Meshstyle','none')
shading interp
title('Global maxima v/s Iterations as Bounded with Population size')
hold on
plot3(fxopt,fyopt,fopt,'Marker','o','Markersize',5,'MarkerFaceColor','r')
axis([-0.2 0.6 -0.2 0.6 -1 0])
subplot(2,1,2)
plot(fopt)
axis([0 50 -1 0])
title('Population Dependent GA Plot')
xlabel('No. of Iterations')
ylabel('Minimum Function')
Explanation:
Firstly the function is called in 'for loop' for defined values of x & y from the mesh grid. Output value for stalagmite function is 'f_output' and this value is in a loop so this matrix is giving random values in the loop and this gives an error which is sorted either by providing it zeros, any cell value, or other.
Similarly, we have to give the value for fxopt, fyopt, fopt....where opt is optimum. Now, this is plotted on the surface as figure 1.
Now creating 3 different studies for stalagmite function in form of a genetic algorithm. Study1 creates a basic GA in which we get random stalagmite coefficients. In Study1 no boundary condition is defined.
For Study2 we just define the boundary conditions as bounded regions and we get a limited range of value for GA. When we move further and define Study3, we provide population and optimization options for creating a proper output loop whenever the program is running.
Genetic Algorithm: For Matlab, GA is a set of random numbers with various parameters to follow. And each parameter gives a different optimized conclusion. Biologically these parameters work as mutation, crossover, and selection. In our coding, we used 3 GA functions:-
Here, fun = stalagmite function; nvars = no. of variables (2); A & b = linear inequalities (default : []) ; Aeq & beq = linear equalities (default = []) ; lb = lower bound (0,0 : initial boundary values); ub = upper bound (0.6,0.6 : final boundary values); nonlcon = non-linear constraints (default : [] ); IntCon = integer constraints (default : [] ); options = gives optimized output as provided before defining GA; x = inputs obtained during execution of a during iteration; fval = objective function value that give solutions.
function[f_output] = stalagmite_func(f) % Creates a stalagmite function
x = f(1); % Provides the value of x from a for loop.
y = f(2); % Provides the value of y from a for loop.
fx1 = (sin(5.1*pi*x + 0.5))^6;
fx2 = exp(-4*log(2)*((x-0.0667)^2/(0.64)));
fy1 = (sin(5.1*pi*y + 0.5))^6;
fy2 = exp(-4*log(2)*((y-0.0667)^2/(0.64)));
f_output = -(fx1*fy1*fx2*fy2); % Formula for creating the stalagmite function.
end
This is a formula for stalagmite function :
Elapsed time is 0.000714 seconds.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Time evaluation for : Study1 = 0.0608; Study2 = 0.0611; Study3 = 0.2299. In Study1 we just provided basic formula for 'genetic algorithm', [x,fval] = ga(fun,nvars); now for Study2 we created lower and upper bound for the present GA formula, [x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub); but all above studies do not give perfect output for "stalagmite function", so to get perfect response from randomly terminated value of genetic algorithm we need to specify population and optimization options this is done in Study3, [x,fval] = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
" Stalagmite function with no genetic algorithm alternation ".
" This plot is driven by the help of "Genetic Algorithm", where GA is defined with no boundary & population condition ".
" In this plot, GA is defined with help of boundary condition which helps to manage the output values from GA to a small range of stalagmite sets ".
" This plot gives the perfect output for the genetic algorithm when population density is defined with optimization options ".
REFERENCES:
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 - 2D meshing for Sheet metal
Objective: Extract the mid-surface surface of the “Hood-panel”, by clearing all geometrical errors and mesh it with defined ‘Quality criteria’ along with perfectly structured mesh elements. PROCEDURE: CAD Model Image – We have a CAD model of the front hood in the format of .step,…
27 May 2023 06:05 PM IST
Project 1 : CFD Meshing for Tesla Cyber Truck
Project - TESLA (DUMMY) CYBERTRUCK VOLUME MESH OBJECTIVE: Check and solve all geometrical errors and assign appropriate PIDs. Perform meshing with the suitable target length and element quality criteria. Target lengths for the different parts of a model can be decided on your own. Meshing wind tunnel volume with tetra…
27 Sep 2022 11:27 PM IST
Week 5 Challenge : Surface wrap on Automotive Assembly
AIM: To perform surface wrap meshing in an assembled model with the entire geometric cleanup. PROCEDURE: The 'aim' of this challenge is to perform cleanup geometry for all three models individually & then assemble them and apply surface wrap mesh (target length = 3mm). Download all three models - Engine, Gearbox…
22 Sep 2022 11:34 AM IST
Week 4 Challenge : CFD Meshing for BMW car
AIM: Check & solve all geometric errors, surface meshing with the given target length for half portion of the model and assign PIDs appropriately. Look for the perfect element Quality criteria by clearing off elements to zero and then performing a complete volume mesh. Surface & volume meshing of the wind tunnel.…
16 Sep 2022 08:43 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.