Project

MATLAB: Stalagmite Mathematical Model using Genetic Algorithm

Introduction:
Genetic Algorithm is derived from Charles Darwin\'s theory of evolution which states that \' It is not the strongest of the species that survives, nor the most intelligent , but the one most responsive to change.\' The entire concept of Genetic Algorithm is based on the above famous statement.

Genetic Algorithm is only used to generate minimum solution. So the value of stalagmite function is taken negative to maximise the function.

Syntax For ga :

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)

where,

1) x = optimum solution/variables.

2) no_of_variables = Number of variables, specified as a positive integer.

3) A,b = Linear inequality constraints.

4) Aeq,beq = Linear equality constraints.

5) lb,ub. lb = Lower bounds, ub=Upper bounds

6) nonlcon : Use for nonlinear constraints.

7) IntCon : to get solution in integer.

8) options: Minimize with non default options.

Matlab Code:

Stalagmite Function Code:

function [F] = stalagmite(input_vector)
         x=input_vector(1) ;
         y=input_vector(2) ;
         
         f1_x = (sin(5.1*pi*x+0.5)).^6;
         f1_y = (sin(5.1*pi*y+0.5)).^6;
         f2_x = exp(-4*log(2)*((x-0.0667).^2)/0.64);
         f2_y = exp(-4*log(2)*((y-0.0667).^2)/0.64);
         [F] = -(f1_x.*f1_y.*f2_x.*f2_y);
         end
%Creating  and y array
x=linspace(0,0.5,150);
y=linspace(0,0.5,150);
num_cases = 50;

%Creating two dimensional mesh
[xx,yy]= meshgrid(x,y);

%Calculating the stalagmite function
for i=1:length(xx)
   for j=1:length(yy)
       a(1)=xx(i,j);
       a(2)=yy(i,j);
      f(i,j)=stalagmite(a);
       
   end
end


%Study: 1 - Satistical Behaviour
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
surfc(xx,yy,f)
shading interp
title(\'Unbounded inputs\')
xlabel(\'x values\')
ylabel(\'y values\')
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',6,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')

%Study: 2 - Statistical Behaviour - with Upper and Lower Bounds
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
surfc(xx,yy,f)
shading interp
title(\'Bounded inputs\')
xlabel(\'x values\')
ylabel(\'y values\')
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',6,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')

%Study: 3 - Increasing GA Iterations
options=optimoptions(\'ga\')
options=optimoptions(options,\'PopulationSize\',170)
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
fopt=-fopt;
f=-f;
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel(\'x values\')
ylabel(\'y values\')
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',6,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
max_value=fopt
stalagmite function code:
function [F]=stalagmite(a)
      x = a(1);
      y = a(2);

      f1=(sin((5.1*pi*x)+ 0.5))^6;
      f2=(sin((5.1*pi*y)+ 0.5))^6;
      f3= exp((-4*log(2)*((x-0.0667)^2))/(0.64));
      f4= exp((-4*log(2)*((y-0.0667)^2))/(0.64));
      
      F = -(f1*f2*f3*f4);
      
end

Study 1 Plot:

Study 2 Plot:

Study 3 Plot:

Error:

Report:

1. Initially create x and y array and number of iterations you want.

2. For 2D Mesh: create xx and yy array by using meshgrid command.

3. Using nested for loop, calculate the stalagmite function and store the value in f. Here we use -ve sign in stalagmite function, to calculate the global maxima.

4. Study-1, it calulate the global maxima by taking random samples from the function. Here we use tic-toc command for calculating the time taken for study-1.

5. Syntax for ga in study-1 is [inputs,fopt(i)]=ga(@stalagmite,2);

6. Study-2, it calculate the global maxima by giving boundary limits for the inputs.

7. Syntax for ga in study-2 is [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);. In that you see the boundary limits 0 to 1.

8. Study-3, it calculate the global maxima by giving population size by which we can find the specific optimum point.

9. Syntax for ga in study-3 is [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);. Here, options=optimoptions(\'ga\') options=optimoptions(options,\'PopulationSize\',170) In that the population size given is 170. The maximum value of stalagmite function is +1 obtained.

10. The study time image is displayed in workspace.

Study Time: For 1, 2 and 3.

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.