Week 4.1 - Genetic Algorithm
Please find the Attached for your review
Kindly let me know if there is any changes required or there is any error
clear all
close all
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases =50; % Number of times it will run the GA case
% creating a 2Dimensional meshgrid
[xx,yy] = meshgrid(x,y);
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
% Note:-The upper loop will run first and then the below loop will run.
% Study numnber 1
% Evaluating the Stalagmite funct ion
tic
for i=1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2); % here 2 represents the variables
xopt(i)= inputs(1);%(here we already have a point for z axis(fopt) thats why we are asking for xopt)
yopt(i)= inputs(2);%(here we already have a point for z axis(fopt) thats why we are asking for yopt)
end
study_time1 = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
title('unbounded inputs')
xlabel('iteration')
ylabel('function Minimum')
% Study numnber 2
% Statistical behaviour with upper and lower bound
tic
for i=1:num_cases
[inputs2, fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); % here 2 represents the variables and check GA Syntax
xopt2(i)= inputs2(1);%(here we already have a point for z axis(fopt) thats why we are asking for xopt)
yopt2(i)= inputs2(2);%(here we already have a point for z axis(fopt) thats why we are asking for yopt)
end
study_time2 = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt2)
title('unbounded inputs')
xlabel('iteration')
ylabel('function Minimum')
% Study numnber 3
% Statistical behaviour with upper and lower bound
% Increasing the number of iterations
% default optimization parameters replaced by values in options(50 to 170)
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize', 170);
tic
for i=1:num_cases
[inputs3, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options); % here 2 represents the variables check GA Syntax
xopt3(i)= inputs3(1);%(here we already have a point for z axis(fopt) thats why we are asking for xopt)
yopt3(i)= inputs3(2);%(here we already have a point for z axis(fopt) thats why we are asking for yopt)
end
study_time3 = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt3)
title('unbounded inputs')
xlabel('iteration')
ylabel('function Minimum')