Challenge

Week 4 - Genetic Algorithm

Optimization using the Genetic Algorithm(GA)

Aim : Determination of the Global Maxima of the Stalagmite Mathematical model using the Genetic Algorithm(GA)

Objective(s) :

  1. To gain knowledge about the Stalagmite mathematical model.
  2. To understand the Genetic algorithm in detail.
  3. Application of GA to solve for the global maxima of the stalagmite function.

Background Study :

  • Stalagmite Function

What is a Stalagmite [1] ?

Stalagmite is a type of rock formation that arises from the floor of a cave due to accumulation of material deposited on the floor from ceiling drippings.

Fig.1 : Stalagmite formation in a cave. [image source :https://ichemeblog.org/tag/stalagmites/]

The stalagmite function is a mathematical way of representation of stalagmites.

Applications of stalagmite function [2] : In nuclear industrial containments, the salt solution precipitates out, forming structures with strange morphologies that bear a resemblance to stalagmites.

  • Genetic Algorithm :

What is Genetic Algorithm (GA)[3]:

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is 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.

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.

Terminology [4] :

  1. Fitness function : The function that has to be optimized is called the fitness function.

In our case the stalagmite function is the fitness function.

2. Individuals : An individual is any point to which you can apply the fitness function. The value of the fitness function for an individual is its score.

For instance, A vector (8,-3) or any random vector is an individual.

3. Population : An array of individuals is called population. The size of the population is the total number of the individuals in the array.

An individual can appear more than once in the population.

At each iteration, the genetic algorithm performs a series of computations on the current population to produce a new population.

4. Generation : The population set produced by the GA afteran iteration is referred to as Generation.

5. Parents and Children : To create the next generation, the genetic algorithm selects certain individuals in the current population, called parents, and uses them to create individuals in the next generation, called children. Typically, the algorithm is more likely to select parents that have better fitness values.

6. Fitness Values : The fitness value of an individual is the value of the fitness function for that individual.

In MATLAB, the toolbox software finds the minimum of the fitness function, the best fitness value for a population is the smallest fitness value for any individual in the population.

How the Genetic Algorithm works [5]:

  1. The algorithm begins by creating a random initial population.
  2. The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population for which it does :
  • Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
  • Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.

  • Selects members, called parents, based on their expectation.

  • Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.

  • Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.

  • Replaces the current population with the children form the next generation.

3. The algorithm stops when one of the stopping criteria is met.

Stopping criteria in Genetic Algorithm [6]:

The genetic algorithm uses the following options to determine when to stop.

  • MaxGeneartions — The algorithm stops when the number of generations reaches MaxGeneartions.

  • MaxTime — The algorithm stops after running for an amount of time in seconds equal to MaxTime.

  • FitnessLimit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to FitnessLimit.

  • MaxStallGenerations — The algorithm stops when the average relative change in the fitness function value over MaxStallGenerations is less than Function Tolerance.

  • MaxStallTime — The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to MaxStallTime.

  • FunctionTolerance — The algorithm runs until the average relative change in the fitness function value over MaxStallGenerations is less than FunctionTolerance .

  • ConstraintTolerance — The ConstraintTolerance is not used as stopping criterion. It is used to determine the feasibility with respect to nonlinear constraints. Also, max(sqrt(eps), ConstraintTolerance) determines feasibility with respect to linear constraints.

The algorithm stops as soon as any one of these conditions is met.

Syntax for GA :

x = ga(function,nvar,A,b,Aeq,beq,lb,ub,nonlcon,options).

1. function refers to the fitness function that has to be mizimized. In our case the fitness function is the Stalagmite function. The fitness function has to be specified as the function handle or function name.

consider x = ga(@stalagmite,2). Here, the stalagmite function is being passed.

2. nvars refers to the number of variables that have to be passed to the function.

In case of the stalagmite function x and y are the variables that have to be passed and hence the value assigned to nvars is 2.

3. A refers to the matrix which contains the coefficients of the variables present in the linear inequalities.

Since A is a matrix, its size is usually M x nvars. M refers to the number of inequalities.

For example, consider the following set of linear inequalities associated with the fitness function.

`3x _1+ 2x_2 <= 10` -------------------(1)

`5x_1 + -1x_2 = -3` ---------------------(2)

The number of linear constraints here are two and hence M is assigned the numerical value 2. Thus the size of the matix A is 2 x 2.

In the syntax, A is written as [3 2; 5 -1].

In our case, the matrix A has to be empty as there aren't any linear constraints specified.

4. b

b is a column vector whose values are assigned to M number of rows.

Consider the linear inequalities presented above, the constituents of the right hand side represent the rows of the vector b.

b = [10 ; -3] for the above mentioned linear inequalities.

In our case of stalagmite function, the vector b is empty as there aren't any linear constraints specified.

5. Aeq refers to the matrix whose elements are the coeffecients of the linear equalities which are constraints to the fitness function.

The size of the matrix Aeq is Me x nvars. Here, Me refers to the number of equalities.

For example, consider the following linear equalities as the constraints,

`-4x_1 + 3x_2 + 7x_3 = -3` ----------------------(1)

`2x_1 + 3x_2 + 5x_3 = 2` ------------------------(2)

`6x_1 + 5x_2 - x_3 = 9` --------------------------(3).

In this case the matrix Aeq = [-4 3 7;2 3 5;6 5 -1]. Here, Me is 3 since there are 3 equalities.

Coming to our case of Stalagmite function, there are no constarints which represent the linear equalities and hence it can be left.

6. beq refers to the column vector which takes in the numerical values present on the R.H.S of the equality sign of the linear equality constraints.

For the above mentioned linear equality constraints, beq = [-3;2;9]. The number of rows for this column vector depend on the number of the linear equality constraints.

7. lb is a vector or an array of elements that represnts the lower bounds of the variables.

For example, let `x(1) >= 0` and `x(3) >=5` be the lower bounds.

Thus lb = [0; -Inf; 5].

8. ub is a vector or an array of elements that represents the upper bounds of the variables.

For example, let `x(5) <= 2` and `x(7) <= -3`be the upper bounds.

Thus ub = [Inf; 2; -3].

9. nonlcon refers to the non-linear constraints. These have to be input as function handle or function name.

Syntax : x = ga(@stalagmite, 2, [ ],[ ],[ ],[ ],[-1 1],[-1 1], @st_con)

In the above syntax, st_con is a function which needs to be defined/ framed earlier in a separate function file.

function [c,ceq] = st_con(k)

c = _____ % compute nonlinear inequalities at x.

ceq = _____ % compute nonlinear equalities at x.

Algorithm to compute the global maxima of the stalagmite function using the ga :

1. create x and y row vectors of 150 elements each whose elements in each row are linearly spaced between 0 and 0.6 using the following lines of code.

x = linspace(0,0.6,150);

y = linspace(0,0.6,150);

2. Craete a 2D matrix [X, Y] of 150 x 150 elements whose each row consists of the elements either form vector x or y.

[X, Y] = meshgrid(x,y)

3. The stalagmite function takes in two independent variables X and Y. These values need to be passed to the stalagmite function by calling the function in the main code.

4. The result will be matix f of 150 x 150 elements which are nothing but the function value corresponding to the input variables X and Y.

5. Type in the below syntax which will invoke the ga solver.

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',600); % A population size of 600 is set.

[inputs, fopt(i)] = ga(@stalagmite,2,[ ],[ ],[ ],[ ],[0;0],[1;1],[ ],options);

6. The inputs variable stores the values of the independent variables (x and y) for which our stalagmite function has the maximum value.

7. use the surfc and plot3 functions which plot the surface and the maximum values of the stalagmite function respectively.

Code :

% A function file "get_stalagmite that generates the necessary inputs for the stalagmite function

function [f] = get_stalagmite(X,Y)

for i = 1 : length(X)
    for j = 1 : length(Y)
        
        [XX, YY] = meshgrid(X,Y);
        
        input_vector(1) = XX(i,j);
        input_vector(2) = YY(i,j);
        
        f(i,j) = stalagmite(input_vector);
    end
end
% A function file that calculates the value of the stalagmite function when called through main function.

function [fopti] = stalagmite(input_vector)

f1x = sin((5.1*pi*input_vector(1)+ 0.5))^6;
f1y = sin((5.1*pi*input_vector(2) + 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);

fopti = -1*f1x*f1y*f2x*f2y;

end

Main function file :

clear,close all;
clc;

% creating x and y row vectors
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

f = get_stalagmite(x,y);

num_cases = 150;

%studying the statistical behaviour
tic
for i = 1 : num_cases
    [inputs, fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2); 
   
end

timer1 = toc
%plotting the results
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('unbounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('no of iterations')
ylabel('function max.')


% studying the statistical behaviour with lower and upper 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
study_time2 = toc
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('no of iterations')
ylabel('function max.')


% increasing the population for GA 

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',600);
tic
for i = 1 : 150
    [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2); 
   
end
study_time3 = toc;
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('increased population');
subplot(2,1,2)
plot(fopt)
xlabel('no of iterations')
ylabel('function max.')

Results :

case 1 : A general case where a ga solver is made to run for 150 iterations without any bounds and constraints.

As evident form the above plot, the solver found 150 local maxima points which is not intended. Inputs like lower and upper bounds may refine the code.

case 2 : ga solver is made to run for 150 times with defined lower and upper bounds.

From the above, it can be commented that the desired local maxima could not be still found. Further refining the criteria by increasing the population may result in the prediction of global maxima.

Case 3 : increasing the population size

As evident, the algorithm is able to determine the global maxima (marked with the red dot).

Comments on the GA :

It would be evident by now that the algorithm predicts the different values for the global maxima for every run as it uses random population sample everytime to produce the next generation. hence it is recommended to run the algorithm for a good number of times and consider the value which has the highest probability.

References:

  1. Stalagmite : What is Stalagmite https://en.wikipedia.org/wiki/Stalagmite
  2. Chemical engg. meets geology : https://ichemeblog.org/2014/12/14/chemical-engineering-meets-geology-day-201/#more-5147
  3. Genetic Algorithm : What is GA ? https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html
  4. Terminology of GA: https://in.mathworks.com/help/gads/some-genetic-algorithm-terminology.html
  5. How the GA works : https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html
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.