Challenge

Week 4.1 - Genetic Algorithm

Aim:-

  • To write a code in MATLAB to optimize the stalagmite function and find the function's global maxima.
  • To explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • To plot graphs for all 3 studies and F maximum vs no. of iterations.

Theory:-

Concept of Genetic Algorithm-

A genetic algorithm is a method for addressing constrained and unconstrained optimization problems that is based on a natural selection process that is similar to biological evolution. The algorithm alters a population of individual solutions on a regular basis. At each phase, the genetic algorithm selects individuals from the present population at random to serve as parents for the following generation's children. The population 'evolves' toward an optimal solution" over generations. We can use the genetic algorithm to solve problems that aren't well suited for traditional optimization algorithms, such as situations with a discontinuous objective function, strongly nonlinear, stochastic, or non-differentiable.

In 'ga', the minimum value of a function is calculated by default. So, to obtain the global maximum of a function, we invert the function whose global maximum we want to find, determine the smallest value of the inverted function, then subtract the negative of the minimum value to get the function's global maximum.

The following are the basic steps in building a genetic algorithm:

- At the start, the algorithm generates a random population.

- The algorithm then rates each member of the current population by computing their fitness value and transforming it into a more useable range of values in order to build a new population.

- The algorithm then selects members, known as parents, depending on these new values.

- The program then generates children from the parent. Children are created by introducing random modifications to a single parent, which is referred to as a mutation, or by joining the vector entries of two parents, which is referred to as a crossover.

- The algorithm replaces the current population with offspring to build the next generation in this fashion.
- The algorithm comes to a halt when one of the preset parameters, such as the time limit, fitness limit, generations, function tolerance, constraint tolerance, and so on, is reached.

The Syntax for defining 'ga' in MATLAB-

The 'ga' syntax in Matlab is used to apply the genetic algorithm to find the minimum of a function. Depending on the problem being solved, there are numerous approaches to Syntax for defining 'ga' (Genetic Algorithm) in Matlab to create e the 'ga' function. The following is a list of the syntax that was used in this study.
x=ga(fun,nvars)
where,
x = optimal solution/variables;
fun = Objective function;
nvars = Number of variables;
Sometimes '[x,fval]' is used instead of 'x' where 'fval' gives objective function value at the solution.
x=ga(fun,nvars, A,B,Aeq, Beq,lb,ub)
where,
AB = Linear inequality constraints
Aeq,Beq = Linear equality constraints
if there are no linear equalities and inequalities, then A, B, Aeq, Beq are set to '[]'.
x=ga(fun,nvars,A,B,Acq,Beq,lb,ub,nonleon,IntCon,options)
where,
nonlcon = Nonlinear constraints
IntCon = Integer Values
options = Optimization options
If there are no non-linear constraints and integer values, the nonlcon and IntCon are set to '[]'.
Stalagmite Function:-
The stalagmite function is a mathematical model that aids in the formation of local minima and maxima. It's a`` three-dimensional function with a number of local maxima and minima and only one global maxima or minima because of the way it's created. The term stalagmite refers to a type of rock structure that forms on the cave floor as a result of material deposited on the floor by celling drippings.
Stalagmite Function is given as-
`f1=[sin(5.1pix+0.5)]^6`
`f2=exp[-4ln(2)(x-0.0667)^2/0.64]`
`f3=[sin(5.1piy+0.5)]^6`
`f4=exp[-4ln(2)(y-0.0667)^2/0.64]`
`therefore f=f1*f2*f3*f4`

Body of the content(Code):-

% Code in MATLAB to optimize the stalagmite function and to find the function's global maxima
clc 
clear all
close all

% Defining our search space 
% Independent varaibles
x = linspace(0,0.6,150);% x array
y = linspace(0,0.6,150);% y array

% Number of iterations
n = 50;

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

% Evaluating the stalagmite function 
% Starting the loop
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

% Statistical Behaviour
% Study 1
tic
for i = 1:n
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time = toc % Time taken for study

figure(1)
subplot(2,1,1)
hold on
surf(xx,yy,-f)% Creates a 3 dimensional surface
xlabel = ('X-axis');
ylabel = ('Y-axis');
title('Statistical Behaviour with Unbound inputs')
shading interp% Gives the appearance of a smoother surface
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel = ('No. of Iterations');
ylabel = ('Function maximum');

% Study 2
tic
for i = 1:n
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1,1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study2_time = toc % Time taken for study

figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)% Creates a 3 dimensional surface
xlabel = ('X-axis');
ylabel = ('Y-axis');
title('Statistical behaviour with upper and lower bounds')
shading interp% Gives the appearance of a smoother surface
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel = ('No. of Iterations');
ylabel = ('Function maximum');

% Study 3
% Increasing the probability of the function by increasing the population size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',400);

tic
for i = 1:n
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1,1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study3_time = toc % Time taken for study

figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-f)% Creates a 3 dimensional surface
xlabel = ('X-axis');
ylabel = ('Y-axis');
title('Statistical Behaviour with increasing iterations')
shading interp% Gives the appearance of a smoother surface
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel = ('No. of Iterations');
ylabel = ('Function maximum');

Function Used:-

% Stalagmite Function
function [f] = stalagmite(input_vector)

a = input_vector(1);
b = input_vector(2);

f1 = (sin(((5.1)*pi*a)+(0.5)))^6;       % Term1
f2 = exp(-4*log(2)*((a-0.0667)^2)/0.64);% Term2
f3 = (sin(((5.1)*pi*b)+(0.5)))^6;       % Term3
f4 = exp(-4*log(2)*((b-0.0667)^2)/0.64);% Term4

[f] = -(f1*f2*f3*f4);% The function's negative sign will invert the stalagmite plot, resulting in the function's maxima

end

Explanation of code:-

1. Before starting the main code, I have used the commands. "clc" for clearing the command window, "clear all" for clearing all the previous variables, and "close all" for closing all the previous plots.

2. To begin, we'll create our own function to calculate the value of the objective function for various x and y values. We'll use the linspace command to do so. Using the meshgrid function, we now generate a matrix. Now we can use the values from the various matrices we've built as inputs to the function we've created by calling it. This allows us to determine the function's value at various x and y values. Now that we have the function's value at various values, we must compute the maximum value, which can be done with the help of the built-in function 'ga'. So for our first study, we will use the for loop and iterate it 50 times.

3. To evaluate the stalagmite function, for loop is used where 'i' is initialized and for every value of 'i' another for loop is used to initialize 'j'.

4. The 'xx' and 'yy' input vector values are defined, as well as the stalagmite function for these input vectors. This will result in a simple stalagmite function, which we can plot separately if necessary.

5. Following the preparation of the function's inputs, the following stage is to obtain the results for three situations of the genetic algorithm. In the first case or study, the function's maximum value is obtained with no limitations, bounds, or limits. This is accomplished via a for loop in which we utilize the 'ga' syntax to provide values for inputs and the best maximum value. In ga syntax, the stalagmite function is called, and the amount of variables 'nvars' is 2.

6. The 'tic' and 'toc' commands are used before and after the loop, respectively, to find out how long it takes to solve the loop for 50 iterations.

7. Then, using the 'surf' command for xx, yy, and function output, the function formed in the loop is plotted. The 'shading interp' command is used to ignore the surface gridlines and get a smooth and clean plot. The 'subplot(2,1,1)' command is used to plot many plots on a single figure while also dividing it into a 2x1 grid and placing the axes at 1.
8. The 'plot3' command shows 3-dimensional points with a 'o' shape and red colour at 'xopt, yopt, and optimum value of function'. After that, the labels and title are provided. The values of the optimum function 'fopt' are now presented for 50 iterations in the same graphic. Each time we run the programme, the outcome of these cases will be different.
9. To produce the same result every time, we must confine the programme to particular limitations, such as the upper bound and lower bound. This approach follows the same coding and graphing procedure as Study 1. The 'ga' syntax is the only change. This time, the method is given [0;0] and [1;1] as lower and higher limits, respectively.
10. When graphing the data for Study 2 or the bounded situation, it is clear that the results are not consistent. Increasing the number of examples or iterations is one solution to this problem.
11. So for Study 3, the number of iterations are increased to 400. So for this study, the syntax, 'options = optimoptions['SolverName','Name','Value')' is used which returns with named parameter 'Populations with the altered values 400.
12. The 'option' term is included in the 'ga' syntax, and the rest of the code is the same as in Studies 1 and 2.

Errors faced:-

Previously I have not installed the package for global optimization, that's why it is continuously giving this error - Undefined function or variable 'ga'.

Conclusion and Final results:-

Study 1 -

The random sampling of the genetic algorithm provides a new optimum value for the function every time the code is executed, as shown in this plot. This, however, is consistent with the concept of a Genetic Algorithm. A vast number of red dots represent the values of x and y coordinates, which change with each iteration.

Study 2 -

The results of this plot demonstrate the convergence of x and y values. In comparison to the previous plot, there are less red dots. This is the effect of giving the function upper and lower boundaries.

Study 3 -

The number of iterations is increased to 400 in this study in order to converge and locate the function's global maxima. On the stalagmite function, we can see that there is just one red dot, which is the required global maxima point.
References:-
https://en.wikipedia.org/wiki/Genetic_algorithm
https://in.mathworks.com/help/gads/ga.html
https://in.mathworks.com/discovery/genetic-algorithm.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.