Genetic Algorithm
Aim
Write a program in Matlab to optimize the stalagmite function and to find the global maxima of the function.
Theory
Optimisation is the area of study which deals with minimizing or maximizing a function.It helps to find the best result of a function within certain constraints.Optimisation is an iterative process which compare the results till best solution is obtained.The iteration follows a certain algorithm to perform the optimization process.Genetic Algorithm(GA) is one of the algorithm used in optimization.
Genetic Algorithm is a technique based on principle of genetics and natural selection.It is based on techniques inspired from evolutionar biology such as selection,crossover ,mutation.The following flowchart depicts the GA:

1.Initialisation
In the initialization process ,the population size is determined.Population size is the number of sample data points taken to perform GA.
2.Selection
From the sample data points the parents are selected.It is the process of selecting highly fit individuals. The fitness value of the given function will be calculated .The selection process is done such that fitter individuals have more probability to be get selected. In this way parents for the crossover are selected.This selection strategy helps to evolve better individuals over time.
3.Crossover
In the process, a child in produced from 2 parents by interchanging the genes.
4.Mutation
It is used to make genetic diversisty from one generation to another by altering the properties of the chromosomes.Mutation is necessary for convergence to take place and find the local maxima or minima.
5.Fitness function
It compares the obtained solution with the existing solutions.Better candidate solution replaces the existing solution .The iteration repeats till optimum solution is found.
Stalagmite Function

%INITIAL PROGRAM
clear all
close all
clc
%defining search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating 2d mesh
[xx yy]=meshgrid(x,y);
%evaluating 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
surfc(xx,yy,f)
shading interp
[inputs,fval]=ga(@stalagmite,2);
%STALAGMITE FUNCTION
function f=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5))^6;
f1_y=(sin(5.1*pi*y+0.5))^6;
f2_x=exp(-4*0.6931*(x-0.0667)^2/0.64);
f2_y=exp(-4*.6931*(y-0.0667)^2/0.64);
f=-(f1_x.*f1_y.*f2_x.*f2_y);
end
Output:


Code Explanation
%Stalagmite function
Create a function stalagmite by passing the input_variable array as the arguments for variables x and y.
function f=stalagmite(input_vector)
Values of input_vector array is stored in x and y
x=input_vector(1);
y=input_vector(2);
definition of function
f1_x=(sin(5.1*pi*x+0.5))^6;
f1_y=(sin(5.1*pi*y+0.5))^6;
f2_x=exp(-4*0.6931*(x-0.0667)^2/0.64);
f2_y=exp(-4*.6931*(y-0.0667)^2/0.64);
Stalagmite function values are calculated and stored on f.
f=-(f1_x.*f1_y.*f2_x.*f2_y);
Negative sign is provided so since the function is to find global maximum.
’ga’ works to minimize a function.
Every time the code is executed we get different results.
Final Program
clear all
close all
clc
%defining search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_case=50;
%creating 2d mesh
[xx,yy]=meshgrid(x,y);
%evaluating 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
num_case=50;
%study_1_statistical_behaviour_unbounded_condition
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time1=toc;
figure(1)
subplot(2,1,1)%to generate multiple plot in single figure window
hold on
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Outputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function_Maximum')
num_case=50
%study_2_statistical_behaviour_bounded_condition
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time2=toc;
figure(1)
subplot(2,1,1)%to generate multiple plot in single figure window
hold on
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded Outputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
num_case=50
%study_3_increasing_ga_iterations
options=optimoptions ('ga');
options=optimoptions (options,'populationsize',500);
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time3=toc;
figure(1)
subplot(2,1,1)%to generate multiple plot in single figure window
hold on
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Increased Population Size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
Outputs :



Code Explanation
The program starts with clear all ,close all and clc
The search space is define by storing 150 values from 0 to 150 in x and y using linspace command.num_case determines no of time ga has to be run.
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_case=50;
Creating a 2D mesh using arrays x and y using meshgrid command
[xx,yy]=meshgrid(x,y);
%evaluating stalagmite function
2 for Loops are used to assign values to input_vector(1) and input_vector(2).Nested loop is used such that values of xx as per sequence of iteration is stored in first column of input_vector and yy in the second column of input_vector.Then the stalagmite function is called wich evaluates the value of function by passing the array input_vector.
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 the study of the statistical behaviour of Genetic Algorithm we introduce a timer.A for loop is defined i from 1 to num_case.The genetic algorithm solver is called by passing function stalagmite with 2 number of variables.Inputs is to store the values of x and y if they are selected and fopt is an array to store the optimum value of the function.Then the value of x slected and y selected in the arrays xopt and yopt respectively for every iteration of for loop.
num_case=50;
%study_1_statistical_behaviour_unbounded_condition
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time1=toc;
To generate multiple plot in single figure window subplot() is used
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Outputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function_Maximum')
For the study of the statistical behaviour of Genetic Algorithm with upper and lower bound, we introduce a timer.A for loop is defined i from 1 to num_case.The genetic algorithm solver is called by passing function stalagmite with 2 number of variables and upper and lower bound.Inputs is to store the values of x and y if they are selected and fopt is an array to store the optimum value of the function.Then the value of x slected and y selected in the arrays xopt and yopt respectively for every iteration of for loop.
num_case=50
%study_2_statistical_behaviour_bounded_condition
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time2=toc;
figure(1)
subplot(2,1,1)%to generate multiple plot in single figure window
hold on
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded Outputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
For the study of the statistical behaviour of Genetic Algorithm by increasing the population size, we introduce a timer.’optimoption’ function is used to return default options to ga solver.Here default option is a parameter named ‘population size, with a value of 500.A for loop is defined i from 1 to num_case.The genetic algorithm solver is called by passing function stalagmite with 2 number of variables.Inputs is to store the values of x and y if they are selected and fopt is an array to store the optimum value of the function.Then the value of x slected and y selected in the arrays xopt and yopt respectively for every iteration of for loop.
num_case=50
%study_3_increasing_ga_iterations
options=optimoptions ('ga');
options=optimoptions (options,'populationsize',500);
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time3=toc;
figure(1)
subplot(2,1,1)%to generate multiple plot in single figure window
hold on
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Increased Population Size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
Syntax of ” ga “
x=ga(func,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
func=is to call the function
nvars=no of variables in the function
A,b=linear inequalities of the function if any otherwise empty[]
Aeq,beq= Equality contraints
lb,ub=to specify the lower and upper bound
nonlcon=function to include the contraints if any
IntCon=to specify the Integer constants
Options=to provide values of default parameters using named parameters if required ,to replace the default parameters.This is done using function optimoptions.