Using Genetic Algorithm to calculate the Global Maxima of Stalagmite Function in MATLAB
AIM:
To find global maxima of stalagmite function
OBJECTIVE:
- To explain the concept of genetic algorithm.
- To find the global maxima of stalagmite function using genetic algorithm.
- To plot graphs for various studies.
THEORY:
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.
This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them. Five phases are considered in a genetic algorithm:
1. 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.
An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.
2. Fitness function: The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
3. Selection: The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
4. Crossover: Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached. The new offspring are then added to the population.
5. Mutation: In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped. Mutation occurs to maintain diversity within the population and prevent premature convergence.
Termination: The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
GOVERNING EQUATIONS:
In our case, the Stalagmite function acts as the fitness function given by,
STALAGMITE FUNCTION CODE:
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
term1 = (sin(5.1*pi*x + 0.5))^6;
term2 = exp(-4*log(2)*(x-0.0667)^2/(0.64));
term3 = (sin(5.1*pi*y + 0.5))^6;
term4 = exp(-4*log(2)*(x-0.0667)^2/(0.64));
f = -1*(term1*term2*term3*term4); % negative sign to invert function and thus find maxima
end
“ga” FUNCTION SYNTAX:
- x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
- x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
- x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
CASE 1: STATISTICAL BEHAVIOUR WITHOUT BOUNDS:
Code:
%% study 1 - statistical behaviour
clear all
close all
clc
% defining our search space and iterations
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 100; % number of iterations
% creating 2D mesh
[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(input_vector);
end
end
tic
for i = 1:num_case % finding maximum value for each iteration
[inputs,fout(i)] = ga(@stalagmite,2); % calling genetic algorthim function
xout(i) = inputs(1);
yout(i) = inputs(2);
end
subplot(2,1,1); % creating subplot to have 2 plots in same figure
hold on
surf(xx,yy,-f); % inverting again to plot actual stalagmite
shading interp; % shading on
plot3(xout,yout,-fout,'marker','o','markersize',5);
xlabel('x-axis');
ylabel('y-axis');
title('unbounded inputs');
subplot(2,1,2);
plot(-fout);
xlabel('iterations');
ylabel('function minimum');
time = toc
Output:
Conclusion:
The optimum value keeps changing and is randomised with no scope of converging. Hence, this approach is wrong
CASE 2: STATISTICAL BEHAVIOUR WITH BOUNDS:
Code:
%% study 2 - with upper and lower bounds
clear all
close all
clc
% defining our search space and iterations
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 100;
% creating 2D mesh
[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(input_vector);
end
end
tic
for i = 1:num_case
[inputs,fout(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); % setting bounds from 0 to 0.6
xout(i) = inputs(1);
yout(i) = inputs(2);
end
subplot(2,1,1);
hold on
surf(xx,yy,-f);
shading interp;
plot3(xout,yout,-fout,'marker','o','markersize',5);
xlabel('x-axis');
ylabel('y-axis');
title('bounded inputs');
subplot(2,1,2);
plot(-fout);
xlabel('iterations');
ylabel('function minimum');
time = toc
Output:
Conclusion:
The optimum value keeps switching between two values but is structured. Hence, this approach is right but can be improved further.
CASE 3: STATISTICAL BEHAVIOUR WITH BOUNDS AND A LARGER POPULATION:
Code:
%% study 3 - increasing population of ga
clear all
close all
clc
% defining our search space and iterations
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 100;
% creating 2D mesh
[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(input_vector);
end
end
tic
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300); % changing population size to 300
for i = 1:num_case
[inputs,fout(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xout(i) = inputs(1);
yout(i) = inputs(2);
end
subplot(2,1,1);
hold on
surf(xx,yy,-1*f);
shading interp;
plot3(xout,yout,-1*fout,'marker','o','markersize',5);
xlabel('x-axis');
ylabel('y-axis');
title('bounded inputs with bigger population');
subplot(2,1,2);
plot(-1*fout);
xlabel('iterations');
ylabel('function maximum');
time = toc
Output:
Conclusion:
The optimum value is almost constant with negligible error. Hence, this approach is right and gives the correct answer. Thus, the maximum value of the stalagmite function under our search space is approximately equal to 1.
CODE EXPLANATION:
1. Clearing workspace and defining our search space and number of iterations.
2. Creating a 2D mesh of search space.
3. Evaluating the stalagmite function for surface plot.
4. For each iteration, using ga function to calculate the maximum value.
5. Plotting the stalagmite function and maximum values.
ERRORS:
Basic syntax errors were encountered which were resolved immediately.
REFERENCES: