Optimizing the stalagmite function using genetic algorithm
Problem Statement:
- Optimise the stalagmite function and find the global maxima of the function such that the code gives same answer for several times of its execution.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations.
Theory:
Genetic Algorithm -
Genetic algorithm is a logic based on theory of evolution which is used for optimizing bounded and unbounded problems to find minimum function value.
It takes into account the natural selection, which is the selection of fittest individuals from the population in race for survival.
This process is carried out in 5 phases,
- Initial population
- The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
2. Fitness function
- The fitness function determines how fit an individual is compared to others. It gives a fitness score to each individual.
3. Selection
- The idea of selection phase is to select the fittest individuals and let them pass their genes (in our case values of variables) to the next generation.
4. Crossover
- Crossover is the most significant phase in a genetic algorithm. For each pair of parents (i.e. previous iteration variables) to be mated, a crossover point is chosen at random from within the genes.
5. Mutation
- In certain new offspring (i.e. values of variables for next iteration) formed, some of their genes can be subjected to a mutation with a low random probability.
References -
https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html
Programming:
Following program is written for solving the given Stalagmite Function using genetic algorithm to find maximum value of function.
Code 1 - Stalagmite Function
%Stalagmite function
function [f] = stalagmite_func(input_vec)
%Function input values (generated in GA program)
x = input_vec(1);
y = input_vec(2);
%Function equations
f1x = (sin(5.1*pi*x + 0.5))^6;
f1y = (sin(5.1*pi*y + 0.5))^6;
f2x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f2y = exp(-4*log(2)*((y-0.0667)^2/0.64));
f = -f1x*f2x*f1y*f2y; % negative sign given as the GA finds solution for minimum value
end
Code 2 - GA for Stalagmite function with 3 different studies
clc
clear all
close all
%Defining space
x = linspace(0,0.5,150);
y = linspace(0,0.5,150);
%Meshgrid
[xx yy] = meshgrid(x,y);
%Finding Value of stalagmite function for the given inputs
for i = 1:length(xx)
for j = 1:length(yy)
input_vec(1) = xx(i,j); %First coloumn of vector
input_vec(2) = yy(i,j); %Second coloumn of vector
f(i,j) = stalagmite_func(input_vec);
end
end
%Iterations for GA
itr =500;
%Study 1: Unbounded Inputs
tic
for i=1:itr
[inputs1, func_val1(i)] = ga(@stalagmite_func, 2); %Solving stalagmite function with 2 variables using GA
xopt1(i) = inputs1(1);
yopt1(i) = inputs1(2);
end
Study1_Time = toc
%Plotting the values for study 1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt1,yopt1,func_val1,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded Inputs\');
xlabel(\'X Value\')
ylabel(\'Y Value\')
subplot(2,1,2)
plot(func_val1)
xlabel(\'Iterations\');
ylabel(\'Optimum Value\');
%Study 2: Bounded Inputs (with Lower and Upper Limits)
tic
for i=1:itr
[inputs2, func_val2(i)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[1;1]); %Solving stalagmite function with 2 variables in given boundry using GA
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
Study2_time = toc
%Plotting the values for study 2
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt2,yopt2,func_val2,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded Inputs\');
xlabel(\'X Value\')
ylabel(\'Y Value\')
subplot(2,1,2)
plot(func_val2)
xlabel(\'Iterations\');
ylabel(\'Optimum Value\');
%Study 3: Initial Population Size
tic
options=optimoptions(\'ga\');
options=optimoptions(options,\'populationsize\',500) %Defining the intial population size
for i=1:itr
[inputs3, func_val3(i)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[1;1],[],[],options); %Solving stalagmite function with 2 variables using GA
xopt3(i) = inputs3(1);
yopt3(i) = inputs3(2);
end
Study3_time =toc
%Plotting the values for study 3
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt3,yopt3,func_val3,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded Inputs\');
xlabel(\'X Value\')
ylabel(\'Y Value\')
subplot(2,1,2)
plot(func_val3)
xlabel(\'Iterations\');
ylabel(\'Optimum Value\');
Error -
1. Error in GA syntax was corrected with the help of MATLAB support page
2. Error occured for writing a code for stalagmite function was solved with the help of Skill Lync support
Output -
1. Study 1 - Unbounded Inputs
Study1_Time =
28.8643
inputs1 =
-0.3456 0.2753
f_val1 =
-0.2547
The optimum value for the function lies within -1 to 0 for any 500 of iterations. Hence the results are still not consistent.
2. Study 2 - Bounded Inputs
Study2_time =
39.2274
inputs2 =
0.0669 0.0668
f_val1 =
-1.0000
The optimum value for the function lies within -1 to -0.4 for any 500 of iterations. The results can be made more consistent.
3. Study 3 - Defined initial population size
Study3_time =
264.7563
inputs3 =
0.0668 0.0668
f_val1 =
-1.0000
The optimum value for the function lies within 0.99999985 to 0.99999975 for any 500 of iterations.
Hence the results are consistent for given problem.
References -
https://in.mathworks.com/help/matlab/ref/subplot.html
https://in.mathworks.com/help/matlab/ref/plot.html?searchHighlight=plot&s_tid=doc_srchtitle