Challenge

Week 4.1 - Genetic Algorithm

%%%%%%%%%%%%%%%%%%%%%%%%%%%%STALAGMITE FUNCTION

function [f] = stalagmite(input)
x = input(1);
y = input(2);

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

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%GA


clear all
close all

clc

%defining our search space
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);
num_case = 50;
%creating a 2d dimensional mesh
[xx yy] = meshgrid(x,y);

%Evaluting 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(input_vector);
    end 
end

tic
for i = 1:num_case
    [inputs,fopt1(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time = toc
figure(1);
subplot(2,1,1);
hold on
surfc(xx,yy,-f);
shading interp
plot3(xopt,yopt,-fopt1,'marker','o','markersize',5,'markerfacecolor','r');
title("Unbounded limits");
subplot(2,1,2);
plot(-fopt1);
xlabel('Iterations');
ylabel('Function minimun');


tic
for i = 1:num_case
    [inputs,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time2 = toc
figure(2);
subplot(2,1,1);
hold on
surfc(xx,yy,-f);
shading interp
plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r');
title("Bounded limits");
subplot(2,1,2);
plot(-fopt2);
xlabel('Iterations');
ylabel('Function minimun');

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',200);
%options = optimoptions(options,'PlotFcn', @gaplotbestf)
tic
for i = 1:num_case
    [inputs,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time3 = toc
figure(3);
subplot(2,1,1);
hold on
surfc(xx,yy,-f);
shading interp
plot3(xopt,yopt,-fopt3,'marker','o','markersize',5,'markerfacecolor','r');
title("Bounded limits with size population");
subplot(2,1,2);
plot(-fopt3);
xlabel('Iterations');
ylabel('Function minimun');
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.

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.