All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
OBJECTIVE :- To write a MATLAB code on Genetic Algorithm. PROBLEM STATEMENT :- Try to understand the concept of GA by reffering to various sources before you attend this challenge. Look out for information regarding stalagmite function and ways to optimize it. Write a code in MATLAB to optimize the stalagmite function…
Gokul Krishnan R S
updated on 18 Aug 2022
OBJECTIVE :- To write a MATLAB code on Genetic Algorithm.
PROBLEM STATEMENT :-
Try to understand the concept of GA by reffering to various sources before you attend this challenge. Look out for information regarding stalagmite function and ways to optimize it.
Content expected in the report-
THEORY & INTRODUCTIONS :-
The genetic algorithm is inspired by the process that drives biological evaluation. The process of evaluation starts with the selection of fittest individuals from a population. Then they produce offspring which inherrit the charecterastics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found. And this is the main working principle for the algorithm because at each step, the genetic algorithm selects randomly from the current populations tobe parents and uses them to produce the children forthe next geeratio.Over successive generations, the population evolves towards an optimal solution. The genetic algorithm can be applied to solve a variety of optimization problems that are not well suited for standard optiization algorith, including problems i which the objective function is discontinuous, nondifferentiable, stachastic or highly nonlinear.
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
Algorithm Working Sequence :-
Algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, it uses the fitness function which is the function you want to optimize.For standard optimization algorithms, this is known as the objective function.The toolbox software tries to find the minimum of the fitness function.
The algorithm performs the following steps-
Input Function :-
The above equations are used in the stalagmite function to plot the graph in the main functions-
function [f] = stalagmite(input_value)
f1x = (sin(5.1*pi*input_value(1) + 0.5)).^6;
f1y = (sin(5.1*pi*input_value(2) + 0.5)).^6;
f2x = exp(-4*log(2)*(((input_value(1) - 0.0667).^2)/(0.64)));
f2y = exp(-4*log(2)*(((input_value(2) - 0.0667).^2)/(0.64)));
f = -(f1x*f1y*f2x*f2y);
end
While using the function at the end as we are using the -ve sign so that the plot will be reversed as we have to find the maxima of the given function using ga.
Main Body :-
MATLAB CODE :-
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% creating a 2D mesh
[xx,yy] = meshgrid(x,y);
%Evaluating Stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_value(1)=xx(i,j);
input_value(2)=yy(i,j);
f(i,j) = stalagmite(input_value);
end
end
% Study1- Unbounded statistical behaviour
tic
for i = 1:num_cases
[inputs1, fopt1(i)] = ga(@stalagmite, 2);
xopt(i) = inputs1(1);
yopt(i) = inputs1(2);
end
study1_time = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function Values')
shading interp
plot3(xopt,yopt,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Statistical Behaviour')
subplot(2,1,2)
plot(-fopt1)
xlabel('Iterations')
ylabel('Function maximum')
% Study2 - Statistical behaviour with upper aned lower bound
tic
for i = 1:num_cases
[inputs2,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs2(1);
yopt(i) = inputs2(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function Values')
shading interp
plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Statistical Behaviour')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('Function Maximum')
%Study3- increasing GA iterations (Population size)
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);
tic
for i = 1:num_cases
[inputs3, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs3(1);
yopt(i) = inputs3(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('X-Values')
ylabel('Y-Values')
zlabel('Function Values')
shading interp
plot3(xopt,yopt,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Statistical behaviour with modified population size')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iterations')
ylabel('Function maximum')
Total_time = study1_time + study2_time + study3_time
fprintf('n The results are below : ')
fprintf('nn study 1 : Unbounded statistical Bahaviour')
fprintf('nn The time taken for the study-1 is : %f',study1_time)
fprintf('n The co-ordinates of x & y for the global Maxima of the function are : %f,%f',inputs1(1),inputs1(2))
fprintf('nn the global maxima of the function is : %f',-(mean(fopt1)))
fprintf('nn study 2 : Unbounded statistical Bahaviour')
fprintf('nn The time taken for the study-2 is : %f',study2_time)
fprintf('n The co-ordinates of x & y for the global Maxima of the function are : %f,%f',inputs2(1),inputs2(2))
fprintf('nn the global maxima of the function is : %f',-(mean(fopt2)))
fprintf('nn study 3 : Unbounded statistical Bahaviour')
fprintf('nn The time taken for the study-3 is : %f',study3_time)
fprintf('n The co-ordinates of x & y for the global Maxima of the function are : %f,%f',inputs3(1),inputs3(2))
fprintf('nn the global maxima of the function is : %f',-(mean(fopt3)))
STEP BY STEP CODING PROCEDURE :-
[inputs, fopt] = ga(fun, nvars)
inputs represent the values of parameters x & y for which the function has the least value (fopt)
fun - the function tobe optimized, in this case stalagmite
nvars - the number of design variables, in this case only x & y are the variables tobe plotted
[input, fopt] = ga(fun, nvars, A, b, Aeq, beq, lb, ub)
Inputs, fopt, fun & nvars are the same as study-1
A - Linear inequality constraints : real matrix, if there are none, then []
b - Linear inequality constraints : real vector, if there are none, then []
Aeq - Linear equality constraints - :- real matrix, if there are none, then []
beq - Linear equality constraints - real vector, if there are none, then []
lb - lower bounds, specified as a real vector or array
ub - upper bounds, specified as a real vector or array
[inputs, fopt] = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, IntCon, options)
inputs, fopt, fun and nvars, A, b, Aeq, beq, lb, ub are the same as study-1 and study -2 respectivelly
nonlcon - nonlinear constraints, specified as a function handle or function name. If there are none then []
IntCon - Integer variables, specified as a vector of positive integers, if there are none, then []
Options - Optimization options, specified as the output of optimoptions or a structure.
OUTPUT :-
Unbounded Statistical Behaviour Plot :- (Iterations = 50)
In this above image it can be seen that since the inputs are boundless GA algorithm has mapped local maxima points outside our stalagmite function. Also we can not infer a particular value of global maxima from the above graph, and it is hence pointless.
The study time is very less as the 'ga' algorithm is made only to run for 50 iterations.
Bounded Statistical Behaviour Plot :- (Iterations = 100)
The above graph is more clear and optimized compare to the first iterations since here the bounds were defined and we could get only the values within our range. We are now more clear on the range within which our maxima lies.
Modified Population Size Plot :- (Iterations = 170)
In this case we are getting the optimized value of global maxima with accurate range, since we increased the number of iterations and population size.
Since the number of iterations and population size is increased the study time is so high compared to the previous two iterations.
Thus it can be concluded that GA gives more accurate results in case of bounded inputs and by increasing the number of iteration and population size.
It can also be infered that even if we define a particular search space for GA, the GA doesn't run inside the search space. Only in casses of bounded inputs we can get interfearable results.
Stalagmite Function Output print in Command Window :-
CONCLUIONS :-
The time taken for the study-1 = 5.912489 sec
The global maxima of the function = 0.512086
Time taken for the study-2 = 7. 937613 sec
The global maxima of the function = 0.905461
Time taken for the study-3 = 19.544688 sec
The global maxima of the function = 0.993887
The total time taken to complete the total study = 33.305790
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...
Design of backdoor
Aim : To develop the car Backdoor(Tail gate) with the reference of given skin data as per engineering standarad by using NX12 Back Door : The Back door is typically hinged, but sometimes attached by other mechanisms…
22 Oct 2022 06:40 AM IST
Roof Design
INTRODUCTION The roof of a car or other vehicle is the top part of it, which protects passengers or goods from the weather. The roof also protects the passenger from any injury when the car gets crashed or is rolled over or something heavy falls on the roof. OBJECTIVE: …
17 Oct 2022 06:53 AM IST
Fender Design
Objective To design various mounting points on the fender, according to the design parameters. Fender Fender is the US English term for part of an automobile (vehicle body of car) that frames the wheel well (the fender underside). A fender is the part of an automobile which may be a car, motorcycle or any other…
06 Oct 2022 02:51 PM IST
Fender Design - Wheel Arch Challenge
Aim: To calculate the Fender Wheel Arc area , to pass the European standards. Objective: Create the intersection point according to given degree angle. Follow the European standards…
01 Oct 2022 05:17 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.