genetic algorithm
introduction
this assigment was about finding the maxima of the stalagmite function .
i studies about how the genetic algorithm works and to use the globaloptimisation tool box
references
1)matlab website
2)wikipedia
3)nptel letchure
after studying all this i gather some basic knowledge about how the algorithm works
sucha as
selection ,crossover and mutation
how the genes of the parents gets intermixted and the mutation occurs ,the fittest of a generation is passed to the next generation
initially a group of population is chosen randomly within the limit that is set , then the
intermixing-the genes are represented in the form of bits are mixied and offsprings are produced which occupuy a new set of locations than that of the parents and some of the offsprings undergoes mutation which can be postive or negative or it may no effectts in the fittness of the offsprings thus the of iteration occurs until satisifactory result is produced and
the average of every single iteration is taken and plotted in term of graph
as we can see that from the graph for populaton of 50 and 50 iterations, the population is spread in different peaks but when the population is increased to250 all the point converge at global maxima (minima)
since the value of optimisation always gives the minima i inverted the graph by giving '-'sign to the stalagmite function thus we can find the maxima like this
the deflection in population set as 50 is big ,compared to 250 size population this deflection in the maxima is due to random black boxprocess happening within the ga algorithm
thus higher the plopulation greater the accurasy but more the computation time reqired
for 50 plopulation time was 6.7358sec
for 250 it was 17.97sec
almost 3 time greater than 50 population size
in this code ihave used the option of initial population size and setting the limit within which population must be created
main code
close all
clear all
clc
%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating 2d array
[xx,yy]=meshgrid(x,y);
%stetting options
options=optimoptions('ga');
options=optimoptions(options,'populationsize',50)%intial population size
iteration=50
%input to 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%starting timer
iteration=50
for i=1:iteration
[inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6,0.6],[],[],options);
x_val(i)=inputs(1);
y_val(i)=inputs(2);
end
timetaken=toc%timer end
subplot(2,1,1)%spliting the window
hold on
surfc(xx,yy,f)
shading interp
plot3(x_val,y_val,fval,'marker','o')
xlabel('xvalue')
ylabel('yvalue')
subplot(2,1,2)
plot(fval)
xlabel('iteration')
ylabel('minima')
stalagmite function code
%stalgamite function
function F=stalagmite(input_vector)
f1x=(sin(5.1*pi*input_vector(1)+0.5))^6;
f1y=(sin(5.1*pi*input_vector(2)+0.5))^6;
f2x=exp(-4*log(2)*((input_vector(1)-0.0677)^2/0.64));
f2y=exp(-4*log(2)*((input_vector(2)-0.0667)^2/0.64));
F=-(f1x*f2x*f1y*f2y);
end
50 population size 250poulation size