Challenge

Week 4.1 - Genetic Algorithm

function f = stalagmite(input)
f1x = (sin(5.1*pi*input(1)+0.5))^6;
f1y = (sin(5.1*3.14*input(2)+0.5))^6;
f2x = exp((-4*log(2))*(input(1)-0.0667)^2/0.64);
f2y = exp((-4*log(2))*(input(2)-0.0667)^2/0.64);

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

end

clear all
close all
clc
% defing search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating  a 2d array
[xx,yy]=meshgrid(x,y);
for i=1:length(xx);
    for j=1:length(yy);
    input_vector1=xx(i,j);
    input_vector2=yy(i,j);
    input_vector=[input_vector1,input_vector2];
    f(i,j)=stalagmite(input_vector);
    end
end

 num_cases=50;
% % study1 statistical behaviour
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
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('iteration')
ylabel('function minimum')
tic
% study2-statistical behaviour with upper and lower bounds
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.3;0.3]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_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('iteration')
ylabel('function minimum')
%study3 increasing ga iteration
options = optimoptions('ga');
opt = optimoptions(options,'populationsize',170);
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.3;0.3],[],[],opt);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_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')
title('bounded 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.

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.