Challenge

Week 4.1 - Genetic Algorithm

%Stalagmite function
function [f] = stalagmite(input_vector)

x=input_vector(1);
y=input_vector(2);

f1=(sin(5.1*pi*x + 0.5)).^6;
f2= exp(-4log(2)*((x-0.0667).^2./0.64);
f3=(sin(5.1*pi*y + 0.5)).^6;
f4= exp(-4log(2)*((y-0.0667).^2)./0.64);

f=-(f1*f2*f3*f4);

end

%Main code

Clear all
close all
clc

%Input value

lower_bound = input ('Lower bound value: ');
upper_bound= input('Upper bound value: ');

% Input variables
x= linspace(lower_bound,upper_bound,150);
y= linspace(lower_bound,upper_bound,150);

%3D Matrix

[xx, yy] = meshgrid(x,y);

count =50;
tic % Tic Starts the Clock time for the 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(input_vector);
end
end

study_time = toc; % Toc stops the timer and shows the end time
figure(1)
surfc(xx,yy,-f)
shading interp
xlabel('x label')
ylabel('y label')
legend('X Values','Y values')
title('Stalagmite Function')
saveas(1, 'Stalagmite.png')
% [x, fval]= ga(@stalagmite,2)


%Study 1 Unbounded/random sampling Inputs
tic

for i= 1:count
[inputs,fopt(i)]= ga(@stalagmite,2);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end

studytime_1 =toc;

figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Unbounded Inputs')
xlabel('x value')
ylabel('y value')

hold on
plot3(xopt,yopt,-fopt,'marker','o', 'markersize',5,'markerfacecolor','#EDB120');
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
saveas(2,'Unbound Inputs.png')

%Study 2 - Statistical Behaviour With upper and lower Bound sampling
%(Bounded Inputs)

tic
for i = 1:count
[inputs, fopt(i)]= ga(@stalagmite, 2, [],[],[],[], [lower_bound;lower_bound],[upper_bound;upper_bound]);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
studytime_2 = toc;

figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs')
xlabel('x value')
ylabel('y value')

hold on

plot3(xopt,yopt,-fopt, 'marker','o', 'markersize', 5, 'markerfacecolor', 'm')
subplot(2,1,2)
plot(-fopt)
xlabel(' Iterations' )
ylabel('Function Maximum')
saveas(3, 'Bounded Input.png')


%Study 3 - Upper and Lower bound sampling and increase the
%population size
options = optimoptions(@ga);
options = optimoptions(options,'populationsize',170);

tic;
for i=1:count
[inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[lower_bound;lower_bound],[upper_bound;upper_bound],[],[],options);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end

studytime_3 = toc;
figure(4)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs with modified population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,-fopt, 'marker','o','markersize',5,'markerfacecolor','c')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maxima')
saveas(4, 'Bounded Inputs with modified population size.png')
global_maxima = -fopt % Finding the Global Maxima

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.