Week 4.1 - Genetic Algorithm
DETERMINING THE GOBAL MAXIMA OF THE FUNCTION USING THE CONCEPT OF GENETIC ALGORITHM
AIM
To determine the Gobal maxima of the function using the genetic algorithm.
INTRODUCTION
Stalagmite function is used to generate values over a stalagmite surface to find out the global maxima and minima of a particular function.
Genetic algorithm is an optimization technique used to solve non linear optimization problems and to optimise the global maxima and minima values of the function.
The below mentioned formulas are used to find out the Global maxima and Global minima of a function.

MAIN PROGRAM
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[x,y] = meshgrid(x,y);
num_cases = 50;
f = get_stalagmite_func(x,y);
tic
% Study Statistical behaviour
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_func,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
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 inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
tic
% Case study 2 - Statistical behaviour with upper and lower limits
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.4;0.4])
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
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 inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
% Case study 3 increasing the ga iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',200);
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.2;0.2],[])
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
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 inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
The Command 'f = get_stalagmite_func(x,y);' mentioned in the above program is a stalagmite function used to calculate the global maxima and minima.
Since the values of Global maxima and minima values keeps varying due to the varying inputs and undefined boundary limits Genetic algorithm has to be calculated for the optimisation of the values. The below mentioned command is used for the calculation.
[inputs, fopt(i)] = ga(@stalagmite_func,2);
The above optimization has to be carried out in 3 cases to reduce the variation of the global maxima and minina values. The above program indicates the 2 case study where the values are bounded and in the 3 case study the population size is increased which is represented by the below mentioned command.
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.4;0.4])
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.2;0.2],[])
FUNCTION PROGRAM
function [f] = get_stalagmite_func(x,y)
f1 = (sin(5.1*pi*x + 0.5)).^6;
f2 = (sin(5.1*pi*y + 0.5)).^6;
f3 = exp(-4*log(2)*((x-0.0667).^2/0.64));
f4 = exp(-4*log(2)*((y-0.0667).^2/0.64));
f = -(f1.*f2.*f3.*f4);
end
RESULTS
Case 1: The values are unbounded.

Case 2: The values are bounded.

Case 3: The values are bounded with increased in population.

ERROR

The above error is due to the non mentioning of the number of inputs variables in the stalagmite function.
CORRECTIVE ACTION
The error is corrected by mentioning the number of inputs variables in the stalagmite function.
[inputs, fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.4;0.4])
REFERENCE
https://in.mathworks.com/help/gads/genetic-algorithm.html?s_tid=CRUX_lftnav