Challenge

Week 5 - Genetic Algorithm

AIM:

To optimise the stalagmite function and find the global maxima of the function.

OBJECTIVES OF THE PROJECT:

Explaining the concept of genetic algorithm and also explain the syntax for ga in MATLAB.

Plot graphs for all 3 studies and for F maximum vs no. of iterations.

INTRODUCTION:

A Genetic Algorithm is a search experiential that is stimulated by Charles Darwin’s theory of natural evolution. This algorithm reproduces the process of natural selection where the appropriate individuals are selected for reproduction in order to produce offspring of the next generation.

The genetic algorithm is a technique for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm constantly modifies the population size of individual solutions.

Phases in Genetic Algorithm-

  1. Initialization of Population (Coding)

The code generates its random population of defined size and from that population the code evaluates the fitness of the members of the population according to which it forms new population based on no. of iterations performed.

  1. Fitness Function

It is that function which we have to optimize and its optimization is done with the help of number of generations which have been formed by the code accordance with the fitness value of each member generated, these fitness values are used for the reproduction of the children.

  1. Selection

The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation. The main goal of this phase is to find the region where the chances of getting the best solution are more.

Inspiration for this is from the survival of the fittest.

It should be a balance between exploration and exploitation of search space.

GA tries to move the genotype to higher fitness in the search space.

Too strong fitness selection bias can lead to sub-optimal solutions.

Too little fitness bias selection results in unfocused search.

  1. Reproduction

Generation of offspring happen in 2 ways:

Crossover

Mutation

  1. a) Crossover

Crossover is the most significant stage in the genetic algorithm. During crossover, a random point is selected while mating a pair of parents to generate offspring.

There are 3 major types of crossover.

Single Point Crossover: A point on both parents’ chromosomes is picked randomly, and designated a ‘crossover point’.

Two-Point Crossover: Two crossover points are picked randomly from the parent chromosomes.

Uniform Crossover: In a uniform crossover, typically, each bit is chosen from either parent with equal probability. The new offspring are added to the population.

  1. b) Mutation

In a few new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This indicates that some of the bits in the bit chromosome can be flipped. Mutation happens to take care of diversity among the population and stop premature convergence.

FLOW CHART:

Syntax for GA:

[x,fval ,exitflag ,output ,population ,scores]= ga(func,nvars,A,b,Aeq,Beq,lb,ub,nonlcon,Intcon,options)

x = optimum solution

exitflag = reason ga stopped

output= information about optimization process

population=final population

scores= final scores

f=ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

fun - fitness function.

nvars- number of variables,

A,b= Linear inequality constraints

Aeq,beq=Linear equality constraints

lb- Lower bound(limit),

ub- Upper bound(limit),

nonlcon : Use for nonlinear constraints

IntCon : to get solution in integer.

options- To set population size minimize with non-default options

BODY OF THE CONTENT:

  • FITNESS FNUCTION

  • The above equations are used in the stalagmite function to plot the graph in the main functions.
function [f] = stalagmite(input_vector)
          
            x = input_vector(1);
            y = input_vector(2);
 
            f1_x = (sin(5.1*pi*x + 0.5)).^6;
            f1_y = (sin(5.1*pi*y + 0.5)).^6;
 
            t1 = ((x-0.0667).^2)/(0.64);
            f2_x = exp(-4.*log(2)*t1);
 
            t2 = ((y-0.0667).^2)/(0.64);
            f2_y = exp(-4.*log(2)*t2);
 
            f = -(f1_x.*f2_x.*f1_y.*f2_y);
           
end
  • While using the function at the end as we are using the -ve sign so that the plot will be reversed as we have to find the maxima of the given function using ga.

CODING LOGIC :-

clear all
close all
clc

%defining the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%creating a 2D mesh
[xx,yy]=meshgrid(x,y);

%evaluating the stalgamite 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 value')
ylabel('y value')
shading interp
[inputs,fval]=ga(@stalagmite,2)
 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
 

RESULTS :-

OUTPUT

OUTPUT :-

OUTPUT:

CONCLUSION :-

ga is useful for optimizing the function but still the result are not accurate as the maximum value for the function lies at -1.

to overcome that using the given programme shown below

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.