Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc

x = linspace(0,0.6,150);%x value inputs
y = linspace(0,0.6,150);%y value inputs
num_cases = 50;% number of iterations
f = get_stalagmite;

tic % study time for study 1 count starts
%study-1 statistical behaviour
for i =1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite1,2);% initializes ga with two input variables
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
studty_time1 = toc % study time for study 1 count ends

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp % To visualize smooth curve lines without mesh lines
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbound inputs')
subplot(2,1,2)
plot(fopt)% plot of fopt for every iteration.
xlabel('Iterations')
ylabel('Function Maximum')

tic % study time for study 2 count starts
%study-2 statistical behaviour with upper and lower bounds
num_cases = 100;% number of iterations for study 2
for i =1:num_cases   
    %ga inputs
    %objective function is stalagmite function and number of variables is 2
    %A,B,Aeq and Beq is [], as there is no linear equalities and
    %inequalities constraints according with respect to syntax
    %lower boundary of variables x and y is [0;]
    %upper boundary limits of variables x and y is [1;1]
    [inputs, fopt(i)] = ga(@stalagmite1,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
studty_time2 = toc %study time for study 2 count ends


figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp % To visualize smooth curve lines without mesh lines
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)% plot of fopt for every iteration.
xlabel('Iterations')
ylabel('Function')

tic % study time for study 3 count starts
%study-3 statistical behaviour with increased population size
num_cases = 150;% number of iterations for study 3

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',450);
for i =1:num_cases
    %ga inputs
    %objective function is stalagmite function and number of variables is 2
    %A,B,Aeq and Beq is [], as there is no linear equalities and
    %inequalities constraints according with respect to syntax
    %lower boundary of variables x and y is [0;]
    %upper boundary limits of variables x and y is [1;1]
    %popultion size is defined by using options in the algorithm
    [inputs, fopt(i)] = ga(@stalagmite1,2,[],[],[],[],[0;0],[0.6;0.6],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
studty_time3 = toc % study time for study 3 count ends

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with increased population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')

function [f] = get_stalagmite
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%creating a two dimensional mesh
[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) = stalagmite1(input_vector);
    end
end 

end

function [f] = stalagmite1(input_vector)
x = input_vector(1);
y = input_vector(2);

f_1_i = (sin(5.1*pi*x+0.5))^6;
f_1_j = (sin(5.1*pi*y+0.5))^6;
f_2_i = exp(-4*log(2)*((x-0.0667)^2)/0.64);
f_2_j = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f = -(f_1_i*f_1_j*f_2_i*f_2_j);% negative sign indicates the inverse of stalagmite funcion to find the maximum value
end

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.