Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
[xx yy] = meshgrid(x, y);

%Study 1 - statistical model
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
% study 1 - satistical behavior
for i = 1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite, 2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Function minimum')

tic

%study 2 - statistical behavior with upper and lower bounds 

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

study2_time = toc

figure(2)
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')
subplot(2,1,2)
plot(fopt)
xlabel('interations')
ylabel('function minimum')


%study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize', 170);

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

study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Function minimum')


function s = stalagmite(vector)
 
x= vector(1);
y= vector(2);
 
f1x = (sin(5.1*pi*x+0.5).^6);
f1y = (sin(5.1*pi*y+0.5).^6);
f2x =exp(-4*log(2)*((x-0.0667).^2));
f2y =exp(-4*log(2)*((y-0.0667).^2));
s =-(f1x.*f1y.*f2x.*f2y);
 
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.