Week 4.1 - Genetic Algorithm
%defining our space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
iterations=50
%creating a 2dimential mesh
[xx, yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)%incrementing i to next value until it stops
for j = 1:length(yy)% incrementing j to next value until it is stops
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite_func(input_vector)
end
end
figure(1)
surfac(xx,yy,f)%surface plot 3d
shading interp % to make plot smoother without lines
xlabel('x valuse')
ylabel('y values')
titile('stalgamite function')
tic%start a stopwatch timer
for i=1:iterations
[input,fop1(i)] = ga(@stalagmite_func,2);
xop(i)= inputs(1);
yop1(i)= inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel(' x values')
ylabel(' y values')
title(' case1-unbounded values using ga ')
plot3(xop1,yop1,-fop1,'marker','o','markersize',3,'markerfacecolor','r')
subplot(2,1,2)
plot(-fop1)
xlabel('iteration')
ylabel('function maximum')
gobal_max1 = -fop1
%case2-lower and upper bounded values using ga
tic
for i = 1:iterations
[inputs,fop2(i)] = ga(@stalagmite_func,2,[],[],[],[],]0;0].[1;1]);
xop2(i)= inputs(1);
yop2(i)= inputs(2);
end
case2_t=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel(' x values')
ylabel('function maximum')
global_max2 = -fop2
%case3- incresed iterations with bonded values using ga
options = optimoptios('ga');
options = optimoptions(options,'populationsize',170);
tic
for i = 1: iterations
[inputs,fop3(i)]=ga(@stalagamite_func,2,[],[],[],[],[0;],[1;1],[],[],options);
xop3(i)= inputs(1);
yop3(i)= inputs(2);
end
case3_t=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xalabel('x values')
ylabel('y values')
titile(' case1-increased iterations with bonded values using ga')
plot3(xop3,yop3,-fop3,'marker','o','markersize',3,'markerfacolor','r')
subplot(2,1,2)
plot(-fop3)
xlabel('iteration')
ylabel('function maximum')
global_max3 = -fop3
%function[f] = stalagmite_func(input_vector)
x = input_vector (1);
y = input_vector(2);
f1=(sin(5.1*3.14*x+0.5))^6;
f2=(sin(5.1*3.14*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 alogoritham:
a genetic alogoriathem makes usus of techniques inspired from evolutionary boology such as selection,mutation,inheritance and recombination to solve a problam . the most commanly employed method in genetic alogarotham is to create a group of induviduals randomly from a given population . the individual thus formed are evaluted with the help of the function provided by the programmer
a genetic algoriathem generates a population of point in each iteration whareas a classical alograthim generats a single point at each iteration
a genetic alograthim selects the next population by computation using random number generator whare a classical alograthim selests the next point by determinstic computation