Week 4.1 - Genetic Algorithm
Aim:
To Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Introduction:
The generic algorithm is the most commonly used method for optimization, it is the decision making process to find the set of cnstraints, the genetic algorithm is based on genetic and natural selection of darwins theory of natural selection.
The main program code:
close all
clear all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy] = meshgrid(x,y);
options = optimoptions('ga');
options = optimoptions(options,'populationsize',25)
tic
for n = 1:50 %n=50 running stimulation 50 times to check consistency
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
[inputs,fval]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
fval_array(n)=fval;
figure(1)
subplot(2,1,1)
hold on
plot(fval_array)
xlabel('iteration')
ylabel('final value')
axis([0 50 -1.1 0])
end
figure(1)
subplot(2,1,2)
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
shading interp
time = toc
The basic commadas such as close all ,clear all and clc is writen first to clear the workspace,graphs and command window respectively.
the inputs are given in linespace command to form the set of matrix and the mesh grid is taken
the number of variables is taken as n=50 to make the stimulation check consistency
an for commands are used to call the function assign with the governing equations and the plots are made.
The stalagmite function program:
function f = stalagmite(input_vector)
x= input_vector(1);linspace(0,0.6,150);
y= input_vector(2);linspace(0,0.6,150);
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);
f = f1x.*f2x.*f1y.*f2y;
end
The stalagmite function programe is derived from the fitness function formulae and thus each term of the inputs are split into various seperate parts of similar to the following formulae.

Results:



The Program is run for 50 times or upto 50 iterations and the plot are obtain for the global maxima are
X = 0.0644
Y = 0.0644
Reference:
1.https://skill-lync.com/knowledgebase/genetic-algorithm-explained
2.https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm
3.Introduction to matlab(NPTEL)