All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: Make a Mat Lab code to solve anonymous function stalagmite with Genetic Algorithm and generate plot with finding global maxima. OBJECTIVE: …
Aniket Kumbhar
updated on 06 Jun 2022
AIM: Make a Mat Lab code to solve anonymous function stalagmite with Genetic Algorithm and generate plot with finding global maxima.
OBJECTIVE:
Make a Mat Lab code to solve anonymous function stalagmite with Genetic Algorithm and generate plot. Explaining the concept and studying the behaviour of genetic algorithm to achieve constant global maxima.
SOLUTION:
A Mat Lab code was written for curve fit. To find fitting the liner and cubic polynomials to identify errors generated in original curve vs. fitted curve. Further code generates a plot for it. With respect to the input variables given.
EXPLANATION:
Genetic Algorithm in Mat-Lab use for solve analysis and search problems. Genetic Algorithm is theory of Charles Darwin's natural evolution. Genetic Algorithm involves the process of selection where the fittest individual are selected for reproduction in order to produce offspring’s of next generation (i.e., traits are passed to one generation to another generation).
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, no differentiable, stochastic, or highly nonlinear. Genetic Algorithm is method of solving constrained and unconstrained optimization problem. Optimization is the making the best and effective use of resource.
Constrained Optimization: It is process of optimizing objective function with respect to some variable.
Unconstrained Optimization: In this optimization deals with minimizing an objective function depending on real variable.
Genetic Algorithm takes random samples, i.e., it takes random inputs and based on that random inputs it evaluates the function and then it sticks on natural selection where it takes two generation and mutate them and do crossover and create a new generation which is hopefully better. The above concept works in executing the 'ga' function in matlab and it optimizes the stalagmite function.
Stalagmite Function –
From this function we can obtain some geometrical structure using math function.
Program For Stalagmite Function –
function f = stalagmite(input_vector);
x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1*pi*x+0.5)^6);
f1y = (sin(5.1*pi*y+0.5)^6);
f2x = exp(-4*log(2)*(x-0.0667)^2/0.64);
f2y = exp(-4*log(2)*(y-0.0667)^2/0.64);
f = -(f1x.*f1y.*f2x.*f2y);
end
Here,
we created the stalagmite function , ‘stalagmite’ and passing ‘input_ vector’ as the arguments for the variable x and y , then value of input vector is added in variable x and y. user define mathematical stalagmite function is defined and variable f is used to define then the sol. Is, multiplied by 1 to get downward stalagmite.
The main program –
%to solve the genetic algorithm
close all
clear all
clc
%1 the dimentional array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%storing 150 values of x and y between 0 and 0.6
%runing Genetic algorithm for 50 times
num_case = 50;
%cearting 2 dimentional array using mesh
[xx, yy] = meshgrid(x,y);
%each of value of x,y creating array called input vector
%evaluting the 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
%Case study 1
%statistical behaviour of Genetic algorithm
%with providing boundries
tic % starting stopwatch timer
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite,2);
xopt(i)= input(1);
yopt(i)= input(2);
end
% reading the time taken by stopwatch
study_time1 = toc
%ploting case study
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
%converts the 2nd plot into 3d plot
xlabel('x values')
ylabel('y values')
zlabel('z values')
shading interp
%control the color shading of surface of surfec by interpolating the
%colormap index
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function main')
%case study 2
%with Bounded conditions ( upper bound and lower)
tic
for i = 1:num_case;
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
xopt(i)= input(1);
yopt(i)= input(2);
end
study_time2 = toc
figure (2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5)
title('Bounded Inputs')
xlabel('x values')
ylabel('y values')
zlabel('z values')
subplot(2,1,2)
plot(-fopt)
xlabel('Interations')
ylabel('Function minimum')
zlabel('f values')
%case study 3
%with increasing the fenetic algoritham iteration
%Bounded conditions ( upper bound and lower bound)
option = optimoptions('ga')
option = optimoptions(option,'PopulationSize',300);
%creates a set of the options with each option set to its defelt value.
%population size is set to 300
tic
for i = 1:num_case;
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6;0.6],[],[],option);
xopt(i)= input(1);
yopt(i)= input(2);
end
study_time3 = toc
figure (3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5)
title('Bounded Inputs with Population Size')
xlabel('x values')
ylabel('y values')
zlabel('z values')
subplot(2,1,2)
plot(-fopt)
xlabel('Interations')
ylabel('Function minimum')
zlabel('f values')
title('Maximum Iteration')
function -
function f = stalagmite(input_vector);
x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1*pi*x+0.5)^6);
f1y = (sin(5.1*pi*y+0.5)^6);
f2x = exp(-4*log(2)*(x-0.0667)^2/0.64);
f2y = exp(-4*log(2)*(y-0.0667)^2/0.64);
f = -(f1x.*f1y.*f2x.*f2y);
end
Procedure –
The program starts with command of clear all, close all, clc. Then it stores 150 values of x and y using linspace command. The num_space variable used for genetic algorithm for 50 times in order to global maxima. After that creating the 2D array by mesh grid command. Creating 2D array of xx’ and yy’ array using counter. Providing the values to the variable 1 and 2. Using for loop. Starting for loop from 1 to length of xx’ array using counter ’i’. the nested loop is created using the counter ‘j’ saving the values of xx’ and yy’ in variable input vector 1 & 2. The final stage is to be call function ‘stalagmite’.
%to solve the genetic algorithm
close all
clear all
clc
%1 the dimensional array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%storing 150 values of x and y between 0 and 0.6
%runing Genetic algorithm for 50 times
num_case = 50;
%cearting 2 dimensional array using mesh
[xx, yy] = meshgrid(x,y);
%each of value of x,y creating array called input vector
%evaluting the 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
We are using the stopwatch timer ‘tic’ for solving the statistical behaviour of genetic algorithm. Starting ‘for’ loop from 1 to length of num_case variable ‘I’. Calling the genetic algorithm ‘ga’ an inbuilt matlab function. To solve ‘stalagmite’ function. ‘Input’ is the array to store optimum values of the variable. The input and fopt area outputs of the genetic algorithm. Saving the time taken by the program in the variable ‘study_time1’. Plotting the case study 1 figure create the 3d surface plot. With the solid edge colour and face colour. Then labelling and x, y axis naming title.
%Case study 1
%statistical behaviour of Genetic algorithm
%with providing boundaries
tic % starting stopwatch timer
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite,2);
xopt(i)= input(1);
yopt(i)= input(2);
end
% reading the time taken by stopwatch
study_time1 = toc
%ploting case study
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
%converts the 2nd plot into 3d plot
xlabel('x values')
ylabel('y values')
zlabel('z values')
shading interp
%control the color shading of surface of surfec by interpolating the
%colormap index
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function main')
Study time = 3.2752.
It shows that algoritham behaves when no bound are present it takes random samples bellow the searching place.
We are using the stopwatch timer ‘tic’ for solving the statistical behaviour of genetic algorithm. Starting ‘for’ loop from 1 to length of num_case variable ‘I’. Calling the genetic algorithm ‘ga’ an inbuilt matlab function. To solve ‘stalagmite’ function. The ga syntax is used for mentioning the upper bound , lower bound. Inut is array to store the optimum values of variables. . ‘Input’ is the array to store optimum values of the variable. The input and fopt area outputs of the genetic algorithm. Saving the time taken by the program in the variable ‘study_time2’. Plotting the case study 2 figure create the 3D surface plot. With the solid edge colour and face colour. Then labelling and x, y axis naming title.
%case study 2
%with Bounded conditions ( upper bound and lower)
tic
for i = 1:num_case;
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
xopt(i)= input(1);
yopt(i)= input(2);
end
study_time2 = toc
figure (2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5)
title('Bounded Inputs')
xlabel('x values')
ylabel('y values')
zlabel('z values')
subplot(2,1,2)
plot(-fopt)
xlabel('Interations')
ylabel('Function minimum')
zlabel('f values')
Study time 2 = 4.1148.
It shows that after including upper bounds and lower bounds, reduces search space & gives optimum outputs.
We are using the stopwatch timer ‘tic’ for solving the statistical behaviour of genetic algorithm. Starting ‘for’ loop from 1 to length of num_case variable ‘I’. Calling the genetic algorithm ‘ga’ an inbuilt matlab function. To solve ‘stalagmite’ function. The ga syntax is used for mentioning the upper bound, lower bound. Input is array to store the optimum values of variables. Providing the value of parameters ‘population size with value of 300. ‘Input’ is the array to store optimum values of the variable. The input and fopt area outputs of the genetic algorithm. Saving the time taken by the program in the variable ‘study_time3’. Plotting the case study 3 figure create the 3D surface plot. With the solid edge colour and face colour. Then labelling and x, y axis naming title.
%case study 3
%with increasing the fenetic algoritham iteration
%Bounded conditions ( upper bound and lower bound)
option = optimoptions('ga')
option = optimoptions(option,'PopulationSize',300);
%creates a set of the options with each option set to its defelt value.
%population size is set to 300
tic
for i = 1:num_case;
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6;0.6],[],[],option);
xopt(i)= input(1);
yopt(i)= input(2);
end
study_time3 = toc
figure (3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5)
title('Bounded Inputs with Population Size')
xlabel('x values')
ylabel('y values')
zlabel('z values')
subplot(2,1,2)
plot(-fopt)
xlabel('Interations')
ylabel('Function minimum')
zlabel('f values')
title('Maximum Iteration')
Study time = 12.4398.
It shows that, after specified that results wants for population size the genetic algorithm search with more deeply to define the search space for increase in population size in genetic algorithm, it takes the 13 seconds to complete. And shows the final results for GA.
Screen shoot while programing Errors and overcomes -
Fig; screen shoot for error while program typing mistakes
Fig; screen shoot while program successfully runs
Conclusion –
The code for stalagmite function by genetic algorithm has been successfully runs.
All the results and statistical behaviour of genetic algorithm has been plotted and outcomes.
The global maxima of stalagmite function has found.
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 11 - Final project
Aim: To Creation of CAD model considering Class A surface, Nominal thickness and Attachment strategy. Objective: To Creation of CAD model considering Class A surface, Nominal thickness and Attachment strategy. Create thickened part using master section for reference Create the mounting features as per design guidelines…
05 Jan 2023 11:12 AM IST
Hood design-Week 2
HOOD DESIGN OF A CAR AIM : To design the hood of a car with the given input Outer panel and its dimensions. Hood Introduction : The hood (North American English) or bonnet (Commonwealth English) is the hinged cove over the engine of motor vehicles. Hoods can open to allow access to the engine compartment…
01 Nov 2022 10:50 AM IST
Week 8 - Challenge 4 - Bumper
BUMPER DESIGN AIM: to make a model from the provided class-A surface.INTRODUCTION:CLASS-A SURFACE: a surface made by the designer which is given as an input to the plastic modeler to work on. It is aesthetic surfaceand the outer most surface.CLASS-B SURFACE: a surface below a certain thickness from the class-A…
18 Sep 2022 08:20 PM IST
Week 10- Assembly Workbench
AIMIn this week’s assignment, you will have to create 2 assemblies according to the contentcovered in the course videos. You will be provided with 2D diagrams of the componentsand you will have to first create the individual part files and then create a completeassembly of those part files in the assembly workbench.…
14 Sep 2022 01:38 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.