GLOBAL MAXIMA BY OPTIMIZING A STALAGMITE FUNCTION USING GENETIC ALGORITHM IN MATLAB
I. OBJECTIVES
- To write a program that can optimize the Stalagmite Function and find the optimum Global Maxima for the same.
- To discuss the concept of a Genetic Algorithm as well as the use of syntax for ga in MATLAB.
- To plot graphs for all three studies and F maximum vs No. of iterations and explain the results.
II. INTRODUCTION
1. The Stalagmite Function: -
A Stalagmite is a type of speleothem that rises from the floor of a limestone cave due to the dripping of mineralized solutions and the deposition of calcium carbonate. Thus, the mathematical function that can plot the paths replicating the stalagmite by generating local maxima and minima is called a Stalagmite Function.
It is a three-dimensional function that has multiple Maxima and Minima for a range of input values. The Stalagmite function results in the formation of cone-like structures based on the formulated data. Following is the mathematical form of the Stalagmite function:
2. Genetic Algorithm: -
A Genetic Algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
The genetic algorithm can address problems of mixed-integer programming, where some components are restricted to be integer-valued. The genetic algorithm solves smooth or nonsmooth optimization problems with any types of constraints, including integer constraints. It is a stochastic, population-based algorithm that searches randomly by mutation and crossover among population members.
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.
2.1. The syntax for Genetic Algorithm: -
The following are the syntaxes used for applying the 'ga'. The basic attribute for the syntaxes is that it solves the minimum for any problem. According to the environment, the syntax is chosen.
2.2. Working Methodology of the Genetic Algorithm: -
The following outline summarizes how the genetic algorithm works:
-
The algorithm begins by creating a random initial population.
-
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. To create a new population, the algorithm performs the following steps:
-
Scores each member of the current population by computing its fitness value. These values are called 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 expectations.
-
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 their 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 to form the next generation.
-
-
The algorithm stops when one of the stopping criteria is met.
2.3. The Stopping Criteria for the Genetic Algorithm: -
The genetic algorithm uses the following conditions to determine when to stop:
-
Generations — The algorithm stops when the number of generations reaches the value of Generations.
-
Time limit — The algorithm stops after running for an amount of time in seconds equal to the Time limit.
-
Fitness limit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to the Fitness limit.
-
Stall generations — The algorithm stops when the average relative change in the fitness function value over Stall generations is less than Function tolerance.
-
Stall time limit — The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to the Stall time limit.
-
Stall test — The stall condition is either
average changeorgeometric weighted. Forgeometric weighted, the weighting function is 1/2n, where n is the number of generations before the current. Both stall conditions apply to the relative change in the fitness function over Stall generations. -
Function tolerance — The algorithm runs until the average relative change in the fitness function value over Stall generations is less than Function tolerance.
-
Constraint tolerance — Constraint tolerance is not used as a stopping criterion. It is used to determine the feasibility concerning nonlinear constraints. Also,
max(sqrt(eps),ConstraintTolerance)determines feasibility concerning linear constraints.
The algorithm stops as soon as anyone of these conditions is met.
III. MATLAB PROGRAM
A. Program for creating the Stalagmite Function
% A program to create the Stalagmite Function
function [S] = stalagmite(input_val)
x = input_val(1);
y = input_val(2);
T1 = (sin(5.1*pi*x + 0.5))^6;
T2 = (sin(5.1*pi*y + 0.5))^6;
T3 = exp(-4*log(2)*((x - 0.0667)^2)/0.64);
T4 = exp(-4*log(2)*((y - 0.0667)^2)/0.64);
S = -(T1*T2*T3*T4);
end
A.1. Steps for the above program: -
- For creating the function program, the keyword 'function' along with the output variable in brackets is used to initiate the code. This is followed by providing the desired syntax (for our case - stalagmite) with the necessary input vectors under parenthesis.
- Since the input vector consists of two arrays of different values, they are assigned to the respective positions.
- As seen from the formula of the Stalagmite Function, the expression is broken into four terms for the ease of purpose.
- Finally, the output variable is given a product of all four terms. The value of S is negative so that the formulation has evaluated for the Global Maxima. The function is ended after this.
A.2. The result from the above program: -
The Stalagmite Figure
The figure above represents the structure of a set of Stalagmites with several Local maxima and one Global maximum. This result is obtained once it is run through the following code.
The program depicted by A. is a functional program that has to be executed in a different program by calling the same function (as seen in the 'for' loop above). This is done to show how the Stalagmite Function works and what we are trying to deal with. This step is not an elegant part of the procedure and hence not included in section A.1.
A.3. Errors from the above program: -
No intricate errors were encountered while running the above program.
B. Program for evaluating the Global Maxima of the Stalagmite Function using GA
% A program to optimize the Stalagmite function by calculating the Global Maxima
clear all
close all
clc
%% Input vectors
x = linspace(0,0.6,150); % Creating uniform vectors
y = linspace(0,0.6,150);
i_num = 50; % Number of iterations to compute
[xx,yy] = meshgrid(x,y); % Creating a 2D array
% Assigning Input Values to the Function
for i = 1:length(xx)
for j = 1:length(yy)
input_val(1) = xx(i,j);
input_val(2) = yy(i,j);
S(i,j) = stalagmite(input_val);
end
end
%% STUDY #1 - Statistical Behavior under Unbounded Conditions
tic
% Evaluating the function using GA
for k = 1:i_num
[inputs_1, fopt_1(k)] = ga(@stalagmite, 2);
xopt_1(k) = inputs_1(1);
yopt_1(k) = inputs_1(2);
end
study1_time = toc;
% Plot commands for STUDY #1
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,S)
shading interp
plot3(xopt_1,yopt_1,fopt_1,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X Values')
ylabel('Y Values')
title('Statistical Behavior under Unbounded Conditions')
subplot(2,1,2)
plot(fopt_1)
xlabel('Iterations')
ylabel('Function Value at the Solution')
%% STUDY #2 - Statistical Behavior under Upper and Lower Bounds
tic
% Evaluating the function using GA
for k = 1:i_num
[inputs_2, fopt_2(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt_2(k) = inputs_2(1);
yopt_2(k) = inputs_2(2);
end
study2_time = toc;
% Plot commands for STUDY #2
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,S)
shading interp
plot3(xopt_2,yopt_2,fopt_2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X Values')
ylabel('Y Values')
title('Statistical Behavior under Bounded Conditions')
subplot(2,1,2)
plot(fopt_2)
xlabel('Iterations')
ylabel('Function Value at the Solution')
%% STUDY #3 - Statistical Behavior under Increasing Population
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize', 220);
tic
% Evaluating the function using GA
for k = 1:i_num
[inputs_3, fopt_3(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt_3(k) = inputs_3(1);
yopt_3(k) = inputs_3(2);
end
study3_time = toc;
% Plot commands for STUDY #3
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,S)
shading interp
plot3(xopt_3,yopt_3,fopt_3,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X Values')
ylabel('Y Values')
title('Statistical Behavior with Higher Population Size')
subplot(2,1,2)
plot(fopt_3)
xlabel('Iterations')
ylabel('Function Value at the Solution')
%% Assembling Study Times into a single array
Study_Times = [study1_time study2_time study3_time] % Array for time taken by each study
%% Determining the Global Maxima Coordinates
X = min(xopt_3);
Y = min(yopt_3);
Z = min(fopt_3);
Global_Maxima = [X Y Z]
B.1. Steps for the above program: -
- The program is commenced with the basic 'clear all','close all','clc' commands.
- The input vectors are provided using 'linspace' command for equal intervals. Also, the number of iterations was specified and the inputs are converted into a 2D array using the 'meshgrid' command.
- The inputs are further assigned as the functional values for the stalagmite function using the 'for' loop. ◈ STUDY #1 - Statistical Behavior under Unbounded Conditions
- The first study is initiated with the 'tic' command to start the timer. A 'for' loop is opened ranging from 1 to the length of the number of iterations.
- This study engages the genetic algorithm without any restriction or bounds. This means that the GA will freely choose the population to reproduce and mutate. Under the loop, evaluation of the Stalagmite function is done by calling the GA using the syntax as seen in the program. Here, the input array and output array are given followed by the initiation of GA for the function having two variables. The syntax can be understood from point 2.1 stated above.
- Under the same loop, the variables for input arrays are provided. This has to be done since the input arrays have to be known throughout the formulation of the loop. The loop is ended from here.
- The timer is ended using the 'toc' command and noted as a constant term.
- Plot commands are further given. Two different subplots are created. In the first one, the 'surfc' function is used to plot the stalagmite and 'plot3' command is used to plot the points evaluated by the GA. The second subplot is graphed for the output values for the stalagmite function that provides a reference. All the necessary titles and labels are given. ◈ STUDY #2 - Statistical Behavior under Upper and Lower Bounds
- For the second study, all the steps are the same as the first study except the evaluation step. As observed in the code, the syntax for the GA is different. Here the GA is provided with an upper limit as well as a lower limit for finding the minimum under the population. The variables for this study are altered accordingly.
- The plot commands for this study remain identical to the previous study. Only the variables and titles are changed per the current study. ◈ STUDY #3 - Statistical Behavior under Increasing Population
- For the third study, the procedure is a tad bit different. Here, the population size for iteration has to bee increased to get an accurate answer. To increase the iterations, the 'optimoption' function is used to increase population size to the desired value. We take 220 for our purpose. The syntax for GA that has to be used can be seen in the code. The 'option' function is integrated into the evaluation loop. The rest of the steps are pretty identical to the previous study. Necessary changes are made to all the variables for this study.
- The plot commands remain the same for this study as well except for the variation in the variables.
- Finally, the total evaluation times for each study are assembled in an array for comparison. Also, the minimum values from the output arrays are picked to display the coordinates of Global Maxima. This was done on a trial basis since the coordinate of the global maxima is obtained by getting the least values from the output values. This can be understood by examining the results.
B.2. Results from the above program: -
1. STUDY #1 - Statistical Behavior under Unbounded Conditions
The above plot is the result of our first study where the GA is run without any restrictions and was free to choose the population for iteration. As a result, the points that were chosen by the algorithm for evaluation can be seen in the first subplot marked by red-colored dots. It is visible that the points are all over the place and this study fails to predict the coordinates for Global Maxima.
2. STUDY #2 - Statistical Behavior under Upper and Lower Bounds
The above-mentioned plot shows much less variation and it looks cleaner. The algorithm is now able to determine the position of the peaks of the function. This happens as a result of providing bounds to the GA which reduces the variation of the values of the function. Although this method provides us with the points of various maxima, it turns out to be futile if we want to find a single Global Maxima. At least this method doesn't change the result on carrying out multiple iterations.
3. STUDY #3 - Statistical Behavior under Increasing Population
From Study #3, we successfully get the desired results. As can be seen in the plot, the point of Global Maxima is marked in red. This was possible due to the optimization function 'optimoption' that returns the GA altered by a set of values i.e. population size. This allowed us to increase the number of iterations by some value without altering the functional values. Since the GA is commanded to work under bounded conditions as well as increased iterations, the value converges to a single coordinate which happens to be the Global Maxima.
The final coordinates obtained after the evaluation of the function is provided below.
X = 0.0668; Y = 0.0668; Fopt = -1.00 (approx.)
B.3. Errors from the above program: -
No intricate errors were encountered while running the above program.
IV. CONCLUSION
In this project report, the theory and application of the Genetic Algorithm were discussed for the Optimization purpose of a Stalagmite Function in MATLAB. Therefore, the following is the only conclusion that can be made on the basis of the study done on the topic: -
- The Genetic Algorithm has better efficacy in optimizing any function when it is bounded by some limits and given enough iterations to come up with an accurate result.
V. REFERENCES
- Genetic Algorithms - GeeksforGeeks
- What is a Genetic Algorithm - MathWorks
- How the Genetic Algorithm Works - MathWorks
- Genetic Algorithm Syntaxes - MathWorks
- Create optimization options - MathWorks