Challenge

Week 4.1 - Genetic Algorithm

clc
clear all
close all
%Stalagmite Function Code (stalagmite_fnc.m)
%Written Based on the Stalagmite Mathematical Model formula
function [stalagmite] = stalagmite_fnc(input_vector)
x= input_vector (1)
y = input_vector (2)
f1_x = (sin(5.1*pi * x +0.5)).^6;
f2_y = (sin(5.1*pi * y +0.5)).^6;
f3_x = exp((-4*0.6931).*(x-0.0667)^2/(0.64));
f4_y = exp((-4*0.6931).*(y-0.0667)^2/(0.64));
stalagmite = (f1_x.*f2_y.*f3_x.*f4_y)
end
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%creating 2dimiensional grid
[xx, yy] = meshgrid(x,y);
% evaluating the stalagmite function
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_fnc(input_vector);
end
end
%study_1 statistical behaviour
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmite_fnc,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_1 = toc;
%plotting
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
ylabel('Y data');
xlabel('X data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','y')
title('Unbounded Inputs')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel('Maximum function')
%study_2 bounding the values
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmite_fnc,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_2 = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X data')
ylabel('Y data')
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded Inputs')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel(' Maximum Function')
%study_3 bounding the values and increasing population size
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmite_fnc,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_3 = toc;
figure(3)
hold on
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('X data')
ylabel('Y data')
plot3(xopt, yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','g');
title('Bounded Inputs with Increased population size');
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations');
ylabel('Maximum Function');
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.