Project

Determining the Global Maxima of a Stalagmite Mathematical function using Genetic Algorithm

Objective

1. To write a MATLAB code to optimize a stalgamite mathematical model

2. To find the global maxima of the following model using Genetic Algorithm.

Generic algorithm:

Genetic Algorithm is optimization method based on the mechanics of natural genetics and natural selection. Genetic Algorithm mimics the principle of natural genetics and natural selection to constitute search and optimization procedures.GA is used for scheduling to find the near to optimum solution in short time. In a genetic algorithm representation is done with variable length of sub-chromosome.GA is developed to generate the optimal order scheduling solution. GA is used as tool in different processes to optimize the process parameters. This paper reviews the genetic algorithms that are designed for solving multiple problems in applications of material science and manufacturing in field of mechanical engineering.

Syntax for defining ga in MATLAB -

There are several ways to define the ga function depending on the problem being solved. The syntax used for this study is shown below.

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

where,

x = optimum solution/variables.

nvars = Number of variables, specified as a positive integer.

A,b = Linear inequality constraints.

Aeq,beq = Linear equality constraints.

lb= Lower bounds, ub=Upper bounds

Code:

%Creating  and y array
x=linspace(0,0.5,150);
y=linspace(0,0.5,150);
num_cases = 50;

%Creating 2d 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); %function call
       
   end
end


%Study: 1 - Satistical Behaviour, unbounded inputs
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 - Bounded input with increased population size
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
title(\'Bounded input with increased population size\'
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); % -ve sign present to get Global maxima rather than minima
      
end

Results:

Study 1 - Unbounded input results

\"\"

It can be observed how the random sampling of the genetic algorithm function generates a new optimum value for the function everytime the code is run from the above figure. Although this falls in line with the definition of genetic algorithm is supposed to function, we need to introduce limits to constrain the \'ga\' function within specified ranges between 0 and 1.

Study 2 - Statistical data with upper and lower bounds

Using the syntax for ga mentioned earlier in the report, the upper and lower bound limit are defined and immediately an improvement in the sampling can be seen from the figure below.

\"\"

Local minima value returned from the function was somewhere in between -0.7 to -0.85 which is much more narrow range but its still not the accurate solution. Specifying the search range depending on the problem being solved can help in marching towards finding the global minima.

Study 3 -

\"\"

The population size was increased to 170 from the default value of 50. By increasing this value, the dataset is going to increase and subsequently the number of random samples is going to increase too. This leads longer solving time but a more accurate solution is obtained. Since the ga function can only calculate the global minima of a function, a negative sign is introduced when defining the function so as to view to global maxima which is shown in the above figure. A steady value of 0.99 can be observed with minor spikes which can be eliminated by increasing the population size further.

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.