Join us on Facebook

Please wait..10 Seconds Cancel

1.03.2014

// // Leave a Comment

Genetic algorithm implementation In Matlab

 Genetic algorithm implementation


The genetic algorithm solves optimization problems by mimicking the principles of biological evolution, repeatedly modifying a population of individual points using rules modeled on gene combinations in biological reproduction. Due to its random nature, the genetic algorithm improves your chances of finding a global solution. It enables you to solve unconstrained, bound-constrained, and general optimization problems, and it does not require the functions to be differentiable or continuous.

Matlab provides several build-in functions and tools to simulate Genetic Algorithm. ga is a command-line function that solves a given problem using Genetic Algorithm while gatool is a graphical tool that provides UI for solving the same.

In order to use any of the alternatives, first of all a fitness function for the problem at hand is required which is defined as follows:



Create function file “fitness1.m” :

function [z] = fitness1( x )
z=x*x

end


Firing the following command in the matlab console will find the minimum of the fitness1 function using GA. The range in which ga operates will be selected randomly and the minimum value of x will be displayed.

x = ga (@fitness1, 1)
z = 0.2560

z = 0.4887

Firing the following command in the matlab console will provide the minimum value of x as explained previously. Along with it other parameters like fval, reason, output and message are also displayed.

Fval provides the value of fitness function at the minimum value of x.

Reason provides the reason for stopping the algorithm.

Output provides information about the problem type, no of generations, function count (total function evaluations) and message (reason for stopping the algorithm).

Population provides the final population generated by the algorithm. By default the population count is 20.

Scores provides the scores (fitness values) related to all the population.
[x, fval,  reason, output, population, scores ]= ga (@fitness1, 1)
x =     0.0022

fval =           4.6955e-006

reason =             1

output =
           problemtype: 'unconstrained'
           rngstate: [1x1 struct]
           generations: 51
          funccount: 1040
             message: [1x86 char]



population =
    0.0022
    0.2215
    0.0022
    0.0452
    0.0022
    0.0632
    0.0022
    0.0022
    0.8862

scores =
    0.0000
    0.0000
    0.7853
    0.0000
    0.0000
    0.1738
    0.0032
    0.3212
    0.0371





GATOOL (Optimization tool) :
          After type “gatool” in command window of matlab, it will redirect to the optimization tool as follows:



Fitness Function - Select ga - Genetic Algorithm in the solver field. Afterwards, declare the fitness function which is saved in function file e.g. fitness1.m. This file name will be specified by typing “@” before it. Also mention the number of variables used by it.


       ction :nt crossover, two-point crossover, etc.lation contains the possible solutions of the given problem for the searchPopulation Population option provides the population used by the genetic algorithm. The population contains the possible solutions of the given problem for the large search space.

Population Type - option specifies options for the representation of the population (encoding) of the genetic algorithm. Encoding can be either bit strings or double vector. Population type specifies the type of the input to the fitness function.

Population Size - option specifies the size of the population to be used by the algorithm. By default this value is set to 20.

Creation function – option specifies the function that creates the initial population. Uniform creates a random initial population with a uniform distribution.

Initial population - enables to specify an initial population for the genetic algorithm. If an initial population is not specified, the algorithm creates one using the Creation function.

Initial range - specifies lower and upper bounds for the entries of the vectors in the initial population. The first row contains lower bounds for the entries of the vectors in the initial population, while the second row contains upper bounds.



Fitness Scaling - Scaling function specifies the function that performs the scaling. For example, rank selection scales the raw scores based on the rank of each individual, rather than its score. The rank of an individual is its position in the sorted scores. The rank of the fittest individual is 1, the next fittest is 2, and so on. Rank fitness scaling removes the effect of the spread of the raw scores.



Selection specifies the function for selection process. Several selection process are used for solving a problem using GA like roulette wheel selection, tournament selection, stochastic uniform, etc. Selection algorithm is generally selected on random base.



Reproduction Reproduction options determine how the genetic algorithm creates children at each new generation.



Elite count – specifies the number of individuals that are guaranteed to survive to the next generation.

Crossover fraction – specifies the fraction of the next generation that crossover produces.

MutationMutation functions make small random changes in the individuals in the population, which provide genetic diversity and enable the genetic algorithm to search a broader space.



Use constraint dependent default chooses:
1.   Gaussian if there are no constraints
2.    Adaptive feasible otherwise

Crossover – combines two individuals, or parents, to form a new individual, or child, for the next generation. Many different methods are available like single point crossover, two-point crossover, etc.











0 comments:

Post a Comment