Week 5 - Genetic Algorithm
The Genetic Algorithm works on the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation. It is a method for solving both constrained and unconstrained optimization problems that is based on natural selection. The algorithm begins by creating a random initial population of individual selections and it keeps on modifying this population repeatedly creating a sequence of new population. The algorithm uses the individuals in the current generation to create the next population. 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.
Syntax for defining ‘ga’ (Genetic Algorithm) in matlab –
The ‘ga’ syntax in matlab is used to find the minimum of function using genetic algorithm. 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)
where,
x = optimal solution/variables;
fun = Objective function;
nvars = Number of variables;
Sometimes ‘[x,fval]’ is used instead of ‘x’ where ‘fval’
- x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub)
where,
A, B = 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,Aeq,Beq,lb,ub,nonlcon,IntCon,options)
where,
nonlcon = Nonlinear constraints
IntCon = Integer Values
options = Optimization options
If there are no nonlinear constraints and integer values, the nonlcon and IntCon are set to ‘[ ]’.
program code
%Define Stalagmite Function
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(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);
f = -(t1*t2*t3*t4);
end
program code
Then the ‘x’ array and ‘y’ array is defined using ‘linspace’ command to create values at 150 equal intervals
A 2D mesh of x and y array is created by using ‘meshgrid’ command and the number of iterations or cases that the code should perform is defined. This is given by n = 50.
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’.
The input vector values of ‘xx’ and ‘yy’ are defined and the stalagmite function for these input vector is defined. This will form a basic stalagmite function and we can plot it separately if needed.
After preparing the inputs needed for the function, next step is to obtain the results for 3 cases of the genetic algorithm. In first case or study, the maximum value of function is found out with no restrictions, no bounds and no limits.
This is done using ‘for’ loop where we use ‘ga’ syntax to give values for inputs and optimum max value. The stalagmite function is called in ‘ga’ syntax and number of variables ‘nvars’ are 2.
To know the time that is taken to solve the loop for 50 iterations, ‘tic’ and ‘toc’ command is used before and after the loop respectively.
Then the function created in the loop is plotted by using ‘surfc’ command for ‘xx, yy and function output’. To neglect the gridlines on the surface and obtain a smooth and clean plot, ‘shading interp’ command is used. The ‘subplot(2,1,1)’ is used to plot multiple plots on same figure which also divides figure in grid of 2x1 and axes to be placed at 1.
The ‘plot3’ command plots the 3D points at ‘xopt, yopt and optimum value of function’ with ‘o’ shaped and red colour. The labels and title are then provided. Now in the same figure, the values of optimum function ‘fopt’ are plotted for 50 iterations.
The results of the above case will be different for each time we run the program. To get same value each time, we need to restrict the program within specific bounds namely upper bound and lower bound. The procedure for coding and plottind this procedure is same as Study 1. The only difference is in the ‘ga’ syntax. This time the algorithm is provided lower limits and upper limits as [0;0] and [1;1] respectively.
After plotting the results for Study 2 or the bounded case, it is observed that the results are not same every time the program terminates. A solution to this problem is to increase the number of cases or iterations.
So for Study 3, the number of iterations are increased to 300. So for this study, the syntax, “options = optimoptions
- The syntax for ‘ga’ contains ‘option’ term and the rest of the code is same as for Study 1 and 2.
RESULTS :
Study 1 – Statistical Behaviour.

Study 2 – Statistical Behaviour with upper and lower bounds.
The above results show the convergence of x and y values. The number of red dots are less compared to previous plot. This is the result of providing upper and lower bounds to the function.
Study 3 – Statistical Behaviour with increasing iterations of GA.
In this study, the number of iterations is increased to 300 to converge and find the global maxima of the function. Here we can see that there is only one red dot on the stalagmite function and that is the required global maxima point.
On rotating the plot, we can clearly see the stalagmite function along with the global maxima value as x = 0.06683 and y = 0.06683.

Also the graph of ‘Function maximum vs No. of Iterations’ results that the value of optimum function ‘fopt’ is approximately equal to 1.
Workspace Values –
