Week 4.1 - Genetic Algorithm
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% creating a 2D mesh
[xx,yy] = meshgrid(x,y);
% evaluating stalagmite function
for i = 1:length(xx)
for j = 1 :length(yy)
input_value(1) = xx(i,j);
input_value(2) =yy(i,j);
f(i,j) = stalagmite(input_value);
end
end
% study1-unbounded statistical behavior
tic
for i =1;num_cases
[inputs1,fopt(i)] = ga(@stalamite,2);xopt(i) = inputs1(1);yopt(i) =inputs1(2);
study1_time = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x_values')
ylabel('y_values')
zlabel('Function values')
shading interp
plot3(xopt,yopt,-fopt1,'marker','0','markersize',5,'markerfacecolor','r')
title('unbounded statistical behaviour')
subplot(2,1,2)
plot(-fopt1)
xlabel('Iterations')
ylabel('Function maximum')
% study2_statistical behaviour with upper and lower bound
tic
for i = 1:num_cases
[inputs1,fopt1(i)] = ga(@stalagmite,2);
xopt(i) = inputs1(1);
yopt(i) = inputs1(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x_values')
ylabel('y_values')
zlabel('function values')
shading interp
plot3(xopt,yopt,-fopt,'marker','0','markersize',5,'markerfacecolor','r')
title('unbounded statistical behaviour')
subplot(2,1,2)
plot(-fopt1)
xlabel('Iterations')
ylabel('function maximum')
%study2_statistical behaviour with upper and lower bound
tic
for i =1; num_cases
[inputs2,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs2(1);
yopt(i) = inputs2(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x_values')
ylabel('y_values')
zlabel('function values')
shading interp
plot3(xopt,yopt,-fopt2,'marker','0',markersize',5,'markerfacecolor','r')
title('bounded statistical behaviour')
subplot(2,1,2)
plot(-fplot2)
xlabel('Iterations')
ylabel('foundation maximum')
% study3_increasing GA iterations (population size)
options = optimoptions('ga')
options = optimophtions(options,'populationsize',170);
tic
for i =1:num_cases
[inputs3,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs3(1);
yopt(i) = inputs3(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x_values')
ylabel('y_values')
zlabel('function values')
shading interp
plot3(xopt,yopt,-fopt3,'marker','0','markersize','5',markerfacecolor,'r')
title('bounded statistical behaviour with modified population size')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iteration')
ylabel('Function Maximum')
total_time = study1_time +study2_time+study3_time
fprintf('n the results are below;')
fprintf('nn study1; unbounded statistical behaviour')
fprintf('nn the time taken for the study_1 is;%f',inputs1(1),inputs1(2))
fprintf('n the coordinate of x&ynfor the global maxima of the function are;%f,%f',inputs1(1),inputs1(2))
fprintf('nn the global maxima of the function is;%f',-(mean(fopt1)))
fprintf('nn study2; unbounded statistical behaviour')
fprintf('nn the time taken for the study_2 is;%f',inputs2(1),inputs2(2))
fprintf('n the coordinate of x&ynfor the global maxima of the function are;%f,%f',inputs2(1),inputs2(2))
fprintf('nn the global maxima of the function is;%f',-(mean(fopt2)))
fprintf('nn study3; unbounded statistical behaviour')
fprintf('nn the time taken for the study_3 is;%f',inputs3(1),inputs3(2))
fprintf('n the coordinate of x&ynfor the global maxima of the function are;%f,%f',inputs3(1),inputs3(2))
fprintf('nn the global maxima of the function is;%f',-(mean(fopt3)))
stalagmite function
function[f] = stalagmite(input_value)
f1x =(sin(5.1*pi*input_value(1)+0.5)).^6;
f1y = (sin(5.1*pi*input_value(2)+0.5).^6;
f2x = exp(-4*log(2)*(((input_value(1)-0.0667).^2)/(0.64)));
f2y = exp(-4*log(2)*(((input_value(2)-0.0667).^2)/(0.64)));
f =-(f1x*f1y*f2x*f2y);
end