You are on page 1of 3

%initial conditions

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
iterations = 50;
[xx,yy] =meshgrid(x,y);

%stalagmite function
for i=1:length(xx)

for j=1:length(yy)
inputvector(1) =xx(i,j);
inputvector(2) =yy(i,j);
f(i,j) = func(inputvector);

end
end
study_time = toc

surfc(xx,yy,f)
shading interp
xlabel('x values')
ylabel('y values')
title('stalagmite function')

tic
%study1
%genetic algorithm
for i=1:iterations
[inputs , fopt(i)] =ga(@func,2);
xopt(i) =inputs(1);
yopt(i) =inputs(2);
end

study_time1 =toc

%plotting
figure (1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','b')
title('Random inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function min')

tic
%study2
%limits of genetic algorithm
for i=1:iterations
[inputs , fopt(i)] = ga(@func,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time2 = toc

%plotting
figure (2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','b')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function min')

%study3
%higher popoulation genetic algorithm
options = optimoptions('ga');
options = optimoptions(options,'populationsize',200)

tic
for i= 1:iterations
[inputs , fopt(i)] = ga(@func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end

study_time3 = toc

%plotting
figure (3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','b')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function min')
function [f] = func(inputvector)
x = inputvector(1);
y = inputvector(2);
function1= (sin(16.02*x + 0.5)).^6;
function2 = (sin(16.02*y + 0.5)).^6;
function3 = exp((-4*log(2)*(x-0.667).^2)/(0.64));
function4 = exp((-4*log(2)*(y-0.667).^2)/(0.64));
F = function1*function2*function3*function4;
f=-F;

end

You might also like