Week 4.1 - Genetic Algorithm
AIM :
To find the global maxima for the stalagmite function using ga and to plot the results.
MAIN PROGRAM :
% To find the global maxima for the stalagmite function using ga and plot the results
clear all
close all
clc
% Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating 2 Dimensional array mesh
[xx, yy] = meshgrid(x,y);
% Preallocating the variables which are inside the loop
f = zeros(150,150);
fopt1 = zeros(1,50);
fopt2 = zeros(1,50);
fopt3 = zeros(1,50);
xopt1 = zeros(1,50);
xopt2 = zeros(1,50);
xopt3 = zeros(1,50);
yopt1 = zeros(1,50);
yopt2 = zeros(1,50);
yopt3 = zeros(1,50);
% Creating 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
F = -f;
% Number of iterations
num_cases = 50;
% Case study 1, finding the global maxima using ga without the input bounds
tic
for i = 1 : num_cases
[inputs1, fopt1(i)] = ga(@stalagmite, 2);
xopt1(i) = inputs1(1);
yopt1(i) = inputs1(2);
end
Fopt1 = -fopt1;
% Time taken for case study 1 ( in seconds)
study_time1 = toc
% Plotting for case study 1
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,F)
shading interp
xlabel('x value')
ylabel('y value')
zlabel('Function (F)')
plot3(xopt1,yopt1,Fopt1,'marker','o',MarkerSize=5,MarkerFaceColor='r')
title('Unbounded inputs')
subplot(2,1,2)
plot(Fopt1)
xlabel('Iterations')
ylabel('Function - Maximum')
hold off
% Case study 2, finding the global maxima using ga with the bounded inputs.
tic
for i = 1 : num_cases
[inputs2, fopt2(i)] = ga(@stalagmite, 2, [],[],[],[],[0;0],[0.6;0.6]);
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
Fopt2 = -fopt2;
% Time taken for case study 2 ( in seconds)
study_time2 = toc
% Plotting for case study 2
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,F)
shading interp
xlabel('x value')
ylabel('y value')
zlabel('Function (F)')
plot3(xopt2,yopt2,Fopt2,'marker','o',MarkerSize=5,MarkerFaceColor='r')
title('Bounded inputs')
subplot(2,1,2)
plot(Fopt2)
xlabel('Iterations')
ylabel('Function - Maximum')
hold off
% Case study 3, finding the global maxima using ga with the input bounds and modified initial population
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',250);
tic
for i = 1 : num_cases
[inputs3, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt3(i) = inputs3(1);
yopt3(i) = inputs3(2);
end
Fopt3 = -fopt3;
Fmax = mean(Fopt3)
% Time taken for case study 3 ( in seconds)
study_time3 = toc
% Global maxima of the stalagmite funtion
Global_maxima = table(inputs3,Fmax)
% Plotting for case study 3
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,F)
shading interp
xlabel('x value')
ylabel('y value')
zlabel('Function (F)')
plot3(xopt3,yopt3,Fopt3,'marker','o',MarkerSize=5,MarkerFaceColor='r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(Fopt3)
xlabel('Iterations')
ylabel('Function - Maximum')
hold off
FUNCTION PROGRAM :
% To find the stalagmite function
function S = stalagmite(F)
f1 = (sin((5.1*pi*F(1))+0.5))^6;
f2 = (sin((5.1*pi*F(2))+0.5))^6;
f3 = exp(-4*log(2)*( ((F(1)-0.0667)^2) / 0.64 ) );
f4 = exp(-4 * log(2) * ( ((F(2)-0.0667)^2) / 0.64 ) );
S = -(f1*f2*f3*f4);
end
OUTPUT PLOTS :
Case study 1 : Unbounded inputs for ga

Case study 2 : Bounded inputs for ga

Case study 3 : Bounded inputs with modified initial population for ga

Output in command window,

EXPLANATION :
Optimization :
It a method for finding a optimum or near optimum solution by comparing various solutions of the function under a given set of constraints.
Genetic algorithm :
It is algorithm based on the evolutionary theory. It can be used to find a best solution from various solutions of the optimization problem within the given constraints. The steps involved in genetic algorithm are as follows.
- Initialization
- Selection
- Crossover
- Mutation
Initialization :
- Here,the population size is given for the function to initialize the algorithm. Higher the population size, more accurate the solution since it will take a range of values for evaluating the fitness function.
- Fitness function which is also known as the objective function is a mathematical model which takes in the desired inputs to provide the output required for the problem.
Selection :
- Then, the best solutions are selected by using roulette wheel selection method. Here, the roulette wheel is rotated, which has a fixed point and for each time wheel is rotated, the fixed point indicates the solution to be selected. The wheel is made such a way that, the solution with higher fitness value will have greater pie.
Crossover :
- Here, the next generation solution (also referred as child) is formed by interchanging some values from two of the selected solutions (referred to parents) by a single point or double point cross over technique. It is used to converge various solutions to find the best one.
Mutation :
- Sometimes, due to cross overs, the solution may get to stuck within the local minima or maxima. So, inorder to avoid this and to find the global solution, the solutions are made diversed by changing any value in the solution randomly which is known as mutation.
Termination or stopping criteria :
- The 'ga' should have a stopping criteria inorder to end the algorithm otherwise the 'ga' can run number of times until it gets a single value.
- The termination statement should be set, with a fixed number of generations or to stop 'ga' if there is a very small or no change in the obtained solution.
Case study :
Since, the 'ga' chooses random values initially, the solution may vary for each time the program run. So, the 'ga' is made to run multiple times inside the loop so that to check the solution obtained changes or not. Based on it, there are three case studies made
- Case study 1 - Here, the bound input values between which the 'ga' should select the inputs was not provided. So, the 'ga' predicts different solutions when it runs multiple times.
- Case study 2 - Here, the bound limits are set for the input variables ( between 0 to 0.6 ). Now, the solutions are converged and reduced but some are still indicating the local maximas.
- Case study 3 - Here, in addition to the bound limits, the population size is modified and set to 250 which by default will be 50. Therefore global maxima is obtained for number of times the 'ga' runs.
Stalagmite function :
- `f(x,y) = f_1,_x * f_2,_x * f_1,_y * f_2,_y`
Where,
- `f_1,_x = (sin(5.1*pi*x + 0.5))^6`
- `f_1,_y = (sin(5.1*pi*y + 0.5))^6`
- `f_2,_x = exp(-4*ln(2) * ((x - 0.0667)^2 / 0.64))`
- `f_2,_y = exp(-4*ln(2) * ((y - 0.0667)^2 / 0.64))`
Main code - Explanation:
- the '@stalagmite', is the objective function which 'ga' uses to find the best fitness value and it is initiated with a function handle ' @',
- the number of input variables(i.e,'2') for the function which 'ga' changes (initially, the 'ga' chooses these values randomly and since, there is no bound limits given to 'ga', it chooses from a wide range of values).
- Then, the 'ga' calculates and returns two outputs where one is a row vector containing the values of the inputs of which the function is minimum and stores it in the variable 'inputs1' and the other is a column vector of values of the fitness function over 50 iterations of the loop and it is stored in the variable 'fopt1' for each iteration of the loop.
- the '@stalagmite', is the objective function which 'ga' uses to find the best fitness value and it is initiated with a function handle ' @',
- the number of input variables(i.e,'2') for the function which 'ga' changes to find the optimum solution,
- the first and second set of square brackets '[], []' are empty since there is no linear inequalities given as a constrain for the function. If there is any linear inequalities given for the function, then, 'ga' uses this inequality constraints to find the value within the default tolerence value of '1*e-3' ,
- the third and fourth set of square brackets '[], []' are empty since there is no linear equalities given as a constrain to the function. If there is any linear equalities given for the function, then, 'ga' uses this equality constraints to find the value within the default tolerence value of '1*e-3' ,
- [0;0],[0.6;0.6] ' are the lower and upper bounds for the stalagmite function. Here, 'ga' chooses the input values between these bounded values and finds the global minimum of the function but here it is the global maximum of the function since the function is given a negative sign ( ' -ve ' ).
- Then, the 'ga' calculates and returns two outputs where one is a row vector containing the values of the inputs of which the function is minimum and stores it in the variable 'inputs2' and the other is a column vector of values of the fitness function over 50 iterations of the loop and it is stored in the variable 'fopt2' for each iteration of the loop.
- the '@stalagmite', is the objective function which 'ga' uses to find the best fitness value and it is initiated with a function handle ' @',
- the number of input variables(i.e,'2') for the function which 'ga' changes to find the optimum solution,
- the first and second set of square brackets '[], []' are empty since there is no linear inequalities given as a constrain for the function. If there is any linear inequalities given for the function, then, 'ga' uses this inequality constraints to find the value within the default tolerance value of '1*e-3' ,
- the third and fourth set of square brackets '[], []' are empty since there is no linear equalities given as a constrain to the function. If there is any linear equalities given for the function, then, 'ga' uses this equality constraints to find the value within the default tolerance value of '1*e-3' ,
- [0;0],[0.6;0.6] ' are the lower and upper bounds for the stalagmite function. Here, 'ga' chooses the input values between these bounded values and finds the global minimum of the function but here it is the global maximum of the function since the function is given a negative sign ( ' -ve ' ),
- the next square brackets '[]' is set as empty since there is no non-linear equality and inequality constraints for the function and if it has any, then, 'ga' uses these non-linear constraints to find the minimum value of the function and the value of the non-linear inequality and equality will be satisfied within the default tolerance value of '1*e-3'.
- the last square bracket '[]' is set as empty since there is no specific variables which is given to take only integer values. If this integer constraint is to be set, then, 'ga' accepts only non-linear inequality contraints and not the non-linear equality constraints,
- options' is set to have a modified parameter values for 'ga'.
- Then, the 'ga' calculates and returns two outputs where one is a row vector containing the values of the inputs of which the function is minimum and stores it in the variable 'inputs2' and the other is a column vector of values of the fitness function over 50 iterations of the loop and it is stored in the variable 'fopt2' for each iteration of the loop.
- The global maxima of the stalagmite function found by using 'ga' is 0.99999 approximately equal to 1 of which the inputs are [ 0.0668 0.0668 ] .
- Inside the 'plot3' command, I have used the 'MarkerSize'=5 and 'MarkerFaceColor'='r' between the quotation marks which gave me a error when I ran the program.
- So, I have to delete the quotation marks for MarkerSize=5 and MarkerFaceColor='r' to run the program without error.
