Week 4.1 - Genetic Algorithm
[10:55 pm, 02/08/2022] Vinay Chaudhari: clear all
close all
clc
%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2dimensional mesh
[xx,yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j) = stalagmite1(input);
end
end
surf(xx,yy,f)
xlabel('xaxis')
ylabel('yaxis')
shading interp
[input,fval] = ga(@stalagmite1,2);
[10:55 pm, 02/08/2022] Vinay Chaudhari: clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j) = stalagmite1(input);
end
end
%case 1 = Unbounded input
for i = 1:50
[input,fopt(i)] = ga(@stalagmite1,2);
xopt(i)= input(1);
yopt(i)= input(2);
end
figure(1)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','.','markersize',10)
title('unbounded input')
subplot(2,1,2)
xlabel('Iteration')
ylabel('function maximum')
%case 2 = Bounded input
for i = 1:50
[input,fopt(i)]=ga(@stalagmite1,2,[],[],[],[],[0;0],[0.4;0.4]);
xopt(i) = input(1);
yopt(i) = input(2);
end
figure(2)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','.','markersize',10)
title('Bounded input')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('function maximum')
%case 3 = population size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',180);
for i = 1:50
[input,fopt(i)] = ga(@stalagmite1,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
xopt(i) = input(1);
yopt(i) = input(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','.','markersize',10)
title('population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('function maximum')
[10:55 pm, 02/08/2022] Vinay Chaudhari: function [f] = stalagmite1(input)
x = input(1);
y = input(2);
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*f1y*f2x*f2y);
end