Challenge

Week 4.1 - Genetic Algorithm

% Genatic_algorithm

close all
clear all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

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

 for i=1:1:length(xx)
 for j =1:length(yy)
     vector(1)=xx(i,j);
     vector(2)=yy(i,j);
     f(i,j)=stalagmite(vector);
end
end

num_cases=50;

%study1-unbound calculations
tic
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
surf(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')

%study2-bound calculations
tic
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
surf(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('Iterations')
ylabel('Function Minimum')

 %study3-bound calculations
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',200);
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
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inputs with  increased population')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Minimum') 

% study4-bound calculations
tic
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500);
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


study4_time=toc;
figure(4)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inputs with increased population(500)')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
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.