All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Aim: To study about Genetic Algorithm and to calculate the global maxima of stalagmite function using the Genetic Algorithm in MATLAB. Objective: Write code in MATLAB to optimize the stalagmite function and find the global maxima of the function. Clearly explain the concept of genetic algorithm in your own words and…
Palukury Bereshith Jacob Alapan
updated on 06 May 2020
Aim: To study about Genetic Algorithm and to calculate the global maxima of stalagmite function using the Genetic Algorithm in MATLAB.
Objective:
Theory:
Genetic Algorithm:
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
Selection rules select the individuals, called parents, that contribute to the population at the next generation.
Crossover rules combine two parents to form children for the next generation.
Mutation rules apply random changes to individual parents to form children.
Syntax for GA:
The ‘ga’ syntax in Matlab is used to find the minimum of a function using a genetic algorithm. There are several ways to define the ‘ga’ function depending on the problem being solved. The syntax used for this study is shown below.
How the genetic algorithm works:
The algorithm begins by creating a random initial population.
The 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, the algorithm performs the following steps:
Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
Selects members, called parents, based on their expectation.
Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.
Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.
Replaces the current population with the children to form the next generation.
The algorithm stops when one of the stopping criteria is met.
Stalagmite Function:
MATLAB Program:
Stalagmite Function:
function [f] = stalagmite_func(input_vector)
x = input_vector(1);
y = input_vector(2);
term1 = (sin((5.1*pi*x )+ 0.5))^6;
term2 = (sin((5.1*pi*y) + 0.5))^6;
term3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
term4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f = -(term1*term2*term3*term4);
end
Genetic Algorithm for Stalagmite function:
clear all
close all
clc
% Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; % no. of iterations
[xx,yy] = meshgrid(x,y);
% Evaluating 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_func(input_vector);
end
end
tic
% Case 1 - Statistical Behaviour
for k = 1:n
[inputs1 , fopt1(k)] = ga(@stalagmite_func, 2);
xopt1(k) = inputs1(1);
yopt1(k) = inputs1(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour')
figure(2)
plot(-fopt1)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour')
% Case 2 - Statistical Behaviour - with upper and lower bound
for k = 1:n
[inputs2 , fopt2(k)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[1;1]);
xopt2(k) = inputs2(1);
yopt2(k) = inputs2(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour - with upper and lower bound')
figure(4)
plot(-fopt2)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour - with upper and lower bound')
% Case 3 - Statistical Behaviour - with increasing GA iterations
options = optimoptions('ga')
options = optimoptions(options,'populationsize',300)
for k = 1:n
[inputs3 , fopt3(k)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3(k) = inputs3(1);
yopt3(k) = inputs3(2);
end
figure(5)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour - with increasing GA iterations')
figure(6)
plot(-fopt3)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour - with increasing GA iterations')
Steps:
1) Start the program with 'clear all','close all', and 'clc' commands
2) First, we have to write a program for the Stalagmite function. Solving the Genetic algorithm for stalagmite function gives us a minimum value of the function. To find the maximum value of the function, we need to change the sign of function, so that it will give value for the reverse case of minima, i.e. maxima.
3) Now we need to define the search space for finding the values of x & y in the stalagmite function. The search space is chosen to be between 0 to 0.6 with 150 intervals. For this we have used 'linspace()'. Genetic Algorithm can take no. of iterations each time we run the code giving different values each time. To save the time of calculations we have restricted our script to run for 50 iterations only. It doesn't mean function will give maximum value in 50 iterations. We will consider whatever maximum value that came within those 50 iterations. We have restricted our area of search. These no of iterations is given by 'n=50'.
4) As 'linspace()' creates the series of nos, x & y will be the column vector. As input to the stalagmite function, we need to convert the column vector to a row vector. The first step of this conversion is to create the 'meshgrid(x,y)'. Next, we use 'for' loop for conversion.
5) The next step is to obtain the results for 3 cases of the genetic algorithm in Matlab. In the first case, maximum value of the function is found out with no restrictions, no bounds, and no limits. Genetic algorithm is free to choose the population then reproduce and mutate.
6) To get the same value each time we run the program, we need to restrict the program within specific bounds namely upper bound and lower bound. The whole procedure for programming this case the same as that of the previous one, exception being for GA.
7) Even the results from the bounded cases are not the same every time the program terminates. Hence we now need to increase GA iterations. We can raise it to any value, we have chosen it as 300. In this case, syntax for GA is different than the previous case. GA has to give this population size via an option.
8) Results for this case will be just one value of function which does not deviate with no of iterations.
Results:
Study 1: Statistical Behaviour
Study 2: Statistical Behaviour with upper and lower bound
Study 3: Statistical Behaviour with increasing GA iterations
Validation:
Results obtained using the Optimization tool in Matlab. We have validated the results for 3rd case only.
Errors:
No complicated errors occurred while running the MATLAB program.
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...
Powertrain for aircraft in runways
1) Search and list out the total weight of various types of aircraft. Weight is the force generated by the gravitational attraction of the earth on the airplane. Each part of the aircraft has a unique weight and mass, and for some problems, it is important to know the distribution.…
18 Feb 2021 11:35 AM IST
Braking
1) For a defined driving cycle, calculate the energy required for braking. We have to create a drive cycle excel file with random data of velocity concerning that time and plotted them to see the drive cycle Then import that file into Matlab. Then to create an array into the command window by entering code braking_energy…
15 Feb 2021 02:33 PM IST
Duty Cycle
1) Why power electronics circuits are efficient? In practice, which types of losses occur in power electronics circuits? Power Electronics: Power electronics involves the study of electronic circuits intended to control the flow of electrical energy. These circuits handle power flow at levels much higher than the individual…
14 Feb 2021 01:28 PM IST
Induction Motor Characteristics - II
1) Discuss the equivalent circuit network of the induction motor MATLAB model. Equivalent circuit of an Induction motor: In the equivalent circuit, R1 represents the resistance of the stator winding and X1 the stator leakage reactance (flux that does not link with the air gap and rotor). Magnetizing reactance required…
14 Feb 2021 09:26 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.