Challenge

Week 4.1 - Genetic Algorithm

AIM  :   Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

GOVERNING EQUATION :

   f(x,y) = f1,x f2,x f1,y f2,y

`f1,x = [sin(5.1pix + 0.5 )]^6`

`f1,y = [sin(5.1piy + 0.5 )]^6`

`f2, x = exp[-4ln(2) ((x-0.0667)^2/0.64]`

`f2,y = exp[-4ln(2) ((y-0.0667)^2/0.64]`

OBJECTIVE : 

  • To write a matlab program to optimise the stalagmite function.
  • To find the global maxima.
  • To explain the concept of genetic algorithm (ga).
  • Explaining syntax for ga .
  • Ploting the graph of all the studies.

GOVERNING BODY :

INTRODUCTION OF GENETIC ALGORITHM(GA)

  • Genetic algorithm is a process of natural selection that belongs to the larger class of evolutionary algorithms(EA).
  • It is based on the concept of Darwin's theory of 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 the 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 better fitness,their offspring will be better than parents and have better chance at surviving. This process keep on iterating and at the end, a generation with the fittest individuals will be found.
  • This can be applied for the search problem and to generate high-quality solutions to optimization.

     -Five phases are cinsidered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. selection
  4. crossover
  5. 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).

2 )  Fitness Function :

The fitness function determines how fit an individual is . 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 with (parents) are selected based on their fitness scores. Individual 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.

5 ) Mutation :

 In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability.

                                                         FLOW CHART OF GENETIC ALGORITHM.

                                             

       SYNTAX OF GENETIC ALGORITHM

x = ga (fun , nvars)

x = ga (fun , nvars, A ,b)

x = ga (fun , nvars, A ,b,Aeg , beq)

x = ga (fun , nvars, A ,b,Aeg , beq , lb ,ub)

x = ga (fun , nvars, A ,b,Aeg , beq , lb ,ub , nonlcon)

 x = ga (fun , nvars, A ,b,Aeg , 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 )

Where , 

 fun - function name

nvars - Number of variables

A - linear inequality constraints as real matrix

b - linear inequality constraints as real vector

Aeq - linear equality constraints as real matrix

beq - linear equality constraints as real vector

lb - lower boundary

ub - upper boundary

nonlcon -  non linear constraints

option - creating optimisation option using 'optimoptions'

Stopping criteria :

  • GENERATIONS -   The algorithm stops when the number of generations reaches the valve of generations.
  • TIME LIMIT  -      The algorithm stops after running for an amount of time in seconds equal to time time limits.
  •  FITNESS LIMITS -    The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal fitness value.

CODE EXPLAINATION :

  • Firstly a random array of x and y values were created and the corresponding 2D array was obtained using meshgrid command.
  • A nested loop was used to assign values fro this array into an array named input_vector.
  • The stalagmite function is created in another command window.
  • minus (-) sign is given to the output of stalagmite function so that , after ga process it will get the global maxima.
  • Tic - toc command is used to get running time of each case.

here is matlab programming for stalagmite function

% program of get_stalagmite function

function [f] = get_stalagmite(input_vector)

%inputs
x = input_vector(1) ;
y = input_vector(2);
 

% solution of the function

%sf1 = f1,x
%sf2 = f1,y
%sf3 = f2,x
%sf4 = f2,y


sf1 = (sin((5.1*pi*x)+0.5))^6;
sf2 = (sin((5.1*pi*y)+0.5))^6;
sf3 = exp (-4*log(2)*((x-0.0667)^2/0.64));
sf4 = exp (-4*log(2)*((y-0.0667)^2)/0.64);

f = -(sf1*sf2*sf3*sf4);

end

CASE STUDY 

1 ) STATISTICAL BEHAVIOUR WITH UNBOUNDED CONDITION : 

%total number of itreations
num_cases = 50


%creating 2D array using meshgridfunction
[xx yy] = meshgrid(x,y);


% Nested loop for array value input

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) = get_stalagmite(input_vector);
end
end

% study 1 - statisticsl behaviour with unbounded inputs

tic
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@get_stalagmite,2);
    xopt(i)= inputs(1);
    yopt(i) = inputs(2);
end

study_time_1 = toc

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Statistical Behavior with Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function maximum')

2) STATISTICAL BEHAVIOUR WITH BOUNDED CONDITION :

% study 2 - statistic behaviour with upper and lowwer bound inputs

for k = 1:num_cases
    [inputs2 , fopt2(k)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1] );
    xopt2(k) = inputs2(1);
    yopt2(k) = inputs2(2);
end

study_time_2 = toc

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
grid on
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
xlabel('iterations')
ylabel('function maximum')
title('statical behaviour with upper and lower bound')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('Function maximum')

3) STATISTICAL BEHAVIOUR WITH BOUNDED CONDITION  AND INCREASING NO . OF POPULATION:

% study case 3 - increasing population size of ga

options = optimoptions('ga');
options = optimoptions(options, 'PopulationSize',650);

tic 
for m = 1:num_cases
    [inputs3, fopt3(m)]=ga(@get_stalagmite,2,[] ,[], [] ,[] ,[0:0],[1;1],[],[],options);
    xopt3 = inputs3(1);
    yopt3 = inputs3(2);
end
study_time_3 = toc

% plotting

figure (3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
grid on
plot3(xopt3,yopt3,-fopt3,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('statistical Behavior with Increased Iterations')
subplot(2,1,2)
plot(-fopt3)
xlabel('iteratios')
ylabel('function maximum ' )

CONCLUSION :

Case time increases as number of population increases but the result we  get will be more precise.

References :

skill - lync projects

chorme 

youtube

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.