Global maxima of a Stalagmite function using Genetic Algorithm
Introduction:
The genetic algorithm is derived from the famous \"Theory of Evolution\" proposed by Sir Charles Darwin which states that organisms evolve over time through changes in their physical and biological traits that are heritable. This helps organisms have better adaptability and produce more offspring of the relative kind. Therefore, in short, a genetic algorithm is the process of solving constrained and unconstrained problems of optimization that is based on natural selection.
Steps Involved in Genetic Algorithm:
- Initialization
- Fitness assignment
- Selection
- Cross-Over
- Mutation
- Termination Criteria
However, the ga code generates the global minima by default execution. To obtain the maxima we introduce a \'-\' sign in the fopt command. This generates the peak for the global maxima.
The general syntax for \'ga\':
x=ga(fnc,n) finds a local unconstrained minimum, \'x\', to the objective function, \'fnc\' and \'n\' stands for the number of variables of the function \'fnc\'
Steps involved in coding the program and related observations:
1) Initially assign the variable arrays and the number of iterations
2) Then create xx and yy array by using the meshgrid command.
3) By using nested for loop, calculate the stalagmite function and store the value in f. The negative gives the global maxima of the study.
4) In Study-1, we calculate the global maxima by taking random samples from the function. Here we use the tic-toc command for calculating the time taken for study-1. Syntax for ga in study-1 is [inputs,fopt(i)]=ga(@stalagmite,2);
7) Study-2 calculates the global maxima by giving boundary limits for the inputs. Syntax for ga in study-2 is [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); where the study is limited from 0 to 1
9) Study-3 calculates the global maxima using population size by which we can find the specific optimum point.
Here, Syntax for ga in study-3 is given by [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
options=optimoptions(\'ga\')
options=optimoptions(options,\'PopulationSize\',170)
The population size given is as 170. The maximum value of stalagmite function is +1.
The Code:
1. Primary Code:
clear all
close all
clc
%Creating x and y array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=100;
%Creating two dimensional x,y co-ordinate system
[xx,yy]=meshgrid(x,y);
%Calculating the stalagmite function
for i=1:length(xx)
for j=1:length(yy)
m(1)=xx(i,j);
m(2)=yy(i,j);
f(i,j)=stalagmite(m);
end
end
%Study-1
tic
%Taking Inputs from the stalagmite function
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
%Obtaining surface plot and removing grid lines
surfc(xx,yy,f)
shading interp
%PLot nomenclature
title(\'Unbounded inputs\')
xlabel(\'x values\')
ylabel(\'y values\')
%Obtaining the marker points for the key-points
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'k\')
subplot(2,1,2)
%Planar plot for the inputs
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
%Study-2-with constraints
%using tic-toc command to determine the time taken for execution
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title(\'Bounded inputs\')
xlabel(\'x values\')
ylabel(\'y values\')
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
%Study-3-population limit
%accepting population limit as the constraint
options=optimoptions(\'ga\')
options=optimoptions(options,\'PopulationSize\',170)
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
f=-f;
study3_time=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\',4,\'markerfacecolor\',\'c\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
max_value=fopt
2. Function Code:
function [F]=stalagmite(m)
x = m(1);
y = m(2);
f1=(sin((5.1*pi*x)+ 0.5))^6;
f2=(sin((5.1*pi*y)+ 0.5))^6;
f3= exp((-4*log(2)*((x-0.0667)^2))/(0.64));
f4= exp((-4*log(2)*((y-0.0667)^2))/(0.64));
F = -(f1*f2*f3*f4);
end
Images of Results Obtained:
1. Unconstrained data:
2. Constrained data:
3. With Population Size:
References:
1. https://www.analyticsvidhya.com/blog/2017/07/introduction-to-genetic-algorithm/
2. https://www.livescience.com/474-controversy-evolution-works.html
3. https://www.mathworks.com/help/gads/ga.html
4. http://www.massey.ac.nz/~wwpapajl/evolution/assign2/NM/darwinth.htm
5. http://web.cecs.pdx.edu/~mperkows/CLASS_479/LECTURES479/EVO01.PDF
Thank You