Week 4 - Genetic Algorithm
AIM; To write a code in Matlab in order to optimize the stalagmite function and find the global maxima of the function by using a genetic algorithm.
THEORY;
A stalagmite is an upward growing mound of mineral deposits that are caused by water dripping onto the floor of a cave. It is a type of rock formation that rises from the floor due to the accumulation of the mineral deposited onto the floor from ceiling drippings. It will be a flattened or rounded shape of the formation rock. Usually, they are formed through the deposition of the calcium carbonite and other minerals that precipitated from mineralized water solution.

GENETIC ALGORITHM;
A genetic algorithm is a search heuristic that was inspired by the Charles darwin theory of natural evolution. It is a method for solving both the constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. In operations research, a genetic algorithm is a metaheuristic inspired by the process of natural selection that belongs to the larger class of relating algorithms. genetic algorithms usually generate high optimization problem solutions by relaying the biologically inspired operators such as mutation, cross-over, and selection.
The process of genetic algorithm goes as follows,
- First, determine the problem and their respective of the goal.
- Make them as a stepwise process of the solution.
- Build a selective population randomly and their appropriate problems.
- Evaluate each process in order to take as a best which chosen the fittest one.
- Now select the breed which was the right genome from each parent.
- And repeat the process up to the optimum one.
Genetic Algorithm in the code;
genetic algorithm is an in-built function in the Matlab, which is always used to find a minimum value and goes a random selection of inputs inorder to find the function value by which natural selection.
syntax of Ga
[x,fval]=ga(@stalagmitefunc,2)
x- shows any previous inputs at which function as a minimum value.
fval-it defines the minimum value.
2- it is a variable of x and y.
First, we create x and y elements by using the linspace command and no_cases to be taken in order to run the genetic algorithm 50 times.
Next, we divide the code into three as each individual study case.
study case 1
In this, we show the statical behavior of the genetic algorithm by taking the no_cases as a 50 and repeat the whole iterations accordingly. evaluate the time toc has to be noted.
The stalagmite shows different peak and minimum values.
study case 2
In this case, we add the upper and lower bounds and run it as usual to the 50 iterations. By repeating this process the stalagmite shows an appropriate value peaks by comparing as a first case.
study case3
In this case, we improve the more population size as 400 and in order to run the more iterations to get a global maxima of a peak value.
FUNCTION CODE;
In this function code we use an formula to derive for the x and y variables
f(x,y)=f1,xf2,xf1,yf2,y
`f1,x=[(sin5.1pix+0.5)]^6`; f1,y=`(sin5.1piy+0.5)]^6`
`f2,x=exp[-4log(2)(x-0.0667)^7/0.64]`
`f2,y=exp[-4log(2)(y-0.0667)^7/0.64]`
By using this formula we defined the stalagmite function.
% stalgmite function
function [stalgmitefunc] = stalgmitefunc(input_vector)
x = input_vector(1);
y = input_vector(2);
% f(x,y)=[f1x,f2x,f1y,f2y]
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)));
stalgmitefunc=-(f1x*f2x*f1y*f2y);
end
MAIN STALAGMITE CODE;
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
no_cases=50;
%creating a 2Dimensional Mesh
[xx yy]=meshgrid(x,y);
%evaluating the stalgmite 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)=stalgmitefunc(input_vector);
end
end
tic
%study case1
for i=1:no_cases
[inputs,fopt(i)]=ga(@stalgmitefunc,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1=toc
figure(1)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
%study case2 with the upper and lower bounds
tic
for i=1:no_cases
[inputs,fopt(i)]=ga(@stalgmitefunc,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2=toc
figure(2)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
%modifiying the Ga iterations
%study case3
options=optimoptions('ga')
options=optimoptions(options,'populationsize',400)
tic
for i=1:no_cases
[inputs,fopt(i)]=ga(@stalgmitefunc,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3=toc
figure(3)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
CASE RESULT 1;

CASE RESULT 2;

CASE RESULT 3;

ERRORS;


We have seen an error which was showing the origin points instead of the showing at the peak points. And I made a mistake in an upper and lower bound case 2, which cross the delimits of the x and y part.
PROCEDURE;
- The stalagmite function is a model that actually generates local maxima and global maxima and in Matlab, we code to show the global maximum point at the peak by using a genetic algorithm.
- First, we set the x and y variables by using the linspace command and create the 2-dimensional arrays by using the mesh grid command to the xx and yy.
- And call the stalagmite function which was derived from the formula of the x and y variables to the main function.
- By using the for loop command we repeat the inputs of xx and yy input_vectors and the surf command is used to create three-dimensional surfaces of the (x,y,-f).
- Now we derive the stalagmite function in three study cases in order to find the global maxima by the genetic algorithm method.
- In these first two study cases, we set the no_cases 50 to the function in order to get proper optimization values.
- Finally by increasing the population size to 400 and run it in order to get the global maxima value of the stalagmite function.
CONCLUSION;
We code the optimum stalagmite function and by using the genetic algorithm we indicate the global maxima point at the peak value.