Week 4 - Genetic Algorithm
Genetic algorithm
INTRODUCTION:
The genetic algorithm is inspired by the process that drives biological evolution. The process of evolution starts with the selection of fittest individuals from a population. Then 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. And this is the main working principle for the algorithm because at each step, the genetic algorithm selects randomly from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population evolves toward an optimal solution.
The main programming is -
clear all
close all
clc
% 1 dimensional array defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% 2 dimensional array using 1 dimensional arrays
[xx yy] = meshgrid(x,y);
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',250)
tic
for n = 1:15 %running the simulation 'n=15' 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
[solution, 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('iterations')
ylabel('final value')
axis([0 15 -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 stalagmite function programme is-
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 programme is run for fifteen times or iteration up to fifteen times and after getting the plot, the global maxima point are
x = 0.0644
y = 0.0644