Genetic Algorithm
Aim - Use MATLAB to optimise the stalagmite function and find the global maxima of the function.
%Global maxima of the given function.
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[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)=stalgmite(input_vector);
end
end
%case1
iterations=100;
tic
for i=1:iterations
[inputa fval1(i)]=ga(@stalgmite,2);
x1(i)=inputa(1);
y1(i)=inputa(2);
end
time=toc
%plots
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x1,y1,fval1,'marker','*')
title("Case 1 (Unbounded Values)")
subplot(2,1,2)
plot(fval1)
xlabel('No. of Iterations')
ylabel('Function Minimum')
%case 2
tic
for i=1:iterations
[inputb fval2(i)]=ga(@stalgmite,2,[],[],[],[],[0;0],[1;1]);
x2(i)=inputb(1);
y2(i)=inputb(2);
end
time2=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x2,y2,fval2,'marker','*')
title("Case 2 (Bounded values)")
subplot(2,1,2)
plot(fval2)
xlabel('No. of Iterations')
ylabel('Function Minimum')
%case 3
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500)
tic
for i=1:iterations
[inputc fval3(i)]=ga(@stalgmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
x3(i)=inputc(1);
y3(i)=inputc(2);
end
time3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x3,y3,fval3,'marker','*')
title("Case 3 (Increased Population)")
subplot(2,1,2)
plot(fval3)
xlabel('No. of Iterations')
ylabel('Function Minimum')