All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
FINDING THE GLOBAL MAXIMA USING GENETIC ALGORITHM AIM: To find the global maxima of the function using Genetic Algorithm. Genetic algorithm is based on the evolution theory proposed by Charles Darwin. Genetic Algorithm provides the…
Harish Kumar
updated on 03 Dec 2020
FINDING THE GLOBAL MAXIMA USING GENETIC ALGORITHM
AIM:
To find the global maxima of the function using Genetic Algorithm. Genetic algorithm is based on the evolution theory proposed by Charles Darwin. Genetic Algorithm provides the random inputs, and it will try to improve the results by creating the offspring from the parent values i.e inputs. Further the results will be improved by mutating the off springs.
CODE:
Main Program:
clear all
close all
clc
%Defining variables
x=linspace(0,1,100);
y=linspace(0,1,100);
num_cases=50;
%Creating a 2D mesh
[xx, yy]=meshgrid(x,y);
%Evaluating the equation using X,Y values
for i=1:length(xx)
for j=1:length(yy)
inputs=[xx(i,j);yy(i,j)];
fout(i,j)=stalagmite(inputs);
end
end
%Genetic Algorithm
%Study 1
rng default
tic;
for k=1:num_cases
[inputs, fopt_study1(k)]=ga(@stalagmite,2);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study1_time=toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study1,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study1)
xlabel('Iterations')
ylabel('Function Minimum')
%Study 2-with upper and lower bounds
rng default
tic
for k=1:num_cases
[inputs, fopt_study2(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1]);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study2)
xlabel('Iterations')
ylabel('Function Minimum')
axis([0 50 -1.2 -0.5])
%Study 3-with population size
rng default
options=optimoptions('ga');
options=optimoptions(options,'populationsize',120);
tic
for k=1:num_cases
[inputs, fopt_study3(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study3,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study3)
xlabel('Iterations')
ylabel('Function Minimum')
axis([0 50 -1.2 0])
Sub-Program:
function [stalfunc]=stalagmite(inputs)
x=inputs(1);
y=inputs(2);
term1=(sin(5*pi*x+0.5)).^6;
term2=(sin(5*pi*y+0.5)).^6;
term3=exp(-4*log(2)*(((x-0.667).^2)/0.64));
term4=exp(-4*log(2)*(((y-0.667).^2)/0.64));
stalfunc=term1.*term2.*term3.*term4;
stalfunc=-stalfunc;
end
CODE EXPLANTION:
Main Program:
Line 1-3:
clear all
close all
clc
Resetting the MATLAB window.
Line 5-8:
%Defining variables
x=linspace(0,1,100);
y=linspace(0,1,100);
num_cases=50;
Defining variables for the stalagmite inputs.
Line 10-11:
%Creating a 2D mesh
[xx, yy]=meshgrid(x,y);
Creating a 2D mesh using the Column Matix and storing them in the variables xx and yy.
Line 14-20:
%Evaluating the equation using X,Y values
for i=1:length(xx)
for j=1:length(yy)
inputs=[xx(i,j);yy(i,j)];
fout(i,j)=stalagmite(inputs);
end
end
Inputs for the stalagmite function will be provided in using the two for loops shown above. Basically we’re passing all the values yy for each and every value of xx and storing the output of the function in variable fout.
Line 22-31:
%Genetic Algorithm
%Study 1
rng default
tic;
for k=1:num_cases
[inputs, fopt_study1(k)]=ga(@stalagmite,2);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study1_time=toc;
These set of commands were used to optimise global maxima of the stalagmite function which we have defined already. Syntax for the genetic algorithm is ga(@function,num_of_variables). Here our function is named as stalagmite function and the number of variables is 2. Once the ga is processed it returns the optimum value of the function and the corresponding x and y values. Here the x and y values will be provided by the Genetic algorithm itself. We have used rng default commands which tells the genetic algorithm to generate the same set of random numbers again and again. If rng default is not included in the program output of this same set of code will give different output. Here the tic and toc command is used to count the time taken for MATLAB to process the set of statements written between the tic and toc key words and the time is stored in the variable study_time1.
Line 33-45:
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study1,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study1)
xlabel('Iterations')
ylabel('Function Minimum')
*Syntax Explanation:
Here we’re placing two plots in same window for that purpose we’re using the subplot command. subplot(m,n,p) this command divides a single plot window into a mxn window. ‘p’refers to the position of the plot in that figure window. Surf command is like the plot command instead of just plotting it will create a surface also. Shading interp will disable the mesh on the surface.
*Programming explanation:
We’re plotting 3 graphs using these lines.
i)Stalagmite function xx,yy and their results.
ii)Value of xopt, yopt, fopt_study1 in the same plot in which the stalagmite function is plotted. xopt, yopt, were the inputs for the stalagmite function provided by the genetic algorithm. Here we’re retrieving them as the output form the GA algorithm line to know the x,y values which the ga provided to the function.
iii)In the second plot we’re plotting the GA output against the number of iterations. To view how the output changes for every iteration.
Line 47-55:
%Study 2-with upper and lower bounds
rng default
tic
for k=1:num_cases
[inputs, fopt_study2(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1]);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study2_time=toc;
Here the stalagmite function which we and used follows the syntax below. ga(@function,numberof variables,A,b,Aeq,beq,lb,ub)
A,b:-
Here A is the coefficient of the inequality equations in matrix form and b is the constrain limit.
For example we have two constrains as shown below
2x+4y<=5
x-3y>=2
First, we need to convert all the equation in such a way that the inequality equations have less than or equal to form.
2x+4y<=5
-x+3y<=-2
Now the A matrix is the coefficients of x and y values and b matrix is the constrain limits.
A= and b=[]. In MATLAB A will be given as [2 4;1 -3] and b as [5 -2].
Aeq,beq:-
It is similar to the A,b as said above but in this set of Linear constrains one inequality and another one will be equalities. Here the equality equation will be considered as the second equations and the Matrix input will be provided as said above.
lb,ub:
Here the lower bounds and upper bounds of the variables will be decided. Input will be provided in the format Lower bounds of x and y and the upper bounds of x and y. In this problem we have defined the lower bounds of the x and y values as 0.5 and the upper bounds of the x and y values as 1 which means the GA algorithms passes the x and y values between 0.5 to 1.
Line 57-69:
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study2)
xlabel('Iterations')
ylabel('Function Minimum')
These set of commands were similar to the lines except that these lines will plot the graph for 2nd study which we have defined the ga algorithm with the lower and upper bounds.
Line 72-82:
%Study 3-with population size
rng default
options=optimoptions('ga');
options=optimoptions(options,'populationsize',120);
tic
for k=1:num_cases
[inputs, fopt_study3(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc;
The empty braces after the upper and lower bounds were to define the nonlinear constrains and the options will be defined for the GA algorithm.
In the options we have defined the population size as 120. Population is the set of the random inputs provided by the ga to the functions. By default, the initial population value will be 50 for the number of variables less than 5. We can be able to define the higher or the lower number of pop-ulations using the options command.
Line 84-96:
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study3,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study3)
xlabel('Iterations')
ylabel('Function Minimum')
These set of statements were used to plot the function and the GA output.
Sub-Program:
function [stalfunc]=stalagmite(inputs)
x=inputs(1);
y=inputs(2);
term1=(sin(5*pi*x+0.5)).^6;
term2=(sin(5*pi*y+0.5)).^6;
term3=exp(-4*log(2)*(((x-0.667).^2)/0.64));
term4=exp(-4*log(2)*(((y-0.667).^2)/0.64));
stalfunc=term1.*term2.*term3.*term4;
stalfunc=-stalfunc;
end
In this function we’re evaluating the four terms and storing the product of the terms in the variable stalfunc. The genetic algorithm is intended to predict the least value of the function. Here the stalfunc is multiplied with with negative sign to invert the stalagmite function. So the peak is now has the least value on the plot so the ga can able to get the minimum value of the functions which is the peak value of the function.
Output:
1.Plot without any bounds and options:
2. Plot with bound of x and y values(0.5<x,y<1):
3.Plot with initial population size of 120:
ERRORS OCCURRED:
Not enough input arguments error. The command window is attached below.
Code popped out the error.
GA can be able to send only one input arguments but in the coding shown above I have provided the 2 input arguments so the GA popped out the error shown above.
REFRENCES:
* https://in.mathworks.com/help/gads/ga.html#d122e38381
* https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.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...
DESIGN OF HOOD USING NX CAD
DESIGN OF HOOD ASSEMBLY AIM: To design the hood inner panel and to fix the striker in path of the trajectory of hinge axis. INTRODUCTION: …
06 Sep 2021 11:20 AM IST
Radar Mast & Final Assembly of Yacht
MODELLING AND ASSEMBLING THE YACHT MODEL INTRODUCTION: Yacht is a sailing vehicle or power vessel commonly used for racing, Sailing, or for pleasure. There are various kinds of yachts available based of their propelling modes and the purpose which they are used. Yachts differ from boats by size and appearances. To be characterised…
30 Aug 2021 05:24 PM IST
MODELLING, ASSEBMLING AND RENDERING AMERICAN CHOPPER USING SOLIDWORKS
DESIGN OF AMERICAN CHOPPER INTRODUCTION: In this project we will be modelling the American Chopper model from the sketch. All the parts were modelled separately using the Solidworks part modelling workbench and they will be assembled in Solidworks assembly workbench. Finally, to make the chopper model realistic some of…
06 Aug 2021 06:14 PM IST
DESIGN OF FRONT DOOR TRIM USING CATIA
DESIGN OF FRONT DOOR TRIM: AIM: To create a door trim solid part along with B side features with CLASS A surface as input. INPUTS: We have skin data for the Lower substrate, Map pocket, Bottle Holder, Arm rest to create solid model. Along with skin data we also have the Master sections of how the parts were joined together…
04 Aug 2021 03:59 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.