Week 4.1 - Genetic Algorithm
Aim: Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Objective:
- Explain the concept of genetic algorithm.
- Use and expalin the syntex ga in Matlab.
Theory:
Equation

Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.
Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.
In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.
In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.
Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.
Advantages of GAs
GAs have various advantages which have made them immensely popular. These include −
-
Does not require any derivative information (which may not be available for many real-world problems).
-
Is faster and more efficient as compared to the traditional methods.
-
Has very good parallel capabilities.
-
Optimizes both continuous and discrete functions and also multi-objective problems.
-
Provides a list of “good” solutions and not just a single solution.
-
Always gets an answer to the problem, which gets better over the time.
-
Useful when the search space is very large and there are a large number of parameters involved.
Limitations of GAs
Like any technique, GAs also suffer from a few limitations. These include −
-
GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.
-
Fitness value is calculated repeatedly which might be computationally expensive for some problems.
-
Being stochastic, there are no guarantees on the optimality or the quality of the solution.
-
If not implemented properly, the GA may not converge to the optimal solution.
Basic Terminology
Before beginning a discussion on Genetic Algorithms, it is essential to be familiar with some basic terminology which will be used throughout this tutorial.
-
Population − It is a subset of all the possible (encoded) solutions to the given problem. The population for a GA is analogous to the population for human beings except that instead of human beings, we have Candidate Solutions representing human beings.
-
Chromosomes − A chromosome is one such solution to the given problem.
-
Gene − A gene is one element position of a chromosome.
-
Allele − It is the value a gene takes for a particular chromosome.

-
Genotype − Genotype is the population in the computation space. In the computation space, the solutions are represented in a way which can be easily understood and manipulated using a computing system.
-
Phenotype − Phenotype is the population in the actual real world solution space in which solutions are represented in a way they are represented in real world situations.
-
Decoding and Encoding − For simple problems, the phenotype and genotype spaces are the same. However, in most of the cases, the phenotype and genotype spaces are different. Decoding is a process of transforming a solution from the genotype to the phenotype space, while encoding is a process of transforming from the phenotype to genotype space. Decoding should be fast as it is carried out repeatedly in a GA during the fitness value calculation.
For example, consider the 0/1 Knapsack Problem. The Phenotype space consists of solutions which just contain the item numbers of the items to be picked.
However, in the genotype space it can be represented as a binary string of length n (where n is the number of items). A 0 at position x represents that xth item is picked while a 1 represents the reverse. This is a case where genotype and phenotype spaces are different.

-
Fitness Function − A fitness function simply defined is a function which takes the solution as input and produces the suitability of the solution as the output. In some cases, the fitness function and the objective function may be the same, while in others it might be different based on the problem.
-
Genetic Operators − These alter the genetic composition of the offspring. These include crossover, mutation, selection, etc.
Basic Structure
The basic structure of a GA is as follows −
We start with an initial population (which may be generated at random or seeded by other heuristics), select parents from this population for mating. Apply crossover and mutation operators on the parents to generate new off-springs. And finally these off-springs replace the existing individuals in the population and the process repeats. In this way genetic algorithms actually try to mimic the human evolution to some extent.
Each of the following steps are covered as a separate chapter later in this tutorial.

Why We use it:
- Genetic Algorithm is fast and result base solution technique.
- It not require any mathematical expression.
- Simple and optimaztized technique to find output.
- IT give perfect solution.
Syntax ga:
x= ga(fun,nvars) :finds a local unconstrained minimum,x, to the objective function,fun.nvarsis the dimension (number of design variables) offun.
x= ga(fun,nvars,A,b) :finds a local minimumxtofun, subject to the linear inequalitiesA*x≤b.gaevaluates the matrix productA*xas ifxis transposed (A*x').
x= ga(gun,nvars,A,b,Aeq,beq) :finds a local minimumxtofun, subject to the linear equalitiesAeq*x=beqandA*x≤b. (SetA=[]andb=[]if no linear inequalities exist.)gaevaluates the matrix productAeq*xas ifxis 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 rangelb≤x≤ub. (SetAeq=[]andbeq=[]if no linear equalities exist.)
x= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) :subjects the minimization to the constraints defined innonlcon. The functionnonlconacceptsxand returns vectorsCandCeq, representing the nonlinear inequalities and equalities respectively.gaminimizes thefunsuch thatC(x)≤0andCeq(x) = 0. (Setlb=[]andub=[]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 inoptions. (Setnonlcon=[]if no nonlinear constraints exist.) Createoptionsusing optimisation.
x= ga(fun,nvars,A,b.Aeq,beq,lb,ub,nonlocon,intcon) :orx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) requires that the variables listed inintcontake integer values.
x= ga(fun,nvars,A,b.Aeq,beq,lb,ub,nonlocon,intcon,options) :
x= ga(problem) : finds the minimum forproblem, a structure described in problem.
[x,fval] = ga(____) : for any previous input arguments, also returnsfval, the value of the fitness function atx.
clear all
close all
clc
%defining search space
x = linspace(0 , 0.6 , 150);
y = linspace(0 , 0.6 , 150);
% Creating 2 Dimensional Mesh
[xx yy] = meshgrid(x,y)
%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
surfc(xx,yy,-f)
xlabel('x values')
ylabel('y values')
shading interp
[x,fval] = ga(@stalagmite,2)
Output:

2)
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%creating 2 dimensional mesh
[xx,yy] = meshgrid(x,y);
%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
%study 1 - Statistical Behaviour
tic
for i = 1:num_cases
[inputs , fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('function value')
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function maximum')
%Study 2 - Stastical Behaviour - With Upper & Lower Bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1:1]);
xopt(i) =inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
xlabel('x-value')
ylabel('y-value')
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
%study 3 -Incresing GA Interactions
options = optimoptions('ga')
options = optimoptions(options,'populationSize',150);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x-value')
ylabel('y-value')
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with increasing ga interactions')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maxima')
Output:
study1

Study 2

Study 3

Function Code:
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1 = (sin(5.1*pi*x+0.5)).^6;
f2 = (sin(5.1*pi*y+0.5)).^6;
f3 = exp((-4*log(2)*(x-0.0667).^2)/(0.64));
f4 = exp((-4*log(2)*(y-0.0667).^2)/(0.64))
f = -(f1*f2*f3*f4);
end
Errors:



Code Explanation:
- Start with clear all,close all,clc to clear all content stored.
- linspace command is used to define 150x & y values between 0 to 0.6 with 50 num_cases to run 50 times.
- meshgrid command is used to form two dimensional array.
- for loop is excutated to evaluate the stalagmite function.
- For Study 1 - statistical behaviour for loop is executed,During execution stalagmite function is called 'ga'.Plotting is done,tic and toc coomand use to measure time taken to perform program.
- For Study 2 - statistical behaviour with upper and lower bounds for loop is executed,During execution stalagmite function is called 'ga'.Plotting is done,tic and toc coomand use to measure time taken to perform program.
- For Study 3 - statistical behaviour with increasing itration for loop is executed,During execution stalagmite function is called 'ga'.To show population size we use 'optimoptions' function.
- 'stalagmite(input_vector)' std function to find main program.
Refrences: