Week 4.1 - Genetic Algorithm
OBJECTIVE : Writing a MATLAB code to optimise the stalagmite function and find the global maxima of the function.
THEORY: The genetic algorithm is inspired by the process that drives biological evolution. The process of evolution starts with the selection of fittest individuals from a population. Then they produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found. And this is the main working principle for the algorithm because at each step, the genetic algorithm selects randomly from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population evolves toward an optimal solution. The genetic algorithm can be applied to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm uses three main types of rules at each step to create the next generation from the current population: Selection rules select the individuals, called parents, that contribute to the population at the next generation. Crossover rules combine two parents to form children for the next generation. Mutation rules apply random changes to individual parents to form children.
INPUT FUNCTION :

STALAGMITE FUNCTION CODE :
- First the function is declared and its input vector is specified.
- Then equations are written with inputs from a mesh grid.
- Then in the last step negative sign is used because we want to maximize the function
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
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
MAIN PROGRAM :
clear all
close all
clc
% defining the our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases=50
% Creating a 2Dimensional mesh
[xx yy] = meshgrid(x, y);
tic
%Evaluating 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
for i=1:num_cases;
[inputs1,fopt1(i)] = ga(@stalagmite, 2);
xopt(i) = inputs1(1);
yopt(i) = inputs1(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
surface(x,y,f)
hold on
xlabel('x value')
ylabel('y value')
shading interp
plot3(xopt,yopt,fopt1,'marker','o','markersize',3,'markerfacecolor','g')
subplot(2,1,2)
plot(fopt1)
xlabel('iterations')
ylabel('Function Minimum')
tic
%Study 2-statistical behaviour
for i =1:num_cases;
[inputs2,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4]);
xopt(i) = inputs2(1);
yopt(i) = inputs2(2);
end
study2_time = toc
%plotting2
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt2,'marker','o','markersize',3,'markerfacecolor','g')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt2)
xlabel('iterations')
ylabel('Function Minimum')
% increasing ga iteration
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
for i =1:num_cases;
[inputs3,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs3(1);
yopt(i) = inputs3(2);
end
study3_time = toc
%plotting3
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt3,'marker','o','markersize',5,'markerfacecolor','g')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt3)
xlabel('iterations')
ylabel('Function Minimum')
Total_Time = study1_time+study2_time+study3_time
fprintf ('The Results Are Below :\n')
fprintf ('Study 1 : Unbounded Statistical Behaviour\n')
fprintf ('The Time taken for the study 1 is : %f \n' ,study1_time)
fprintf ('The Co-ordinates of x and y for the Global Maxima of the Function are :%f,%f \n',inputs1(1),inputs1(2))
fprintf ('The Global Maxima of the Function is : %f \n\n',-(mean(fopt1)))
fprintf ('Study 2 : Bounded Statistical Behaviour \n')
fprintf ('The Time taken for the study 2 is : %f \n',study2_time)
fprintf ('The Co-ordinates of x and y for the Global Maxima of the Function are :%f,%f \n',inputs2(1),inputs2(2))
fprintf ('The Global Maxima of the Function is : %f \n\n',-(mean(fopt2)))
fprintf ('Study 3 : Bounded Statistical Behaviour with Increased Population Size\n')
fprintf ('The Time taken for the study 3 is : %f\n',study3_time)
fprintf ('The Co-ordinates of x and y for the Global Maxima of the Function are :%f,%f\n',inputs3(1),inputs3(2))
fprintf ('The Global Maxima of the Function is : %f\n\n',-(mean(fopt3)))
fprintf ('The Total Time taken to Complete the total Study = %f\n',Total_Time)
RESULTS :

- Unbounded statistical behaviour plot
[inputs,fopt] = ga(fun,nvars)
Inputs represent the values of parameters x and y for which the function has the least value (fopt)
fun - The function to be optimized, in this case Stalagmite nvars the number of design variables in this case only x and y are the variables to be plotted

- Bounded statistical behaviour plot
Then the same study is made but this time it is bounded (has lower and upper limit) which requires the same commands and steps except the ga command which have the following format:
[inputs,fopt] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
Inputs, fopt,fun and nvars are the same as Study 1
A - Linear inequality constraints : real matrix, if there are none, then []
b - Linear inequality constraints : real vector, if there are none, then []
Aeq - Linear equality constraints : real matrix, if there are none, then []
beq - Linear equality constraints : real vector, if there are none, then []
lb - Lower bounds, specified as a real vector or array
ub - Upper bounds, specified as a real vector or array

- Modified population plot - Then increasing population size study was done which is the same as before but it had different ga code of :
[inputs,fopt]=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
inputs, fopt,fun and nvars,A,b,Aeq,beq,lb,ub are the same as Study 1 and Study 2 respectively
nonlcon - Nonlinear constraints, specified as a function handle or function name. If there are none,then []
IntCon - Integer variables, specified as a vector of positive integers, if there are none, then []
Options - Optimization options, specified as the output of optimoptions or a structure.
