Genetic Algorithm Analysis using MATLAB
Aim:To optimize the stalagmite function and find the global maxima of the function.
Introduction: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.
Notion of Natural selection:
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.
This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.
Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
1.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.
2.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.
3.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.
4.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.
5.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.
6.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 for ga and its explanation:
Syntax:
x = ga(fun,nvars)x = ga(fun,nvars,A,b)x = ga(fun,nvars,A,b,Aeq,beq)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)Explanation: finds a local unconstrained minimum, x = ga(fun,nvars)x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
finds a local minimum x = ga(fun,nvars,A,b)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').
finds a local minimum x = ga(fun,nvars,A,b,Aeq,beq)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').
defines a set of lower and upper bounds on the design variables, x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
subjects the minimization to the constraints defined in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)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.)
minimizes with the default optimization parameters replaced by values in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) requires that the variables listed in x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)IntCon take integer values.
[, for any previous input arguments, also returns x,fval] = ga(___)fval, the value of the fitness function at x
Matlab Program:
clear all
close all
clc
%First we define the search space
p=linspace(0,.6,150);
k=linspace(0,.6,150);
iterations=50;
%Creating a 2D mesh
[xx,yy]=meshgrid(p,k);
%Now the stalagmite function evaluation
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 %tic and toc is used to determine how much time it takes for one study
for i=1:iterations
[inputs,f_optimum(i)]=ga(@stalagmite,2);
x_optimum(i)=inputs(1);
y_optimum(i)=inputs(2);
end
study_1_time=toc
%Plotting
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x_optimum,y_optimum,f_optimum,'marker','o','markersize',6,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(f_optimum)
xlabel('Iterations')
ylabel('Function maximum')
% Study-2-Statistical behaviour with upper and lower bounds
tic
for i=1:iterations
[inputs,f_optimum(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
x_optimum(i)=inputs(1);
y_optimum(i)=inputs(2);
end
study_2_time=toc
%Plotting
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x_optimum,y_optimum,f_optimum,'marker','o','markersize',6,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(f_optimum)
xlabel('Iterations')
ylabel('Function maximum')
%Study-3-Increasing ga iterations
options=optimoptions('ga');
options=optimoptions(options,'populationsize',200)
tic
for i=1:iterations
[inputs,f_optimum(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
x_optimum(i)=inputs(1);
y_optimum(i)=inputs(2);
end
Study_3_time=toc
%Plotting
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x_optimum,y_optimum,f_optimum,'marker','o','markersize',6,'markerfacecolor','r')
title('Bounded inputs with modified popultion size')
subplot(2,1,2)
plot(f_optimum)
xlabel('Iterations')
ylabel('Function Maximum')
%This is Stalagmite Function
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
Procedure:1.First the search space is defined onto which the study will takes place.
2.Now we create a 2Dmesh with the help of meshgrid.
3.After that the we define stalagmite function for different inputs.
4.Now comes optimization study in study1 statistical behaviour is observed.This study is not helpful as it shows many points.
5.Now the 2nd study have some modifications with upper and lower bounds.But in this also there are high range bw max values.
6.Now the third is modification of second study but in this the range ia not high and more accurate than second one.
Result:According to Study-1 following plot is obtained:
According to Study-2 following plot is obtained:
According to Study-3 following plot is obtained:
Conclusion:1.In study 1,the values are more varying and range is so high and there are different maximas there.So we need some modification.
2.In study2,the values are not so varying but there is some variations and the range is also high.
3.In study3,there is a value which is almost constant and the max points are also three.But it takes more time for evaluation.
Errors:Just a spelling mistake is done: