Week 4.1 - Genetic Algorithm
clear all
close all
clc
% Defining our Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating 2-d Array
[xx,yy] = meshgrid(x,y)
% Calculating Fitting Function
for i = 1:length(xx);
for j = 1:length(yy);
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j) = stalagmite_func(input);
end
end
% Converting Fitting Function to change the graph upwards
F = -f;
% Plotting Stalagmite Function
surface(xx,yy,F);
% shading interp;
hold on;
% Performing the Genetic Algorithm
[output , fval] = ga(@stalagmite_func,2,[],[],[],[],[0,0],[0.6,0.6])
% Plotting Global Maxima
x_max = output(:,1);
y_max = output(:,2);
f_max = -fval;
plot3(x_max,y_max,f_max,'marker','o','markersize',5,'markerfacecolor','r');
xlabel ('X-value');
ylabel ('y-value');
title('GLOBAL MAXIMA');
legend('STALAGMITE','GLOBAL MAXIMA')