Challenge

Week 4.1 - Genetic Algorithm

1) Stalagmite function program:

function [f] = stalagmite(input)

x = input(1)
y = input(2)

term1 = (sin((5.1*pi*x) + 0.5))^6;

term2 = exp((-4)*log(2)*((x-0.0667)^2)/0.64);

term3 = (sin((5.1*pi*y) + 0.5))^6;

term4 = exp((-4)*log(2)*((y-0.0667)^2)/0.64);

f = (term1*term2*term3*term4);

end


2) Genetic algorithm program


% A program for genetic algorithm

% Defining our search space
x = linspace(0,1,150);
y = linspace(0,1,150);

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

% Evaluating the stalagmite 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 

 num = 50;

 % case1 unbound variables statistical behaviour

 for i = 1:num
 [input, fopt(i)] = ga(@stalagmite,2)
 xopt(i) = input(1);
 yopt(i) = input(2);
 end

 figure(1)
 subplot(2,1,1)
 hold on
 surfc(x,y,f)
 shading interp
 hold on
 plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
 title('statistical behaviour with unbound variables')
 subplot(2,1,2)
 plot(fopt)
 xlabel('Iterations')
 ylabel('Function maximum')

 % case2 statistical behaviour with upper and lower bounds

for i = 1:num
    [input, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
      xopt(i) = input(1);
      yopt(i) = input(2);
end     

 figure(2)
 subplot(2,1,1)
 hold on
 surfc(x,y,f)
 shading interp
 hold on
 plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
 title('statistical behaviour with upper and lower')
 subplot(2,1,2)
 plot(fopt)
 xlabel('Iterations')
 ylabel('Function maximum')

 % case3 increasing iterations by increasing population size

 options = optimoptions('ga');
 % defining population size
 options = optimoptions(options,'populationsize',160);

 for i = 1:num
 [input, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
 xopt(i) = input(1);
 yopt(i) = input(2);
 end

 figure(3)
 subplot(2,1,1)
 hold on
 surfc(x,y,f)
 shading interp
 hold on
 plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
 title('statistical behaviour with increase in population size')
 subplot(2,1,2)
 plot(fopt)
 xlabel('Iterations')
 ylabel('Function maximum')



Explanation:
In stalagmite function program the equations provided in the challenge is divided into 4 terms and defined.

In genetic algorithm program output values of x & y is computed between search space 0 to 1. Here a 2D array is created since there are 2 variables. A for loop is created and both variables is defined using stalagmite function. The program is run for 50 iterations for each variables x&y.
case1 Here in figure1 a subplot is created in order to plot multiple graphs in same figure1.First surface is plotted then later plot3 is used to find optium value of x,y & f. Another subplot is plotted for optimum value of function f. After runnimg the code for 50 iterations, the graph obtained is not accurate.
case2 here variables x&y are alloted with bounds (upper & lower) i.e 0 & 1. The code is run within this bounds and results obtained is slightly better than case1.
case3 Inthis case along with upper & lower bounds population size of 160 is alloted. By doing this it will take longer time to run the code.The results obtained is better than case2. So in order to get accurate results we nee to run the code for more number of oterations. 









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.