Week 4.1 - Genetic Algorithm
Stalagmite Function :
function [f] = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
t1 = (sin((5.1*pi*x)+0.5))^6;
t2 = (sin((5.1*pi*y)+0.5))^6;
t3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
t4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f = -(t1*t2*t3*t4);
end
Genetic Algorithm Coding :
clear all
close all
clc
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);
num_cases = 50;
[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
% Statistical Behaviour-Study 1- Unbounded Inputs
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time(1) = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% Statistical behaviour with upper and lower bounds
tic
for j = 1:num_cases
[inputs,fopt(j)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(j) = inputs(1);
yopt(j) = inputs(2);
end
study_time(2) = toc;
figure (2)
subplot(2,1,1)
hold on
surfc (xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% Statistical Behaviour with Bounded Inputs with modified polulation size
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
for k = 1:num_cases
[inputs,fopt(k),] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study_time(3)=toc;
figure (3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs with modified polulation size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum ')
% Error Faced
1)In Stalagmite Function in showed the error not enough input, to resolve that i inserted the value of input_vector 1 as x and input_vector 2 as y
2)Didint gave parantheses in stalagmite function
3)In ploting the GA i forgot to put 3 after plot commad thatsy matlab was reading only x and y