Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc

%Defining the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%no of iteration
n=50;

[xx,yy]=meshgrid(x,y);

%Evaluating stalgamite 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)=stalagmite(input_vector);
    end
end

tic
%Case 1-Statistical behaviour
for k=1:n
    [inputs1,fopt1(k)]=ga(@stalagmite,2);
    xopt1(k)=inputs1(1);
    yopt1(k)=inputs1(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp

plot3(xopt1,yopt1,-fopt1,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour')

figure(2)
plot(-fopt1)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour')

%Case 2-Statistical behaviour with upper and lower bound
for k=1:n
    [inputs2,fopt2(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
    xopt2(k)=inputs2(1);
    yopt2(k)=inputs2(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp

plot3(xopt2,yopt2,-fopt2,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical behaviour with upper and lower bound')

figure(4)
plot(-fopt2)
xlabel('Iterations')
ylabel('fopt')
title('Statistical behaviour-with upper and lower bound')

%Case 3-Statistical behaviour-with increasing GA iterations
options=optimoptions('ga')
options=optimoptions(options,'populationsize',300)

for k=1:n
    [inputs3,fopt3(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt3(k)=inputs3(1);
    yopt3(k)=inputs3(2);
end
figure(5)
subplot(2,1,1)
hold on,
surfc(xx,yy,-f)
shading interp

plot3(xopt3,yopt3,-fopt3,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical behaviour with increasoing GA iteratoions')

figure(6)
plot(-fopt3)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour-with increasing GA iterations')
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.