Challenge

Week 4.1 - Genetic Algorithm

clc
clear vars
close all
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx, yy] = meshgrid(x,y);
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

%Reproduceability of Genetic algorithm
%case1: No restrictions in design space
iterations=50;
tic
for i=1:iterations
    [output, fval(i)] = ga(@stalagmite,2);
    x_out(i)=output(1);
    y_out(i)=output(2);
end
study_time1 = toc;
figure(1)
subplot(2,1,1)
hold on
title('No restrictions on design space')
surfc(xx,yy,f)
shading interp
plot3(x_out,y_out,fval,'o','Color','b','MarkerSize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fval,'linewidth',4)
xlabel('No.of iterations');
ylabel('Fmax');
hold off

%case2: Input variables are restricted
iterations_2=50;
tic
for y=1:iterations_2
    [output_2, fval_2(y)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    x_out_2(y)=output_2(1);
    y_out_2(y)=output_2(2);
end
study_time2 = toc;
figure(2)
subplot(2,1,1)
hold on
title('Inputs restricted')
surfc(xx,yy,f)
shading interp
plot3(x_out_2,y_out_2,fval_2,'o','Color','b','MarkerSize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fval_2,'linewidth',4)
xlabel('No.of iterations');
ylabel('Fmax');
hold off

%case3
options = optimoptions('ga','PopulationSize',100)
iterations_3=50;
tic
for y=1:iterations_3
    [output_3, fval_3(y)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    x_out_3(y)=output_3(1);
    y_out_3(y)=output_3(2);
end
study_time3 = toc;
figure(3)
subplot(2,1,1)
hold on
title('More input values')
surfc(xx,yy,f)
shading interp
plot3(x_out_3,y_out_3,fval_3,'o','Color','b','MarkerSize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fval_3,'linewidth',3)
xlabel('No.of iterations');
ylabel('Fmax');
axis([1 50 -1 -0.7])
hold off


%%% Function

function [values] = stalagmite(input)
v1 = (sin(5.1*pi*input(1)+0.5))^6;
v2 = (sin(5.1*pi*input(2)+0.5))^6;
v3 = exp((-4*log(2)*(input(1)-0.0667)^2)/0.64);
v4 = exp((-4*log(2)*(input(2)-0.0667)^2)/0.64);
values = -(v1*v2*v3*v4);
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.