Challenge

Week 4.1 - Genetic Algorithm

Main function:
function f = stalagmite_fun(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin((5.1*pi).*x+0.5)).^6;
f1y = (sin((5.1*pi).*y+0.5)).^6;


f2x = exp(((((x-0.0667).^2)/(0.64)).*(-4*log(2))));
f2y = exp((((y-0.0667).^2)/(0.64)).*(-4*log(2)));

f = -(f1x*f1y*f2x*f2y);
end

clear all 
close 
clc

x = linspace(0,0.6,200);
y = linspace(0,0.6,200);
num_cases = 50;
[xx,yy] = meshgrid(x,y);
% f = get_stalagmite(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_fun(input_vector);
    end
end

 tic
 % study1-statastical behavior

 for i = 1:num_cases
     [inputs,fopt(i)] = ga(@stalagmite_fun,2);
     xopt(i) = inputs(1);
     yopt(i) = inputs(2);
 end
 study1_time = toc
 figure(1)
 subplot(2,2,1)
surfc(xx,yy,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function minimum')

 tic
% study 2 - statastical behavior with upper and lower bounds 
 
 for i = 1:num_cases
     [inputs,fopt(i)] = ga(@stalagmite_fun,2,[],[],[],[],[0;0],[2;1])
     xopt(i) = inputs(1);
     yopt(i) = inputs(2);
 end
 study2_time = toc
 figure(2)
 subplot(2,2,1)
 % hold on 
 surfc(xx,yy,-f)
 hold on
 shading interp
 plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
 title('bonded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('Iterations')
 ylabel('function minimum')

% study 3 increasing GA iterations
 options = optimoptions('ga');
 options = optimoptions(options,'populationsize',200);
 tic 
 for i = 1:num_cases
     [inputs,fopt(i)] = ga(@stalagmite_fun,2,[],[],[],[],[0,0],[2,1],[],[],options);
     xopt(i) = inputs(1);
     yopt(i) = inputs(2);
 end
 study_3time = toc
 figure(3)
 subplot(2,2,1)
 hold on 
 surfc(xx, yy, -f)
 shading interp
 hold on
 plot3(xopt,yopt,-fopt,'marker','o','markersize',3,'MarkerFaceColor','r')
 title('unbounded inputs')
 subplot(2,1,2)
 plot(-fopt) 
 xlabel('iteration')
 ylabel('function minimum')
 
 
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.