Week 4.1 - Genetic Algorithm
Aim: Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Theroy :
Stalgmite: 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.
GA algorithms: Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.
Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs as shown in the following figure.

Optimization refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.
The set of all possible solutions or values which the inputs can take make up the search space. In this search space, lies a point or a set of points which gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.
Syntex explanation:
example 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 range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
subjects the minimization to the constraints defined in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
Coding% code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
limit = 30;
% creating two dimensional array
[xx, yy] = meshgrid(x,y);
% evaluating a stalgamite 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) = stalgamite_func(input_vector);
end
end
surf(xx,yy,-f)
shading interp
xlabel ('xvalues')
ylabel ('yvalues')
title ('genetic algorithm')
% syntex for ga
[x,fval] = ga(@stalgamite_func,2)
function for stalgmite:- https://in.mathworks.com/help/gads/ga.html
- https://oceanexplorer.noaa.gov/facts/stalactite.html#:~:text=A%20stalagmite%20is%20an%20upward,mineral%20formations%20found%20in%20caves.
- https://en.wikipedia.org/wiki/Genetic_algorithm.
- https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm
results:
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
0.0609 0.0636
fval =
-0.9653