Week 4.1 - Genetic Algorithm
Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Explain the concept of genetic algorithm 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.
What is genetic algorithm?
The genetic algorithm method is used to generate high-quality solutions for optimization problems and search problems. Genetic algorithms simulate the process of natural selection which means those species who can adapt to changes in their environment are able to survive and reproduce and go to next generation. Genetic algroithm consists of five important process such as
Initial population
Fitness function
Selection
Crossover
Mutation
Initial population :
The process of initial population begins with a set of individuals which is called an Initial Population.
Individuals are characterized by a set of parameters which is known as genes.
Fitness function :
It is used to determine the fitness value for an individuals and comparing the individuals with one another. Depends on the fitness values the individuals will be selected for reproduction process.
Selection :
Selection is the stage of a genetic algorithm in which individual genes are chosen from a population for later breeding using the crossover method.
Crossover :
Crossover is a genetic operator used to combine the genetic information for two parents to generate new offspring.
Mutation :
Mutation is used to maintain the genetic diversity from one generation of a population of genetic algorithm chromosomes to the next. It is analogous to biological mutation.
Mutation alters one or more gene values in a chromosome from its initial state. In mutation, the solution may change entirely from the previous solution.
Flow chart of genetic algorithm :

What is stalagmite function?
A stalagmite function generally refers to finding out the local and global maxima and minima of that particular given function.
Finding the global maxima using matlab coding:
clear all
close all
clc
%Defining the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
num_cases=50
%Calculating 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 1;
tic
for i =1 :num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_1=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour with unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
%Case 2
for k= 1:num_cases
[inputs2,fopt2(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt2(k)=inputs2(1);
yopt2(k)=inputs2(2);
end
study_time_2=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('statistical behaviour with upper and lower bound')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('Function maximum')
%Case 3
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
for m =1 : num_cases
[inputs3,fopt3(m)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3=inputs3(1);
yopt3=inputs3(2);
end
study_time_3=toc
%Plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
grid on
plot3(xopt3,yopt3,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behavior with increased interations')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iterations')
ylabel('Function maximum')
Obtained Plots :
Plot for case 1 :

Plot for case 2 :

Plot for case 3 :

Study_Time_1=4.753
Study_Time_2=10.895
Study_Time_3=12.365
Coding 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
Coding explanation :
Using of clear all, close all , clc commands will clear the entire datas in command window and workspace, and close the all backend running process in matlab.
Linspace command is used to find the search space of genetic alogrithm.
Here, Linspace begins from zero and end with 0.6. Between 0 - 0.6, it will split 150 values equally.
The total number of iterations for genetic algorithm is defined in a name of num_cases.
By using meshgrid command, we could generate 2D arrays with help of x and y values.
Here,For loop command is used to provide an inputs for stalagmite function inputs.
case-1:
Statistical behaviour with unbounded inputs : By using the for loop command we could find the optimum values for each input for iterations.
Each time for loop runs the values will be changing for every run.
Tic and toc command used to calculate the time taken to complete the process.
Plotting :
Figure command is used to opening new figure window.
Subplot command is used to bring more than one plot in single figure.
Plot3 command is used to get the 3D surface for the plot.
case-2 process are as same as case-1 process but the ga syntax is used in secound case.
The process case-3 is simular to case-2 process but instead of using ga we use optimoptions command to increase the population size.
By converting the following procedure into matlab coding, we could get three different plots as a result for the coding.
The obtained plots were mentioned above the heading of coding for stalagmite function.
Application of the Genetic Algorithm :
Automation sector
Robotic sector
To improve the efficiency of AI (Artificial Intelligence)
Conclusion :
In order to get best output, Increasing the population size of the study is important.
Doing simulation multiple times will bring us best optimized values.
References :
https://en.wikipedia.org/wiki/Genetic_algorithm
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm