Week 4.1 - Genetic Algorithm
Genetic Algorithm: -
- A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
- The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation.
- If parents have better fitness, their offspring will be better than parents and have a better chance at surviving.
- This process keeps on iterating and at the end, a generation with the fittest individuals will be found.
-
Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
Initial Population: -
- The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
- An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
- In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.

Fitness Function: -
- The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals).
- It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
Selection: -
- The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
- Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
Crossover: -
- Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
-
For example, consider the crossover point to be 3 as shown below.

- Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

-
The new offspring are added to the population.

Mutation: -
- In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.
- Mutation occurs to maintain diversity within the population and prevent premature convergence.

Termination: -
The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
Syntax of GA: -
x = ga(fun, nvars)
finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
x = ga(fun, nvars, A,b)
finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
x = ga(fun, nvars, A,b, Aeq, beq)
finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub)
defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon)
subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon, options)
minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon, intcon)
x = ga(fun, nvars, A,b, Aeq, beq, lb, ub, nonlcon, intcon, options)
x = ga(problem)
finds the minimum for problem, a structure described in problem.
[x, fval] = ga(_)
for any previous input arguments, also returns fval, the value of the fitness function at x.
[x, fval, exitflag, output] = ga(_)
also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.
[x, fval, exitflag, output, population, scores] = ga(_)
also returns a matrix population, whose rows are the final population, and a vector score, the scores of the final population.
Program for creating stalagmite Function: -
%% Creating a function to obtain global maxima for the given Objective Function
function [F_x] = stalagmite_func(input_vector)
% x,y are the design variables
x = input_vector(1);
y = input_vector(2);
% F_x is the objective function where F_1, F_2, F_3, F_4 are the components of the function
F_1 = (sin(5.1*pi*x+0.5))^6;
F_2 = (exp(-4*log(2)*((x-0.0667)^2)/0.64));
F_3 = (sin(5.1*pi*y+0.5))^6;
F_4 = (exp(-4*log(2)*((y-0.0667)^2)/0.64));
F_x = -(F_1*F_2*F_3*F_4); % -ve sign used to minimize the function
end
Program to obtain global Minima of the function: -
%% Substituting the Inputs to get the gobal maxima
clear all
close all
clc
%% Inputs
% taking the Inital values of x, y using linspace
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Giving number of iterations
num_iter = 50;
%% Converting the x, y values into 2d Array using Meshgrid
[xx yy] = meshgrid(x,y);
%% Case 1: substituting the x, y values in function Using for loop without ga
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_func(input_vector);
end
end
% Plotting
figure(1)
surfc(xx,yy,f)
shading interp
%% Case 2: applying GA algorithm for the function and substituting the x, y values
tic
for i = 1:num_iter
[results, fopt(i)]= ga(@stalagmite_func,2);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time = toc
% Plotting the optimum value of x, optimum value of y & function optimum
figure(2)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('maximum Function')
%% Case 3: Giving the lower bounds & upper bounds
tic
for i = 1:num_iter
[results, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time1 = toc
% Plotting the optimum value of x, optimum value of y & function optimum
figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('maximum Function')
%% Case 4: Initializing the population size using optimoptions for GA
options = optimoptions('ga','PopulationSize',300)
tic
for i = 1:num_iter
[results, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = results(1);
yopt(i) = results(2);
end
study_time2 = toc
% Plotting the optimum value of x, optimum value of y & function optimum
figure(4)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('maximum Function')
Explanation of code: -
- For the given objective function, we have to obtain the global minima/maxima. To obtain the global minima/maxima there should some design variables by which we can vary the boundary conditions of the design variables to get the global maxima/minima.
- So, we are considering two design variables ('x, y').
- The initial values of x, y is defined using linspace(0,0.6,150). Were the values are taken linear/ equal from 0 to 0.6 up to 150 values.
- Considering number of iterations as 50; which can be varied.
- as the x, y values in a 1d array we are transforming it to 2d array format using meshgrid command.
- Now for the case 1 we are just plotting the function without using GA algorithm. and we used for loop to plot the function.
- surfc command is used were it plots with contours, shading interp command used to remove the mesh formed on the plot.
- Coming to the case 2 we are introducing GA algorithm for optimization.
- By using for loop were i = num_iter i.e. 50; and the syntax of GA is defined to get the xoptimum, yoptimum. and we did not define the bounds, population size.
- Now we plot the case 2 values of xopt, yopt, fopt using surfc, plot3 commands.
- By executing this we can observe the plot that the values are beyond the optimization function which cannot be considered as better optimization.
- As we saw that the values obtained int the plot are messing up and beyond the limit now in the case 3 we are defining the bonds in the syntax of GA i.e. [results, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
- Now plotting the case 3 we can find the plot some optimistic also it did not find the global minima of the function.
- So, in Case 4 to get the gobal minima of the function we are now defining the population size about 250 which is initiated in the optimoptions, and this is initiated in syntax of GA i.e. [results, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
- By plotting the case 4 we found the global optimum of the function and the plot is very clear and we can say it as a better optimization.
Plotting: -
Case 1

Case 2: -

Case 3: -

Case 4: -

