Challenge

Week 4.1 - Genetic Algorithm

*Theory about 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.

  • 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.

Some phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation
  • 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.

  • 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.

  • 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.

  • 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.

  • 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 convergenc

  • Genetic Algorithm Stopping Criteria

It stops when it reaches the maximum number of generations; by default, this number is 100 times the number of variables. ga also detects if the best fitness value does not change for some time given in seconds (stall time limit), or for some number of generations (maximum stall generations).

  • Syntax for GA

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,Aeq,beq,lb,ub,nonlcon,intcon)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)

x = ga(problem)

[x,fval] = ga(___)

[x,fval,exitflag,output] = ga(___)

[x,fval,exitflag,output,population,scores] = ga(___)

  • Matlab code-
  • function code-
function [F]=stalagmite(input_vector)

x1=input_vector(1);
y1=input_vector(2);
f1_x=(sin((5.1*pi*x1)+0.5))^6;
f1_y=(sin((5.1*pi*y1)+0.5))^6;
f2_x=exp((-4*(log(2))*(x1-0.0667).^2)/.64);
f2_y=exp((-4*(log(2))*(y1-0.0667).^2)/.64);
F=-(f1_x*f1_y*f2_x*f2_y);
end
  • Main code
close all 
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
%creating a 2D 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
tic
%study1-statistical behavior
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(3,1,1)
hold on
surfc(xx,yy,-f)
shading interp
title('Unbounded inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(3,1,3)
plot(-fopt)
xlabel('Iteractions')
ylabel('function minimum')


%study2 - statistical behavior with upper and lower bounds
tic
for k=1:num_cases
    [inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
     xopt(k)=inputs(1);
     yopt(k)=inputs(2);
  
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
title('Bounded inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')        
subplot(2,1,2)
plot(-fopt)
xlabel('Iteractions')
ylabel('Function maximum')

%study3-Increasing GA iterations
option=optimoptions('ga');
options=optimoptions(option,'populationsize',200);
tic
for p=1:num_cases
    [inputs,fopt(p)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(p)=inputs(1);
    yopt(p)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteraction')
ylabel('function maximum')
    
  • Figure 1

  • Figure 2

  • Figure 3

  • Report-
  1. Firstly used the stalgmite function for different iteractions.Here we used -ve sign in stalagmite function,to calculate the global maxima
  2. Then in main code Used linspace command to create 150 values from 0 to 0.6.
  3. Used meshgrid command to evaluate the function of two variables.
  4. Study1-it calculate the global maxima by taking random samples from the function.Here we used tic-toc command for calculating the time taken for study-1.
  5. syntax for ga in study1 is [input,fopt(i)]=ga(@stalagmite,2).
  6. Study2-it calculate the global maxima by giving boundary limits for the inputs
  7. Syntax for ga in study2 is [inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); boundary limit-0 to1.
  8. Study3- it calculate the global maxima by giving population size by which we can find the specific optimum point.
  9. Syntax for ga in study3-[inputs,fopt(p)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);here options=optimoption('ga') options=optimoption(options,'population size',200) where population size is 200
  10. The study times are as follows-

    study1_time =

    2.5660

    study2_time =

    3.8350

    study3_time =

    8.2431

  • Refernces-

https://in.mathworks.com/help/gads/ga.html

https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3

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.