Week 4.1 - Genetic Algorithm
Aim:
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
Theory:
What is Genetic Algorithm?
Optimization:
Optimization techniques are the techniques used to discover the best solution out of all the possible solutions available under the constraints present. The genetic algorithm is one such optimization algorithm built based on the natural evolutionary process of our nature.
Genetic Algorithm:
The genetic algorithm is based on the genetic structure and behaviour of the chromosome of the population
- Each chromosome indicates a possible solution. Thus, the population is a collection of chromosomes.
- A fitness function characterizes each individual in the population. Therefore, greater fitness better is the solution
Phases of Genetic Algorithm
- Initialization of Population:
- Every gene represents a parameter (variables) in the solution. This collection of parameters that forms the solution is the chromosome. Therefore, the population is a collection of chromosomes.
- Fitness Function
- We have to select the best ones to reproduce offspring out of the available chromosomes, so each chromosome is given a fitness value.
- The fitness score helps to select the individuals who will be used for reproduction
- Selection
- This phase’s main goal is to find the region where getting the best solution is more
- GA tries to move the genotype to higher fitness in the search space
- Reproduction
- Crossover
- Mutation
- Crossover
- Crossover is the most vital stage in the genetic algorithm. During crossover, a random point is selected while mating a pair of parents to generate offsprings
- Single Point Crossover: A point on both parents’ chromosomes is picked randomly
- Two-Point Crossover: Two crossover points are picked randomly from the parent chromosomes.
- Uniform Crossover: In a uniform crossover, typically, each bit is chosen from either parent with equal probability
- Mutation
- Mutation is defined as a small random tweak in the chromosome, to get a new solution. It is used to maintain and introduce diversity in the genetic population and is usually applied with a low probability.
Advantages of GA
- The concept is easy to understand
- GA supports multi-objective optimization
- GA search from a population of points, not a single point
- GA Easily Find Global and Local Maxima.
- GA Solves Difficult Problems.
- Easy Selection of Dataset
Applications:
- Optimization
- Economics
- Neaural Networks
- Image Proccessing
- Machine Learning
Robotics
Optimization Of Stalagmite Function
`f(x,y)=(f1x)*(f2x)*(f1y)*(f2y)`
`f1x=[sin(5.1pi.x+0.5)]^6`
`f1y=[sin(5.1pi.y+0.5)]^6`
`f2x=e(-4.log(2).(((x-0.0667)^2))/((0.64)))`
`f2y= e(-4.log(2).(((y-0.0667)^2))/((0.64)))`
Syntax for ga Code :
% Syntax for ga
x=ga(fun,nvrs)
x=ga(fun,nvrs,A,b)
x=ga(fun,nvrs,A,b,Aeq,beq)
x=ga(fun,nvrs,A,b,Aeq,beq,lb,ub)
x=ga(fun,nvrs,A,b,Aeq,beq,lb,ub)
x=ga(fun,nvrs,A,b,Aeq,beq,lb,ub,nonlcon)
x=ga(fun,nvrs,A,b,Aeq,beq,lb,ub,nonlcon,options)
x=ga(fun,nvrs,A,b,[],[],lb,ub,nonlcon,intclon)
x=ga(fun,nvrs,A,b,[],[],lb,ub,nonlcon,intclon,options)
x=ga(problem)
[x,fval]=ga(_)
[x,fval,output]=ga(_)
Function Code:
function[f]= stalagmite(inputs)
x=inputs(1);
y=inputs(2);
term_1=(sin(((5.1*pi).*x)+0.5)).^6;
term_2=(sin(((5.1*pi).*y)+0.5)).^6;
term_3= exp((((x-0.0667).^2).*(-4*log(2)))./0.64);
term_4= exp((((y-0.0667).^2).*(-4*log(2)))./0.64);
f=-term_1.*term_2.*term_3.*term_4;
end
Main Code :
clear all
close all
clc
% defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
iterations=50;
% creating 2Dimensional mesh
[xx,yy]= meshgrid(x,y);
tic
% evaluating stalagmite function
for i=1:length(xx)
for j=1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
f(i,j)=stalagmite(inputs);
end
end
study_time=toc;
shading interp
% Study-1 unbounded points % study 1
tic
for i=1:iterations
[input,fopt(i)]=ga(@stalagmite,2); % used stalagmite function
xopt(i)=input(1);
yopt(i)=input(2);
end
studytime_1=toc
fopt=-fopt; % getting negative value for find Maxima value
% plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('unbounded points')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Max function')
% Study-2 bounded points % study 2
tic
for i=1:iterations
[inputs,fopt(i)]=ga(@stalagmite, 2, [], [], [], [], [0,0], [1,1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
studytime_2=toc
fopt=-fopt;
% plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('bounded points')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Max function')
%study -3 Bounded points with Highest Population
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',200)
tic
for i=1:iterations
[inputs,fopt(i)]=ga(@stalagmite, 2, [], [], [], [], [0,0], [1,1],[],[],options)
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
studytime_3=toc
fopt=-fopt;
% plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('bounded points with Higher population')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Max function')
Stepwise Explaination of Code:
Step 1: First We have to code the stalagmite Function by using Formulae as Shown in Theory. then we have to start with Main code with 'Clear all','close all' and 'clc'. then create the search space, by using meshgrid we have to plot in 3D. then we have to conferm iterations like 50 iterations.
Step 2 : we have to find out Inputs by using For loop, then we have to calculate the study Time by using tic and toc. then we have to add Shading interp for making Smooth Shade.
Step 3 : After that We can find Study 1 by Using 'ga' ,then we have to find optimum value of Function (fopt). then we have to plot Figure(1), here we can use one subplot for (xopt,yopt,fopt) and another for (fopt). we can get study 1 is for the unbounded points ,here we can't find Maxima value.
Step 4 : Then we can find 'ga' for Study-2 because we have to rectify the stalagmite. then we can plot Figure(2) for bounded points with less population. here we can use one subplot for (xopt,yopt,fopt) and another for (fopt). we can get study 2 is for the bounded points ,but With less Population.
Step 5 : Then we can find 'ga' for Study-3 because we have to rectify the stalagmite. then we can plot Figure(3) for bounded points with Higher Population . here we can use Optimoptions, here we can use one subplot for (xopt,yopt,fopt) and another for (fopt). we can get study 3 is for the bounded points With Higher Population, here we can get Our Maxima Value.
Errors Occurred During Programming:
After Correcting Plot Syntax Program Run Sccessfully

Results :
Output:
inputs =
0.0668 0.0668
fopt =
Columns 1 through 10
-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
Columns 11 through 20
-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
Columns 21 through 30
-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -0.8472 -1.0000 -1.0000 -1.0000 -1.0000
Columns 31 through 40
-0.8472 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
Columns 41 through 50
-1.0000 -1.0000 -1.0000 -1.0000 -0.8472 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
studytime_3 =
9.3253
Study-1: Unbounded Points with scattered points:-

Fig.Unbounded Points with scattered points
In above Fig. we can see that we can not find Maxima value due to Un-bounded scattered Points,also time For Study is 4.0015 sec.
Study-2 : Bounded Points With Less Population

Fig. Bounded Points With Less Population
In above Fig. we can see that we can get the Bounded Points but We can not find Maxima value,also time For Study is 3.9313 sec.
Study-3 : Bounded Points With Higher Population

Fig. Bounded Points With Higher Population
In above Fig. we can see that we can get the Bounded Points We are getting Maxima value(x=0.0668,y=0.0668,z=1) also time For Study is 9.3253 sec.
Conclusion:
In Study -3 case we are geting optimized value of global maxima with accurate range, since we increased number of iterations and population size.
thus we can conclude GA gives more accurate results as compared to Traditional Methods.
Refrences:
https://in.mathworks.com/help/gads/referencelist.html?type=function&s_tid=CRUX_topnav
https://www.sciencedirect.com/topics/engineering/genetic-algorithm