Project

Determining Global Maxima of a Stalagmite Function

Determining Global Maxima of a Stalagmite Function

Introduction to Genetic Algorithm

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  • Selection rules select the individuals, called parents, that contribute to the population at the next generation.

  • Crossover rules combine two parents to form children for the next generation.

  • Mutation rules apply random changes to individual parents to form children.

The above principle is used in the implementation of the 'ga' function. A random set of values are chosen from a swarm of values of a function for the purpose of achieving a higher generation of values. And the next set of values are chosen from the `2^nd`generation of values. A similar operation is performed until a threshold is achieved and the function returns the minimum function value and the corresponding dependent variables.

Stalagmite Equation

Stalagmites.

A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites may be composed of lava, minerals, mud, peat, pitch, sand, sinter and amberat (crystallized urine of pack rats).[2][3]
A close identical geometrical shape can be obtained by using a mathematical function. This function should be applied to two variables in two dimensions which yields the stalagmite. It can be mathematically expressed as below.

`f_1(x)=sin^6(5.1*pi*x+0.5)`

`f_1(y)=sin^6(5.1*pi*y+0.5)`

`f_2(x)=exp^((-4*log2*(x-0.0667)^2)/0.64)`

`f_2(y)=exp^((-4*log2*(y-0.0667)^2)/0.64`

`f(x,y)=f_1(x)*f_1(y)*f_2(x)*f_2(y)`

Structure of the MATLAB program

The program has two User Defined Stalagmite Functions and one primary body. Three studies are performed using the same function but with alterations in function 'ga'.

Part 1: ‘stalagmite1’ function

  • A function 'stalagmite1' is created with return value being `f(x,y)`
  • The inputs of the function should be a vector of x and y.
  • The equations as shown in 'Stalagmite Equations' are entered but `f(x,y)`is multiplied by -1 which generates a downward stalagmite.

Function body

function [product] = stalagmite1(input_vector)
             xpy = input_vector(1)*pi;
             ypy = input_vector(2)*pi;
             
             f1x = (sin(5.1*xpy+0.5))^6;
             f1y = (sin(5.1*ypy+0.5))^6;
             
             f2x = exp(-4*log(2)*((input_vector(1)-0.0667)^2/0.64));
             f2y = exp(-4*log(2)*((input_vector(2)-0.0667)^2/0.64));
             
             product = -1*f1x*f1y*f2x*f2y;

end

Part 2: ‘stalagmite’ function

  • A function 'stalagmite' is created with return value being `f(x,y)`
  • The inputs of the function should be a vector of x and y.
  • The equations as shown in 'Stalagmite Equations' are entered and `f(x,y)`is kept as shown.

Function body

function [product] = stalagmite(input_vector)
             xpy = input_vector(1)*pi;
             ypy = input_vector(2)*pi;
             
             f1x = (sin(5.1*xpy+0.5))^6;
             f1y = (sin(5.1*ypy+0.5))^6;
             
             f2x = exp(-4*log(2)*((input_vector(1)-0.0667)^2/0.64));
             f2y = exp(-4*log(2)*((input_vector(2)-0.0667)^2/0.64));
             
             product = f1x*f1y*f2x*f2y;

end

Part 3: Implementing the UDFs and 'ga' in the body

The body is divided into 3 studies in which certain commands are altered.

  • x and y values are initialized and another value num_case is initialized to any number which is used to iterate the 'ga' function so many times.
  • A meshgrid is created using x and y values using 'meshgrid' function which gives two square matrices.
  • A new matrix is assigned to the 'stalagmite1' function whose inputs are set of coordinates which are generated by keeping this in two for loops. The outer for loop navigates the first square matrix generated by 'meshgrid' and the second for loops navigates the second square matrix. This generates a set of values in the 3rd dimension.
  • Now a similar operation as the previous step is performed using 'stalagmite' function. 'stalagmite' function generates values in the positive z-direction.
  • Now 'ga' function is implemented to 'stalagmite1' because 'stalagmite1' generates values in the negative z-direction. The 'ga' function returns the minimium of the 'stalagmite1' function and their corresponding dependent variables.
  • The minimum value van by multiplied with -1 to get a positive number and ca be plot on the function surface of 'stalagmite' function and hence the maximum and their coresponding x and y values can be shown. The 'ga' function is iterated several times to get a range of maximum values and the most maximum value is chosen.
  • The similar procedure is followed in study 2 where the limiting values of x and y axes are shown on the plot.
  • In the 3rd study, the number of experimental subjects which the 'ga' function picks at a time is increased to increase the accuracy.

Body of the program

% Study 1
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 100;


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

% Evaluating the downward stalagmite(stalactite) function 
for i = 1:length(xx)
    for j = 1:length(yy)
      input_vector1(1) = xx(i,j);
      input_vector1(2) = yy(i,j);
      F(i,j) = stalagmite1(input_vector1);  
        
        
    end
end



% 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



tic
for i=1:num_case
    [inputs,Fopt(i)] = ga(@stalagmite1, 2);
    Xopt(i) = inputs(1);
    Yopt(i) = inputs(2);
    
end
study1_time = toc;
pFopt = -1.*Fopt;
%Fid is the maximum value
Fid = max(pFopt);

for q=1:num_case
    if pFopt(q)==Fid
       Xid = Xopt(q);
       Yid = Yopt(q);
        
    end
end


figure(1),clf

subplot(211)
surfc(xx,yy,f);
xlabel('X value');
ylabel('Y value');
axis square;
shading interp;
hold on;
scatter3(Xid,Yid,Fid,'o');
title('Without modifying ga');

subplot(212)
plot(pFopt)
xlabel('Iterations')
ylabel('Function Maximum');


%% Study 2

% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 100;


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

% Evaluating the downward stalagmite(stalactite) function 
for i = 1:length(xx)
    for j = 1:length(yy)
      input_vector1(1) = xx(i,j);
      input_vector1(2) = yy(i,j);
      F(i,j) = stalagmite1(input_vector1);  
        
        
    end
end



% 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


% Evaluating the downward stalagmite(stalactite) function
tic
for i=1:num_case
    [inputs,Fopt(i)] = ga(@stalagmite1, 2,[],[],[],[],[0;0],[1;1]);
    Xopt(i) = inputs(1);
    Yopt(i) = inputs(2);
    
end
study1_time = toc;
pFopt = -1.*Fopt;
%Fid is the maximum value
Fid = max(pFopt);

for q=1:num_case
    if pFopt(q)==Fid
       Xid = Xopt(q);
       Yid = Yopt(q);
        
    end
end


figure(2),clf

subplot(211)
surfc(xx,yy,f);
xlabel('X value');
ylabel('Y value');
axis square;
shading interp;
hold on;
scatter3(Xid,Yid,Fid,'o');
title('Boundary Values Assigned')

subplot(212)
plot(pFopt)
xlabel('Iterations')
ylabel('Function Maximum');


%% Study 3
close all
clear all
clc

% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 100;


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

% Evaluating the downward stalagmite(stalactite) 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) = stalagmite1(input_vector);  
        
        
    end
end



% 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


% Evaluating the downward stalagmite(stalactite) function
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);
tic
for i=1:num_case
    [inputs,Fopt(i)] = ga(@stalagmite1, 2,[],[],[],[],[0;0],[1;1],[],[],options);
    Xopt(i) = inputs(1);
    Yopt(i) = inputs(2);
    
end
study1_time = toc;
pFopt = -1.*Fopt;
%Fid is the maximum value
Fid = max(pFopt);

for q=1:num_case
    if pFopt(q)==Fid
       Xid = Xopt(q);
       Yid = Yopt(q);
        
    end
end


figure(3),clf

subplot(211)
surfc(xx,yy,f);
xlabel('X value');
ylabel('Y value');
axis square;
shading interp;
hold on;
scatter3(Xid,Yid,Fid,'o');
title('With Boundary levels and Specified Population size');

subplot(212)
plot(pFopt)
xlabel('Iterations')
ylabel('Function Maximum');


Output

Study 1 output

Study 2 output

Study 3 output

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.