How to calculate Global maxima of Stalagmite function
Objective: To write a code in order to find the global maxima of a Stalagmite function.
Optimization:
- Optimization is a study that deals with how to minimize or maximize a functiom which depends on multiple input parameters.
- Stalagmite is a mathematical model that helps to generate local minimas and maximas of a 3D function.
- The main objective of optimization is to determine the point of global maxima without knowing the values.
- This optimization can be done in a very smooth way using Genetic Algorithm.
Genetic Algorithm:
The genetic algorithm is a method for solving both constrained and un-constrained optimization problems that is based on a natural selection, the process that drives a biological evolution.
Explanation for How does a genetic algorith works?
The following outline summarizes how the genetic algorithm works:
- The algorithm begins by creating a random initial population
- The algorithm then creates a sequence set of population. At each the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:
-
Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
-
Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
-
Selects members, called parents, based on their expectation.
-
Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.
-
Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.
-
Replaces the current population with the children to form the next generation.
- The algorithm stops when one of the stopping criteria is met.
- The above steps are the logics that are applied by genetic algorithm for evolution of population by natural selection.
Explanation:
- In order to execute the stalagmite function, first we have to write a code which executes the stalagmite function as below:
-
function [F] = stalagmite_function(input_vector) a = input_vector(1); b = input_vector(2); f1 = [sin(5.1*pi*a+0.5)]^6; f2 = [sin(5.1*pi*b+0.5)]^6; f3 = exp(-4*log(2)*(a-0.667)^2/0.664); f4 = exp(-4*log(2)*(b-0.667)^2/0.664); [F] = -(f1*f2*f3*f4); end
- In the above code as we can see I have given that the output value is \"F\" and the input value as \"input_vector\" and then divided the different inputs of that input_vetor to \'a\' and \'b\'.
- Then I have converted the mathematical functions into code and the final output of that code is assigned to the value of \"F\".
- Then I have written another code and saved it as \"stalagmite_main_code\".
- First I have inputted the basic commads such as clear all,close all and clc
- Then assigned the values of numbers with certain intervals using linspace command.
- In the figure-1 we would be generating the 3D plot of all the data we have assigned using \"surfc\" command.
- Then I have labelled the x-axis as x values, y axis as \'y values\' and the z axis as \'Function Values\' .
- I have given the title of figure-1 as \'Stalagmite Function\'. \'shading interp\' command helps in removing the grid lines that are formed on the surface of the plot.
- In the end of evaluating the stalagmite function we have to call the function and I have provided the output variables as x_outputs and fval.
- Below is the image of the resultant plot of figure-1.
Fig-1: Output of stalagmite function code used for evaluation
- As the \'ga\' command always minimizes the function, we know the that the output we got is the lowest possible one.
- In order to find the global maxima we can do it in three ways and they are as below:
- Case-1: Statistical behaviour without bounding
- Case-2: Statistical behaviour with upper and lower bounds.
- Case-3: Statistical behaviour-Increasing GA iterations.
Case-1:
- First I have provided the number of iterations to do in stalagmite function and then started adding a \'for\' loop.
- In the for loop I have provided the number of times to run as \"1:num_cases\".
- The output of the ga(@stalagmite_function) was assigned as \"inputs and f_optim(i)\" and the outputs of inputs(1) and inputs(2) as we have given the number of inputs as 2 in ga code.
- The output variables of inputs as x_optim(i) and y_optim(i) and ended the \"for loop\".
- Then I have plotted the output as 3D and graph so that I can get the results clearly and given labels to axis and functions as labelled in the below fig-2.
Fig-2: Final out screenshot of Case study-1
- I have made a mistake in my function code in the f3 and f4 equations which caused me the below errors(fig-3,fig-4,fig-5) which could not point my peaks in the right way.
- fig-3: Error of my case study-1
- Fig-4: Error in my case study-2
- Fig-5: Error in my case study-3
Case Study-2:
- This case study is slightly different than case study1 because in this case, we are just going to set the upper and lower limits of the function to carry.
- We can find the difference in plots from fig-2 and fig-6 as below:
- Fig-6: Final output of Case study-2
Case Study-3:
- In this case we are going to findout the increased value of ga iterations.
- The result of the code is as below fig-7.
- Fig-7: Final result plot of my case study-3
Below is the attached code that I have used to plot these peaks and global maxima of the stalagmite function.
clear all
close all
clc
%Providing the inputs 1D arrays
x = linspace(0,0.6,100);
y = linspace(0,0.6,100);
%Assigning the results to 2D arrays using mesh grid command
[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) = stalagmite_function(input_vector);
end
end
figure(1);
surfc(xx,yy,-f);
colorbar
xlabel(\'x value\');
ylabel(\'y value\');
zlabel(\'Function values\');
title(\'stalagmite function\');
shading interp
[x_outputs, fval] = ga(@stalagmite_function,2);
%Case Study-1
%Number of iterations to be done
num_cases = 50;
%CASE STUDY1= Stastical behaviour without bounding
%f = stalagmite_function(input_vector);
tic
for i=1:num_cases
[inputs, f_optim(i)] = ga(@stalagmite_function,2);
x_optim(i)= inputs(1);
y_optim(i)= inputs(2);
end
case1 = toc;
figure(2);
subplot(2,1,1);
surfc(xx,yy,-f);
shading interp
xlabel(\'x values\');
ylabel(\'y values\');
zlabel(\'function values\');
hold on
plot3(x_optim,y_optim,-f_optim,\'-o\',\'MarkerSize\',5,\'MarkerFaceColor\',\'g\');
subplot(2,1,2);
plot(-f_optim);
xlabel(\'Number of Iterations\');
ylabel(\'Function Maximum value\');
%CASE STUDY2 = Statistical Behaviour with upper and lower bounds
tic
for i = 1:num_cases
[inputs,f_optim(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1]);
x_optim(i)= inputs(1);
y_optim(i)= inputs(2);
end
study_time2 = toc;
figure(3)
subplot(2,1,1);
surfc(-xx,-yy,-f);
hold on
shading interp
plot3(-x_optim,-y_optim,-f_optim,\'-o\',\'MarkerSize\',5,\'MarkerFaceColor\',\'b\');
subplot(2,1,2);
plot(-f_optim);
xlabel(\'Iterations\');
ylabel(\'Maximum Functions\');
hold off
%CASE STUDY3- Statistical behavior- Increasing GA iterations
options = optimoptions(\'ga\');
options = optimoptions(options,\'PopulationSize\',400);
tic
for i=1:num_cases
[inputs,f_optim(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1],[],[],options);
x_optim(i)= inputs(1);
y_optim(i)= inputs(2);
end
Study_time3 = toc;
figure(4)
subplot(2,1,1);
surfc(-xx,-yy,-f)
shading interp
hold on
plot3(-x_optim,-y_optim,-f_optim,\'-o\',\'MarkerSize\',5,\'MarkerFaceColor\',\'r\');
title(\'Inputs with increased Iterations\')
subplot(2,1,2);
plot(-f_optim);
xlabel(\'Iterations\');
ylabel(\'Maximum Functions\');
hold off