Week 4.1 - Genetic Algorithm
Writting code in MATLAB for optimising the stalagmite function and findimg global maxima of given function.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
x and y are one dimensional sets or array;
[xx yy] = meshgrid(x,y);
using meshgrid command we will be creating 2 dimensional arrays;
following a loop is created for finding global maxima;
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
after using values of meshgrid command that are [xx and yy] and values of (f) from for loop, we will be plotting plot in order to know or locate global maxima on plot.
surf(xx,yy,f)
xlabel('x value')
ylabel('y value')
shading interp
[inputs, fval] = ga(@stalagmite,2)
but simply using ga command which is inbulit command is not enough, we have to save a function ion directory so that ga command will work efficiently. following code is saved as function named stalagmite and used in above program as to locate global maxima on plot.
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(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
GENETIC ALGORTHIM (GA)
basically, this is taken from the evolution theory stated by charles darwin. Genetic algorthim is a inbuilt function in matlab and used for locating or finding global maximas when between given values. Genteic algortihm works upon follwoing steps:
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation