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.
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
INITIAL POULATION
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.
FITNES 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.
CROSS OVER
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.
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.
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.
PROBLEM DESCRIPTION:
The given problem is to maximise the stalagmite function:
f1=(sin(5.1*3.14*x + 0.5))^6;
f2=(sin(5.1*3.14*z+ 0.5))^6;
f3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f4=exp(-4*log(2)*((z-0.0667)^2)/0.64);
z=f1*f2*f3*f4;
APPROACH:
As genetic algorithm only minimises a function in matlab we use new function f= 1/(1+z); Minimising f will maximise Z.We conduct 3 studies with genetic algorithm each study showing how to reach the optimum solution
CODE:
Stalagmite function definition:
function y = stalgamite(a)
x=a(1,1);
z=a(1,2);
f1=(sin(5.1*3.14*x + 0.5))^6;
f2=(sin(5.1*3.14*z+ 0.5))^6;
f3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f4=exp(-4*log(2)*((z-0.0667)^2)/0.64);
y=1/(1+(f1*f2*f2*f4));
end
GA CODE:
study 1:Using genetic algorithm to find minimum value of stalgamite and maximum value from ymaximum from the same.
study2 :minimising stalgamite with in the bounds
study3 :minimising stalagmite with bounds and options
The syntax for GA is :
[optx optz]= ga(@stalgamite,2,[],[],[],[],[0,0],[.3;.3],[],[],options);
here stalagmite is our function that we defined.
options are used to define population size:
options=optimoptions('ga','PopulationSize',200);..................here a population size of 200 is chosen
[0,0],[.3;.3] are the bounds that GA has to find solution in.
%defining population bounds
x1=linspace(0,0.6,150);
y1=linspace(0,0.6,150);
[xx yy]=meshgrid(x1,y1);
%finding stalgamite function value within the given bound
for i=1:length(x1)
for j=1:length(y1)
f(i,j)=stalgamite([x1(i) y1(j)]);
end
end
%Finding the actual function to be maximised
g=f.^(-1)-1;
%study 1 Using genetic algorithm to find minimum value of stalgamite and maximum
%value from ymaximum from the same
for k=1:20
[optx optz]= ga(@stalgamite,2);
ymaximum(k)=(1/optz)-1;
x(k)=optx(1);
y(k)=optx(2);
end
%defining figure 1 surface plot and optimum values
figure(1)
hold on
plot3(x,y,ymaximum)
surfc(xx,yy,g)
shading interp;
title('fitness vs variable');
xlim([0 5]);
ylim([0 5]);
xlabel('x1');
ylabel('y1');
zlabel('z1');
% study 2 minimising stalgamite with in the bounds
options=optimoptions('ga','populationsize',170);
for k=1:20
[optx optz]= ga(@stalgamite,2,[],[],[],[],[0;0],[1;1]);
ymaxi(k)=(1/optz)-1;
l1(k)=optx(1);
l2(k)=optx(2);
end
%plotting maximum values and visualising with surface plot
figure(2)
subplot(2,1,1)
hold on
plot3(l1,l2,ymaxi)
surf(xx,yy,g)
shading interp;
xlim([0 0.9]);
ylim([0 0.9]);
title('fitness vs variable');
xlabel('x1');
ylabel('y1');
zlabel('z1');
subplot(2,1,2)
plot(linspace(1,20,20),ymaxi);
xlabel('number of iterqations')
ylabel('maximum value')
%study 3 minimising stalagmite with bounds and options
options=optimoptions('ga','PopulationSize',200);
for k=1:20
[optx optz]= ga(@stalgamite,2,[],[],[],[],[0,0],[.3;.3],[],[],options
);
ymaxi3(k)=(1/optz)-1;
m1(k)=optx(1);
m2(k)=optx(2);
end
figure(3)
subplot(2,1,1)
hold on
plot3(m1,m2,ymaxi3)
surf(xx,yy,g)
shading interp;
xlim([0 0.9]);
ylim([0 0.9]);
title('fitness vs variable');
xlabel('x1');
ylabel('y1');
zlabel('z1');
subplot(2,1,2)
plot(linspace(1,20,20),ymaxi3);
xlabel('number of iterqations')
ylabel('maximum value')
Here i have used subplots to visualise results in each study.In each subplot upper plot has a surface plot of stalagmite and optimum points chosen by GA. I have iterated the solution for 20 times.In study 1 it is observed that the solutions are hugley different in each iteration.
Study 2 is done by providing bound for GA [0;0],[1;1] and solutions are more nearer but with no repeatability.
study3 shows far more better results than study 1 and 2.
RESULTS:
study 1.
A seen the results are fluctuating very much and dont converge to the same solution

Study2:

Study 3:

ERRORS:
giving the wrong function input to GA.

using the wrong matrix dimension for input for stalgamite function
corrected the errors afters they popped up in command window.