Week 5 - Genetic Algorithm
Week 5 - Genetic Algorithm
PROBLEM STATEMENT
Try to understand the concept of genetic algorithm by referring to varius sources before you attend this challenge. Look out for information regarding stalagmite function and ways to optimise it.
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Make sure that your code gives the same output, even if it is made to run several times.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
SOLUTION WITH THE REPORT
The concept of Genetic Algorithm
A genetic algorithm is a natural technique for selecting the most suited people. The genetic algorithm is mostly used for reproduction in order to generate children for the following generation; this process of selecting and reproducing the fittest person was inspired by Charles Darwin, who termed it the "Theory of Evolution."

Natural selection begins with the selection of the fittest individuals in a group. They have kids who inherit the qualities of their parents and are passed on to the next generation. If parents are more fit, their children will be fitter than their parents and have a greater chance of survival. This procedure is repeated until a generation of the fittest individuals is discovered.
Five phases are considered in a genetic algorithm. They are
Initial population
Fitness function
Selection
Crossover
Mutation

Initial Population- the process begins with a set of individuals which is called a Population. An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a chromosome (solution).

Fitness function - To determine the fitness function we are comparing how fittest an individual is, basically we are comparing the individuals with one another. It gives a fitness score to each individual and based on the fitness score the individual will be selected for reproduction. To perform the optimization in GAs we need to optimize the stalagmite function to find its minima. these have two inputs (x,y).
Selection - The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation. Here, two pairs of individuals are selected according to their fitness score, higher will be the fitness the more chance is for the selection for reproduction.
Cross over - cross over is a process of taking two-parents' solutions and producing a child from them. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

now the new offsprings are added to the population.

Mutation- Mutation is used to carry out the genetic diversity from one generation of the population of chromosomes to the next generation. This mutation process is too important to perform because if it is not performed then the chromosomes will be too similar to each other. The mutation occurs to maintain diversity within the population and prevent premature convergence and results in local minima.

Advantages of Genetic Algorithms in MATLAB:
-
Finds Hard Solutions: Genetic Algorithms are good at solving tough problems with many possible solutions.
-
Works for Different Problems: They can help with many types of problems in different fields.
-
No Math Tricks Needed: GAs work even when the math is tricky or not clear.
-
Saves Time with Many Steps: They can do lots of calculations at the same time, which speeds things up.
-
Easy to Understand: They're based on how nature evolves, so they're easy to grasp.
Limitations of Genetic Algorithms in MATLAB:
-
Takes a Lot of Computer Work: They need a lot of computer power, especially for complex problems.
-
Can Be Slow to Finish: They might take a while to find answers, especially for big problems.
-
Needs Right Settings: You have to set some numbers just right, or they won't work well.
-
Can Get Lost: Sometimes they focus too much or too little, which can be a problem.
-
Might Not Get Best Answer: They don't always find the best solution, just a good one.
-
Doesn't Like Noise: Noise or sudden changes can confuse them.
-
No Guarantee for Best Answer: They might not find the very best solution.
Syntax For GA Algrorithm
Syntax 1:
x = ga(fun, nvars)
This syntax finds a local unconstrained minimum of the objective function fun, with nvars design variables. The output x is the solution vector that represents the values of the design variables that minimize the objective function.
Syntax 2:
x = ga(fun, nvars, A, b, Aeq, beq, lb, ub)
This syntax finds a solution within bounds for the design variables, while considering linear inequalities and equalities. Here's what each parameter means:
fun: The objective function to minimize.nvars: The number of design variables.A: Coefficients for linear inequalities (if any).b: Right-hand side values for linear inequalities.Aeq: Coefficients for linear equalities (if any).beq: Right-hand side values for linear equalities.lb: Lower bounds for each design variable.ub: Upper bounds for each design variable.
Syntax 3:
x = ga(fun, nvars, A, b, [], [], lb, ub, nonlcon, IntCon)
or
x = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, IntCon, options)
This syntax adds support for nonlinear constraints and integer variables. Here's what the additional parameters mean:
nonlcon: A function that represents nonlinear constraints. It takes a vector or array of variables and returns constraint values.IntCon: A vector of indices that specifies which design variables must take integer values.options: An optional argument for optimization options. It allows you to customize how the genetic algorithm behaves.
To provide more context, let's explain the parameters further:
A,b,Aeq,beq: These parameters define linear inequalities and equalities. If there are none, use empty matrices ([]).lb,ub: These are arrays specifying lower and upper bounds for each design variable. These bounds restrict the search space of the genetic algorithm.nonlcon: If you have nonlinear constraints, you can define a separate function for them. The function should accept the variable vector and return constraint values.IntCon: If you have variables that must be integers, specify their indices in this vector.options: This is an optional argument that allows you to customize the behavior of the genetic algorithm. You can set parameters related to population size, constraint tolerance, display options, etc.
The GA algorithm tries to find the best solution within the specified bounds and constraints by evolving a population of potential solutions over generations. The optimization process aims to minimize the given objective function while respecting the constraints.
Stalagmite function
Stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmite function is the mathematical function which generates similar shapes to stalagmites. Stalagmite function is a two-variable function, mathematically it is represented as: -

code of the stalagmite function
function [f] = 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((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2y = exp((((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)));
f1 = f1x*f2x*f1y*f2y;
f = -f1;
end
Main Optimisation code
%creating the search space to be optimized
%one dimensional
clear all
close all
clc
%Inputs
n = 150;
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating two dimensional grid
[xx , yy] = meshgrid(x,y);
tic
%Evaluating the stalagmite function
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
figure(1)
surfc(xx , yy , f )
shading interp
xlabel('x-value')
ylabel('y-value')
study_time = toc;