Week 4 - Genetic Algorithm
OPTIMIZE STALAGMITE FUNCTION AND FIND GLOBAL MAXIMUM OF A FUNCTION
AIM : To find the maximum of a given matrix using stalagmite function and also apply the concept of genetic alogarithm to find the optimum values using the present and future generation population.
GENETIC ALOGARITHM :
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.It is frequently used to solve optimization problems, in research, and in machine learning.
Genetic Algorithm optimisation starts with population initialization.Population is the number of sample data points taken to perform the genetic algorithm operation. These sample data points undergoes the following three steps :
1.selection
2.crossover
3.Mutation
Basically to explain it briefly first from the given population a selection process is done such that a perfect parents were selected and then a crossover is done i.e., taking two parents from the population and use them to produce next generation child till the optimised solution occurs and at finally mutation is a process which helps to change or diverse the parents to get a change in the generation of child.
So this process is continued till an optimised solution is occured that gives the best solution or an perfect next generation child. So to get the optimised solution here we use the stalagmite that performs these operation using different GA syntax.
The following are the different GA syntax of the stalagmite function:
GA SYNTAX EXPANSION :x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exisx = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in 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.)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.STALAGMITE :
The word stalagmite came from the small peaks that form inside the caves of mountains that shows the different forms of stalagmites as the water slips down the rocks it slowly forms a peak and solidifies as salts were accumulated and water is precipitated.So from this the 3D surface of the stalagmite function is drawn.The below image can very well explain the structure of stalagmite.

Based on this concept the maximum and minimum of a given set of values or arrays can be found using genetic alogarithm.
PROGRAM CODE :
x = linspace(0,0.8,120);
y = linspace(0,0.8,120);
%creating the 2D mesh
[X, Y] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(X)
for j = 1:length(Y)
input_vector(1) = X(i,j);
input_vector(2) = Y(i,j);
f(i,j) = stalagmite_calling_function(input_vector);
end
end
surf(X,Y,f)
shading interp
xlabel('x values')
ylabel('y values')
title('GLOBAL MAX AND MIN')
%evaluating the stalagmite function
[inputs,fval] = ga(@stalagmite_calling_function,2)
This is the main code that basically outputs the global maximum point at some X and Y points out of some randomly taken points.
1. First some set of random values is given as input values using linspace command.
2. Then using meshgrid command an xx and yy array values were created.
3. So now by genetic alogarithm theory a function called stalagmite is used to find the maximum of all that X and Y values by taking each and every set i.e., (x,y).
4. Now for that values a 3D surface is created so that a 3D stucture of stalagmites can be viewed by plotting those values.
The function code for the stalagmite is shown below :
function[M] = stalagmite_calling_function(input_vector)
%input values
pi = 3.14;
e = 2.718;
ln(2) = 0.6931;
x = input_vector(1);
y = input_vector(2);
%function for defined values
x1 = (sin(5.1*pi*x+0.5))^6;
y1 = (sin(5.1*pi*y+0.5))^6;
x2 = e^(-4*ln(2)*((x-0.0667)^2/(0.64)));
y2 = e^(-4*ln(2)*((y-0.0667)^2/(0.64)));
M = (x1.*y1.*x2.*y2);
end
In this function code it just takes all the x and y points out of which it gives the max and min points that can be very well calculated by the formula given above.
Now, after looking about the stalagmite its time to know about some other additional things like evolution of the genetic alogarithm where predicting the results though how many times the code is made to run there should be a single output i.e., the syntax is ran using the bounded, unbounded and increase in the population size to result in the optimum next generation values.
The output of the code which was used here is shown in the below image :

So in the output of this code the two values shown in the above image of command window are the maximum value of f which is projected by the help of X and Y data and all those values can be seen in the workspace window and also all the details all can be sited in the workspace window.
Now to work on the additional things like next generation exploration there are three studies like data with ubounded, bounded and bounded with increase in the population which gives uss the perfect optimised results .
PROGRAM CODE :
clear all
close all
clc
%inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
[xx, yy] = meshgrid(x,y);
% 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) = stalagmitefunction_call_test(input_vector);
end
end
%study_1
%statistical behaviour
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmitefunction_call_test,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
studytime_1 = toc;
%plotting with unbounded inputs
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,-f);
shading interp;
ylabel('Y values');
xlabel('X values');
plot3(xopt,yopt,-Fopt,'marker','o','markersize',6,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('no of Iterations')
ylabel('Maximum function value')
%study_2
%Within bounded values
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmitefunction_call_test,2,[],[],[],[],[0;0],[0.3;0.3]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
studytime_2 = toc;
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('X values')
ylabel('Y values')
plot3(xopt,yopt,-Fopt,'marker','o','markersize',6,'markerfacecolor','g');
title('Bounded Inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('no of Iterations')
ylabel('Maximum Function value')
%study_3
%With bounded values and population size increased
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmitefunction_call_test,2,[],[],[],[],[0;0],[0.3;0.3],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
studytime_3 = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X values')
ylabel('Y values')
plot3(xopt,yopt,-Fopt,'marker','o','markersize',6,'markerfacecolor','b');
title('Bounded values and Increased population size');
subplot(2,1,2)
plot(-Fopt)
xlabel('no of Iterations');
ylabel('Maximum Function value');
The starting of the code is about creating the meshgrid as done in the previous code and other than that remaining code is about three studies.
1. Study 1
In here there are 50 cases given as the loop runs over that time.
In this code for loop is used to find the maximum value with an unbounded values using ga syntax where there are many number of maximum values for number of iterations which is a basic syntax method.
2. Study 2
In here we are bounding the upper and lower limit, so by this, code runs with in the limit and the values were arranged better than unbounded but the no of maximum values were reduced but not optimised well.
So to get an optimised maximum values we have to use the another syntax by process mutation that is defined by using particular syntax.
3. Study 3
In this type of study we are going to increase the size of population which comes under mutation process as it changes the order of pairing in between the parents to form the good generation of child, so this type of syntax is used to form the optimum result.
STALAGMITE FUNCTION CODE :
function [stalagmite] = stalagmitefunction_call_test(input_vector)
x = input_vector (1);
y = input_vector (2);
x1 = (sin(5.1*pi*x +0.5))^6;
y1 = (sin(5.1*pi*y +0.5))^6;
x2 = exp((-4*0.6931)*(x-0.0667)^2/(0.64));
y2 = exp((-4*0.6931)*(y-0.0667)^2/(0.64));
stalagmite = -(x1*y1*x2*y2);
end
This code was same as the previous stalagmite function code that obtains the maximum value of the given input.
The output graphs of the three studies were shown in the below images :
STUDY 1 :

STUDY 2:

STUDY 3:

OUTPUT :

These are output of the code 2 which has the time taken for all the three studies and from the study 3 has taken the more time which contains more population size and more study time and more the optimised results.
ERRORS OCCURED :
The error occured when a program was created is, that not enough input arguments in the stalagmite function even though correct program was written and when another program was created the previous error was rectified.

REFERENCES :
The some of knowledge about ga was gained from mathworks genetic alogarithm theory.
reference on genetic alogarithm
CONCLUSION :
Finally the optimised maximum solution is found as getting a single solution as output by options syntax, mutation and changing the size of population.
LINK FOR ABOVE CODES :