Challenge

Week 4 - Genetic Algorithm

%study 1 %study 2 %study 3

close all
clear all
clc

%setting up the search spcae 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);


%creating a 2d mesh 
[xx,yy] = meshgrid(x,y);

%for salagmite 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

%[results,fval] =ga(@stalagmite,2);
%maximum = -fval

%for study 1
iterations = 50;
tic
for i = 1:iterations
  [inputs,fopt(i)] = ga(@stalagmite,2);
  xopt(i) = inputs(1);
  yopt(i) = inputs(2);
end
 study_time1 = toc
 figure(1)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 shading interp
 plot3 (xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','b');
 title('Unbounded Input');
 subplot(2,1,2);
 plot (-fopt)
 xlabel ('Iterations');
 ylabel ('Function Maximum');

 
 %for study 2
 iterations = 50
 tic
 for i = 1:iterations
   [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
   xopt(i) = input(1);
   yopt(i) = input(2);
 
end

study_time2 = toc
 figure(2)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 shading interp
 plot3 (xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','r');
 title('bounded Input');
 subplot(2,1,2);
 plot (-fopt);
 xlabel ('Iterations');
 ylabel ('Function Maximum');
 
 %for study 3
 options = optimoptions('ga');
 options = optimoptions(options,'Populationsize',250)
 
 tic
 for i=1:iterations
 [input fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
   xopt(i) = input(1);
   yopt(i) = input(2);
 
end

study_time3 = toc
 figure(3)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 shading interp
 plot3 (xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','y');
 title('bounded Input');
 subplot(2,1,2);
 plot (-fopt)
 xlabel ('Iterations');
 ylabel ('Function Maximum');
Stalagmite Function :

 

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.