Optimization techiniques using Genetic Algorithm on a stalagmite function using MATLAB
Optimization of Stalagmite Function using Genetic Algorithm method
AIM:
To carry out an optimization analysis for a stalagmite function using Genetic Algorithm
Governing Equations:
Governing equations include the equations for creating a stalagmite function:
Objectives:
- To carry out genetic algorithm method of optimization to find the maximum value of the stalagmite function for the given range of data.
- To show how to improve the accuracy of Genetic algorithm by various studies.
Research about topic:
Genetic Algorithm:
Genetic algorithm is a random population based evelutionary dependent algorithm. It works on the basis of "Darwin's Theory of Evolution". By random population i mean that it takes up random candidates ( i.e datapoints) and then calculates the fitness of the function at that particular point. Then the generation of datapoints are kept as parents to generate children. Children are produced from parents through:
- Crossovers
- Mutations
- Elite candidtes
Once the children are produced the fitness of function is checked for the evolved population to form the next set of children. In this method the algorithm tries to find the lowest or maximum possible value for the required function and determines its position.
To increase the accuracy of our optimization we can:
- control and specify our domain of intrest within the datarange.
- We can increase the population size of the initial randomly generated generation
Syntax for GA :
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
- fun - Function for optimization
- nvars - number of variables in the function
- A,b - to bring the equations into A*x<=b format
- Aeq,beq - to bring equations into A*x == b format
- lb,ub - to specify the upper and lower bounds for the population
- nonlcon - if the function is in nonlinear form then specify this condition
- options - used to change certain default setting of ga algorithm
Program:
STUDY 1: Code for optimization of stalagmite function
% Optimization program code
clear all
close all
clc
%inputs for function
x = linspace(0,1,150);
y = linspace(0,1,150);
% generating matricies from cordinates of the inputs
[a b] = meshgrid(x,y);
% setting limit for iterations
iteration = 50;
% loop to store the value for the stalagmite function
for i = 1:length(a)
for j = 1:length(b)
input(1) = a(i,j);
input(2) = b(i,j);
results(i,j) = stalagmite(input);
end
end
tic
%loop to perform desiered iterations
for i = 1:iteration
% calling the stalagmite function to find the maximum value of stalagmite
% function
[pos val(i)] = ga(@stalagmite,2);
rx(i) = pos(1);
ry(i) = pos(2);
end
time_taken = toc
figure(1)
subplot(2,1,1)
hold on
% plotting the surface plot for the generated stalagmite function
surfc(a,b,results)
% performing a 3-D plot for the generated minimum values
plot3(rx,ry,val,'marker','*','markerfacecolor','r','markersize',5)
xlabel('x value')
ylabel('y value')
shading interp
subplot(2,1,2)
plot(val)
xlabel('Iterations');
ylabel('function value');
The following code calculates the stalagmite function for the values of a matrix generated using the given range of values X and Y. Then the ga algorithm is used to find out the global maxima of the function for the given data points.
In this case each time the program is executed the value of maxima can vary as there is no control over the bounds of population region and the population size itself.
Time take 5.27 sec
Plot:
in the above picture, the plot above shows the stalagmite function and 3-D plot for maxima values generated. The plot below, shows the global maxima value for each iteration.
STUDY 2: Code for optimization of stalagmite function, specifing the upper and lower bounds:
% optimization using genetic algorithm with upper and lower bounds
clear all
close all
clc
%inputs for function
x = linspace(0,1,150);
y = linspace(0,1,150);
% generating matricies from cordinates of the inputs
[a b] = meshgrid(x,y);
% setting limit for iterations
iteration = 50;
% loop to store the value for the stalagmite function
for i = 1:length(a)
for j = 1:length(b)
input(1) = a(i,j);
input(2) = b(i,j);
results(i,j) = stalagmite(input);
end
end
tic
%loop to perform desiered iterations
for i = 1:iteration
% calling the stalagmite function to find the maximum value of stalagmite
% function
[pos val(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5]);
rx(i) = pos(1);
ry(i) = pos(2);
end
time_taken = toc
figure(1)
subplot(2,1,1)
hold on
% plotting the surface plot for the generated stalagmite function
surfc(a,b,results)
% performing a 3-D plot for the generated minimum values
plot3(rx,ry,val,'marker','*','markerfacecolor','r','markersize',5)
xlabel('x value')
ylabel('y value')
shading interp
subplot(2,1,2)
plot(val)
xlabel('Iterations');
ylabel('Global maxima');
In the above code, the upper and lower bounds for random generation of initial population is specified so that to increase the accuracy of the optimization result.
Time take 9.57 sec
Plot
From the plot above it is understood that the accuracy of the optimization has significantly increased by reducing the data space for generation of population.
STUDY 3: Code for optimization of stalagmite function, specifing the upper and lower bounds and increasing initial population size:
% optimization using genetic algorithm with upper, lower bounds and
% increased population size
clear all
close all
clc
%inputs for function
x = linspace(0,1,150);
y = linspace(0,1,150);
% generating matricies from cordinates of the inputs
[a b] = meshgrid(x,y);
% setting limit for iterations
iteration = 50;
% loop to store the value for the stalagmite function
for i = 1:length(a)
for j = 1:length(b)
input(1) = a(i,j);
input(2) = b(i,j);
results(i,j) = stalagmite(input);
end
end
%increasing the population size for the genetic algorithm
options = optimoptions('ga','PopulationSize',200)
tic
%loop to perform desiered iterations
for i = 1:iteration
% calling the stalagmite function to find the maximum value of stalagmite
% function
[pos val(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[.5;.5],[],[],options);
rx(i) = pos(1);
ry(i) = pos(2);
end
time_taken = toc
figure(1)
subplot(2,1,1)
hold on
% plotting the surface plot for the generated stalagmite function
surfc(a,b,results)
% performing a 3-D plot for the generated minimum values
plot3(rx,ry,val,'marker','*','markerfacecolor','r','markersize',5)
xlabel('x value')
ylabel('y value')
shading interp
subplot(2,1,2)
plot(val,'lineWidth',2)
axis([0 50 -1 -0.75])
xlabel('Iterations');
ylabel('Global maxima');
Time take 20.34 sec
In the above code the population size is increased from 50 to 200 to get accurate results.
Plot:
From the above plot it is explicit that increasing the population size has yielded highly accurate optimization results.
Conclusion:
Optimization on the stalagmite function is performed. The accuracy in increasing upper ,lower bounds and population size are studied with results.
References
https://in.mathworks.com/videos/what-is-a-genetic-algorithm-100904.html
https://in.mathworks.com/help/gads/fitness-function-forms.html
https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html
https://in.mathworks.com/help/gads/ga.html#mw_a07ddf7d-face-4243-a656-f1528b592de0
https://towardsdatascience.com/introduction-to-optimization-with-genetic-algorithm-2f5001d9964b