Week 4.1 - Genetic Algorithm
Optimization of Mathematical Stalagmite function using Genetic Algorithm
Aim:
To write a code to optimize the stalagmite function and to find out the local maxima with the help of genetic algorithm
Software used:
MATLAB R2022a
Project Description:
Stalagmite:-
A stalagmite is an upward-growing mound of mineral deposits that have precipitated from water dripping onto the floor of a cave. Most stalagmites have rounded or flattened tips.

Now here in our project, we are going to find a mathematical model for this stalagmite function with the help of the genetic algorithm using matlab and also we are going to find out the global maxima or minima. And for that we need to know about the concept of genetic algorithm and that can be explained as follows:-
Genetic Algorithm:-
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions.A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
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. The idea of Natural Selection and Genetic Inheritance is used here. Unlike other algorithms, it uses guided random search, i.e., finding the optimal solution by starting with a random initial cost function and then searching only in the space with the least cost (in the guided direction). Suitable when you are working with huge and complex datasets.
Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
1)Initial polulation:-
The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
2)Fitness Function:-
The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
3)Selection:-
The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
4)Crossover:-
Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
5)Mutation:-
In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.Mutation occurs to maintain diversity within the population and prevent premature convergence.
The following image will helps us to understand the concept of genetic algorithm in a better manner:-

Our given stalagmite function:-
Now with the enough knowledge of stalagmite and the genetic algorithm, we will get inside our program proceedings:-
In this project we are provided with a stalagmite function represented as follows:

Here the usage of ga is to find out the local minima. But in our case, we need to find out the local maxima. And hence what we do is, we just invert the function formula with a negative sign.
And hence for this function of f(x,y), we are going to derive the mathematical stalagmite model as follows:-
Function program and its Explainations:-
Here in the function program named 'stalagmite', contains the function f with 'input_vector' as the function definition and that is stored in two variables x and y.
Then the total function is divided into four terms as f1x, f1y, f2x, f2y and the final function have been arrived by the product of these four terms and is given in the last line of the program.
The negative sign in the function indicates that the function is inverted to find out the local maxima value.
FUNCTION PROGRAM:-
%Function program for stalagmite
function [f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f_1x=((sin(5.1*pi*x+0.5))^6);
f_1y=((sin(5.1*pi*y+0.5))^6);
f_2x=exp(-4*log(2)*((x-0.0667).^2)./0.64);
f_2y=exp(-4*log(2)*((y-0.0667).^2)./0.64);
[f]=-(f_1x*f_1y*f_2x*f_2y);
end
MAIN PROGRAM TO DEFINE THE STALAGMITE FUNCTION WITH THE HELP OF GA & ITS STEP BY STEP EXPLAINATIONS:-
In the above codes, we are defining the values of x and y with the help oh 'linspace' code which creates 150 values between 0 & 0.6.
And the 'meshgrid' code is used to obtain the surface plots as grid for the obtained x and y values.


x = ga(fun,nvars)x = ga(fun,nvars,A,b)x = ga(fun,nvars,A,b,Aeq,beq)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)x = ga(problem)
finds a local unconstrained minimum,x= ga(fun,nvars)x, to the objective function,fun.nvarsis the dimension (number of design variables) offun.finds a local minimumx= ga(fun,nvars,A,b)xtofun, subject to the linear inequalitiesA*x≤b.gaevaluates the matrix productA*xas ifxis transposed (A*x').defines a set of lower and upper bounds on the design variables,x= ga(fun,nvars,A,b,Aeq,beq,lb,ub)x, so that a solution is found in the rangelb≤x≤ub. (SetAeq=[]andbeq=[]if no linear equalities exist.)subjects the minimization to the constraints defined inx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)nonlcon. The functionnonlconacceptsxand returns vectorsCandCeq, representing the nonlinear inequalities and equalities respectively.gaminimizes thefunsuch thatC(x)≤0andCeq(x) = 0. (Setlb=[]andub=[]if no bounds exist.)minimizes with the default optimization parameters replaced by values inx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)options. (Setnonlcon=[]if no nonlinear constraints exist.) Createoptionsusingoptimoptions.orx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon)requires that the variables listed inx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)intcontake integer values.finds the minimum forx= ga(problem)problem, a structure described inproblem.
*******************************************************************************************************************************************



clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
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
num_cases=50;
%study 1- STUDY OF STATISTICAL BEHAVIOUR OF GA
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(1)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('Unbounded Inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('Iterations');
ylabel('Function Minimum');
%study 2- Stastical behaviour of ga with upper and lower bounds
tic
for i=1:num_cases;
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time2=toc
figure(2)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('Unbounded Inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('Iterations');
ylabel('Function Minimum');
%study-3 Increasing the polpulation size
options=optimoptions('ga');
options=optimoptions(options,'populationsize',400);
tic
for i=1:num_cases;
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2)
end
study_time3=toc
figure(3)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('Unbounded Inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('Iterations');
ylabel('Function Minimum');

%Function program for stalagmite
function [f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f_1x=((sin(5.1*pi*x+0.5))^6);
f_1y=((sin(5.1*pi*y+0.5))^6);
f_2x=exp(-4*log(2)*((x-0.0667).^2)./0.64);
f_2y=exp(-4*log(2)*((y-0.0667).^2)./0.64);
[f]=-(f_1x*f_1y*f_2x*f_2y);
end
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
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
num_cases=50;
%study 1- STUDY OF STATISTICAL BEHAVIOUR OF GA
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(1)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('Unbounded Inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('Iterations');
ylabel('Function Minimum');
%study 2- Stastical behaviour of ga with upper and lower bounds
tic
for i=1:num_cases;
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time2=toc
figure(2)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('Unbounded Inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('Iterations');
ylabel('Function Minimum');
%study-3 Increasing the polpulation size
options=optimoptions('ga');
options=optimoptions(options,'populationsize',400);
tic
for i=1:num_cases;
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2)
end
study_time3=toc
figure(3)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('Unbounded Inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('Iterations');
ylabel('Function Minimum');