Genetic Algorthim
Genetic Algorthim
The genetic algorithm is a method for solving both constrained and unconstrained
optimization problems that is based on natural selection, the process that drives biological
evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At
each step, the genetic algorithm selects individuals at random from the current population to
be parents and uses them to produce the children for the next generation. Over successive
generations, the population "evolves" toward an optimal solution. Genetic algorithm can be
applied to solve a variety of optimization problems that are not well suited for standard
optimization algorithms, including problems in which the objective function is discontinuous,
non-differentiable, stochastic, or highly nonlinear.
The genetic algorithm uses three main types of rules at each step to create the next generation
from the current population:
Selection rules select the individuals, called parents, that contribute to the population at the
next generation.
Crossover rules combine two parents to form children for the next generation.
Mutation rules apply random changes to individual parents to form children.
The genetic algorithm differs from a classical, derivative-based, optimization algorithm in
two main ways, as summarized in the following table.
Fitness Functions
The fitness function is the function you want to optimize. For standard optimization
algorithms, this is known as the objective function. The optimtool in the Matlab software tries
to find the minimum of the fitness function.
Individuals
An individual is any point to which can be applied to the fitness function. The value of the
fitness function for an individual is its score. An individual is sometimes referred to as
a genome and the vector entries of an individual as genes.
Populations and Generations
A population is an array of individuals. For example, if the size of the population is 100 and
the number of variables in the fitness function is 3, you represent the population by a 100-by-
3 matrix. The same individual can appear more than once in the population. For example, the
individual (2, -3, 1) can appear in more than one row of the array.
At each iteration, the genetic algorithm performs a series of computations on the current
population to produce a new population. Each successive population is called a
new generation.
Diversity
Diversity refers to the average distance between individuals in a population. A population has
high diversity if the average distance is large; otherwise it has low diversity. In the following
figure, the population on the left has high diversity, while the population on the right has low
diversity.
Diversity is essential to the genetic algorithm because it enables the algorithm to search a
larger region of the space.
Fitness Values and Best Fitness Values
The fitness value of an individual is the value of the fitness function for that individual.
Because the toolbox software finds the minimum of the fitness function, the best fitness value
for a population is the smallest fitness value for any individual in the population.
Parents and Children
To create the next generation, the genetic algorithm selects certain individuals in the current
population, called parents, and uses them to create individuals in the next generation,
called children. Typically, the algorithm is more likely to select parents that have better
fitness values.
In this example, the initial population contains 20 individuals, which is the default value
of Population size in the Population options. Note that all the individuals in the initial
population lie in the upper-right quadrant of the picture, that is, their coordinates lie between
0 and 1, because the default value of Initial range in the Population options is [0;1].
If you know approximately where the minimal point for a function lies, you should set Initial
range so that the point lies near the middle of that range. For example, if you believe that the
minimal point for Rastrigin's function is near the point [0 0], you could set Initial range to
be [-1;1]. However, as this example shows, the genetic algorithm can find the minimum even
with a less than optimal choice for Initial range.
Creating the Next Generation
At each step, the genetic algorithm uses the current population to create the children that
make up the next generation. The algorithm selects a group of individuals in the current
population, called parents, who contribute their genes—the entries of their vectors—to their
children. The algorithm usually selects individuals that have better fitness values as parents.
You can specify the function that the algorithm uses to select the parents in the Selection
function field in the Selection options.
The genetic algorithm creates three types of children for the next generation:
Elite children are the individuals in the current generation with the best fitness values. These
individuals automatically survive to the next generation.
Crossover children are created by combining the vectors of a pair of parents.
Mutation children are created by introducing random changes, or mutations, to a single
parent.
The following schematic diagram illustrates the three types of children.
Mutation and Crossover explains how to specify the number of children of each type that the
algorithm generates and the functions it uses to perform crossover and mutation.
The following sections explain how the algorithm creates crossover and mutation children.
Crossover Children
The algorithm creates crossover children by combining pairs of parents in the current
population. At each coordinate of the child vector, the default crossover function randomly
selects an entry, orgene, at the same coordinate from one of the two parents and assigns it to
the child. For problems with linear constraints, the default crossover function creates the
child as a random weighted average of the parents.
Mutation Children
The algorithm creates mutation children by randomly changing the genes of individual
parents. By default, for unconstrained problems the algorithm adds a random vector from a
Gaussian distribution to the parent. For bounded or linearly constrained problems, the child
remains feasible.
The following figure shows the children of the initial population, that is, the population at the
second generation, and indicates whether they are elite, crossover, or mutation children.
Plots of Later Generations
The following figure shows the populations at iterations 60, 80, 95, and 100.
As the number of generations increases, the individuals in the population get closer together
and approach the minimum point [0 0].
Stopping Conditions for the Algorithm
The genetic algorithm uses the following conditions to determine when to stop:
Generations — The algorithm stops when the number of generations reaches the value
of Generations.
Time limit — The algorithm stops after running for an amount of time in seconds equal
to Time limit.
Fitness limit — The algorithm stops when the value of the fitness function for the best point
in the current population is less than or equal to Fitness limit.
Stall generations — The algorithm stops when the average relative change in the fitness
function value over Stall generations is less than Function tolerance.
Stall time limit — The algorithm stops if there is no improvement in the objective function
during an interval of time in seconds equal to Stall time limit.
Stall test — The stall condition is either average change or geometric weighted.
For geometric weighted, the weighting function is 1/2n, where n is the number of generations
prior to the current. Both stall conditions apply to the relative change in the fitness function
over Stall generations.
Function Tolerance — The algorithm runs until the average relative change in the fitness
function value over Stall generations is less than Function tolerance.
Nonlinear constraint tolerance — The Nonlinear constraint tolerance is not used as
stopping criterion. It is used to determine the feasibility with respect to nonlinear constraints.
Also, a point is feasible with respect to linear constraints when the constraint violation is
below the square root of Nonlinear constraint tolerance.
The algorithm stops as soon as any one of these conditions is met. You can specify the values
of these criteria in the Stopping criteria pane in the Optimization app. The default values are
shown in the pane.
When you run the genetic algorithm, the Run solver and view results panel displays the
criterion that caused the algorithm to stop.
The options Stall time limit and Time limit prevent the algorithm from running too long. If
the algorithm stops due to one of these conditions, you might improve your results by
increasing the values of Stall time limit and Time limit.
Selection
The selection function chooses parents for the next generation based on their scaled values
from the fitness scaling function. An individual can be selected more than once as a parent, in
which case it contributes its genes to more than one child. The default selection
option, Stochastic uniform, lays out a line in which each parent corresponds to a section of
the line of length proportional to its scaled value. The algorithm moves along the line in steps
of equal size. At each step, the algorithm allocates a parent from the section it lands on.
A more deterministic selection option is Remainder, which performs two steps:
In the first step, the function selects parents deterministically according to the integer part of
the scaled value for each individual. For example, if an individual's scaled value is 2.3, the
function selects that individual twice as a parent.
In the second step, the selection function selects additional parents using the fractional parts
of the scaled values, as in stochastic uniform selection. The function lays out a line in
sections, whose lengths are proportional to the fractional part of the scaled value of the
individuals, and moves along the line in equal steps to select the parents.
Note that if the fractional parts of the scaled values all equal 0, as can occur
using Top scaling, the selection is entirely deterministic.
For details and more selection options, see Selection Options.
Reproduction Options
Reproduction options control how the genetic algorithm creates the next generation. The
options are
Elite count — The number of individuals with the best fitness values in the current
generation that are guaranteed to survive to the next generation. These individuals are
called elite children. The default value of Elite count is 2.
When Elite count is at least 1, the best fitness value can only decrease from one generation to
the next. This is what you want to happen, since the genetic algorithm minimizes the fitness
function. Setting Elite count to a high value causes the fittest individuals to dominate the
population, which can make the search less effective.
Crossover fraction — The fraction of individuals in the next generation, other than elite
children, that are created by crossover. Setting the Crossover Fraction describes how the
value of Crossover fraction affects the performance of the genetic algorithm.
Mutation and Crossover
The genetic algorithm uses the individuals in the current generation to create the children that
make up the next generation. Besides elite children, which correspond to the individuals in
the current generation with the best fitness values, the algorithm creates
Crossover children by selecting vector entries, or genes, from a pair of individuals in the
current generation and combines them to form a child
Mutation children by applying random changes to a single individual in the current
generation to create a child
Both processes are essential to the genetic algorithm. Crossover enables the algorithm to
extract the best genes from different individuals and recombine them into potentially superior
children. Mutation adds to the diversity of a population and thereby increases the likelihood
that the algorithm will generate individuals with better fitness values.
See Creating the Next Generation for an example of how the genetic algorithm applies
mutation and crossover.
You can specify how many of each type of children the algorithm creates as follows:
Elite count, in Reproduction options, specifies the number of elite children.
Crossover fraction, in Reproduction options, specifies the fraction of the population, other
than elite children, that are crossover children.
For example, if the Population size is 20, the Elite count is 2, and the Crossover
fraction is 0.8, the numbers of each type of children in the next generation are as follows:
There are two elite children.
There are 18 individuals other than elite children, so the algorithm rounds 0.8*18 = 14.4 to 14
to get the number of crossover children.
The remaining four individuals, other than elite children, are mutation children.
p2 = [1 2 3 4 5 6 7 8]
and the binary vector is [1 1 0 0 1 0 0 0], the function returns the following child:
child1 = [a b 3 4 e 6 7 8]
Caution Do not use @crossoverscattered when you have linear constraints. Otherwise, your population wi
Single point (@crossoversinglepoint) chooses a random integer n between 1 and Number of
variables and then
o Selects vector entries numbered less than or equal to n from the first parent.
o Selects vector entries numbered greater than n from the second parent.
o Concatenates these entries to form a child vector.
For example, if p1 and p2 are the parents
p1 = [a b c d e f g h]
p2 = [1 2 3 4 5 6 7 8]
and the crossover point is 3, the function returns the following child.
child = [a b c 4 5 6 7 8]
Caution Do not use @crossoversinglepoint when you have linear constraints. Otherwise, your population w
Two point (@crossovertwopoint) selects two random
integers m and n between 1 and Number of variables. The function selects
o Vector entries numbered less than or equal to m from the first parent
o Vector entries numbered from m+1 to n, inclusive, from the second parent
o Vector entries numbered greater than n from the first parent.
The algorithm then concatenates these genes to form a single gene. For example,
if p1 and p2 are the parents
p1 = [a b c d e f g h]
p2 = [1 2 3 4 5 6 7 8]
and the crossover points are 3 and 6, the function returns the following child.
child = [a b c 4 5 6 g h]
Caution Do not use @crossovertwopoint when you have linear constraints. Otherwise, your population wi
Intermediate (@crossoverintermediate), the default crossover function when there are
linear constraints, creates children by taking a weighted average of the parents. You
can specify the weights by a single parameter, Ratio, which can be a scalar or a row
vector of length Number of variables. The default is a vector of all 1's. The function
creates the child from parent1 andparent2 using the following formula.
child = parent1 + rand * Ratio * ( parent2 - parent1)
If all the entries of Ratio lie in the range [0, 1], the children produced are within the
hypercube defined by placing the parents at opposite vertices. If Ratio is not in that range, the
children might lie outside the hypercube. If Ratio is a scalar, then all the children lie on the
line between the parents.
To change the default value of Ratio at the command line, use the syntax
options = optimoptions('ga','CrossoverFcn', ...
{@crossoverintermediate, ratio});
where ratio is the value of Ratio.
Heuristic (@crossoverheuristic) returns a child that lies on the line containing the two
parents, a small distance away from the parent with the better fitness value in the
direction away from the parent with the worse fitness value. You can specify how far
the child is from the better parent by the parameter Ratio, which appears when you
select Heuristic. The default value of Ratio is 1.2. If parent1 and parent2 are the
parents, and parent1 has the better fitness value, the function returns the child
child = parent2 + R * (parent1 - parent2);
To change the default value of Ratio at the command line, use the syntax
options = optimoptions('ga','CrossoverFcn',...
{@crossoverheuristic,ratio});
where ratio is the value of Ratio.
Arithmetic (@crossoverarithmetic) creates children that are the weighted arithmetic
mean of two parents. Children are always feasible with respect to linear constraints
and bounds.
Custom enables you to write your own crossover function. To specify the crossover
function using the Optimization app,
o Set Crossover function to Custom.
o Set Function name to @myfun, where myfun is the name of your function.
If you are using ga, set
options = optimoptions('ga','CrossoverFcn',@myfun);
Your crossover function must have the following calling syntax.
xoverKids = myfun(parents, options, nvars, FitnessFcn, ...
unused,thisPopulation)
The arguments to the function are
o parents — Row vector of parents chosen by the selection function
o options — options structure
o nvars — Number of variables
o FitnessFcn — Fitness function
o unused — Placeholder not used
o thisPopulation — Matrix representing the current population. The number of rows of the
matrix is Population size and the number of columns is Number of variables.
The function returns xoverKids—the crossover offspring—as a matrix where rows
correspond to the children. The number of columns of the matrix is Number of variables.
Passing Extra Parameters in the Optimization Toolbox documentation explains how to
provide additional parameters to the function.
Caution When you have bounds or linear constraints, ensure that your crossover function creates individua
population will not necessarily satisfy the constraints.
Migration Options
Note: Subpopulations refer to a form of parallel processing for the genetic algorithm. ga currently does not
hosts a number of individuals. These individuals are a subpopulation. The worker evolves the subpopulation
migration causes some individuals to travel between workers.
Because ga does not currently support this form of parallel processing, there is no benefit to setting Populati
the MigrationDirection, MigrationInterval, orMigrationFraction options.
Migration options specify how individuals move between subpopulations. Migration occurs if
you set Population size to be a vector of length greater than 1. When migration occurs, the
best individuals from one subpopulation replace the worst individuals in another
subpopulation. Individuals that migrate from one subpopulation to another are copied. They
are not removed from the source subpopulation.
You can control how migration occurs by the following three fields in the Migration options
pane:
Direction (MigrationDirection) — Migration can take place in one or both directions.
o If you set Direction to Forward ('forward'), migration takes place toward the last
subpopulation. That is, the nth subpopulation migrates into the (n+1)th subpopulation.
o If you set Direction to Both ('both'), the nth subpopulation migrates into both the (n–1)th and
the (n+1)th subpopulation.
Migration wraps at the ends of the subpopulations. That is, the last subpopulation migrates
into the first, and the first may migrate into the last.
Interval (MigrationInterval) — Specifies how many generation pass between migrations. For
example, if you set Interval to 20, migration takes place every 20 generations.
Fraction (MigrationFraction) — Specifies how many individuals move between
subpopulations. Fraction specifies the fraction of the smaller of the two subpopulations that
moves. For example, if individuals migrate from a subpopulation of 50 individuals into a
subpopulation of 100 individuals and you set Fraction to 0.1, the number of individuals that
migrate is 0.1*50=5.
Constraint Parameters
Constraint parameters refer to the nonlinear constraint solver. For details on the algorithm,
see Nonlinear Constraint Solver Algorithms.
Choose between the nonlinear constraint algorithms by setting
the NonlinearConstraintAlgorithm option to 'auglag' (Augmented Lagrangian)
or 'penalty' (Penalty algorithm).
Augmented Lagrangian Genetic Algorithm
Penalty Algorithm
Augmented Lagrangian Genetic Algorithm
Initial penalty (InitialPenalty) — Specifies an initial value of the penalty parameter that is
used by the nonlinear constraint algorithm. Initial penalty must be greater than or equal to 1,
and has a default of 10.
Penalty factor (PenaltyFactor) — Increases the penalty parameter when the problem is not
solved to required accuracy and constraints are not satisfied. Penalty factor must be greater
than 1, and has a default of 100.
Penalty Algorithm
The penalty algorithm uses the gacreationnonlinearfeasible creation function by default. This
creation function uses fmincon to find feasible
individuals. gacreationnonlinearfeasiblestarts fmincon from a variety of initial points within
the bounds from the InitialPopulationRange option.
Optionally, gacreationnonlinearfeasible can run fmincon in parallel on the initial points.
You can specify tuning parameters for gacreationnonlinearfeasible using the following name-
value pairs.
Name Value
UseParallel When true, run fmincon in parallel on initial points; default is false.
options = optimoptions('ga',options,'HybridFcn',{@fminunc,hybridopts});
hybridopts must exist before you set options.
Note: To stop the iterations, set state.StopFlag to a nonempty character vector, such as 'y'.
The output function has the following input arguments:
options — Options structure
state — Structure containing information about the current generation. The State
Structure describes the fields of state.
flag — Current status of the algorithm:
o 'init' — Initialization state
o 'iter' — Iteration state
o 'interrupt' — Iteration of a subproblem of a nonlinearly constrained problem
When flag is 'interrupt', the values of optimvalues fields apply to the subproblem iterations.
When flag is 'interrupt', ga does not accept changes in options, and ignores optchanged.
o 'done' — Final state
Passing Extra Parameters in the Optimization Toolbox documentation explains how to
provide additional parameters to the function.
The output function returns the following arguments to ga:
state — Structure containing information about the current generation. The State
Structure describes the fields of state. To stop the iterations, set state.StopFlag to a nonempty
character vector, such as 'y'.
options — Options structure modified by the output function. This argument is optional.
optchanged — Boolean flag indicating changes to options. To change options for subsequent
iterations, set optchanged to true.
Changing the State Structure
Caution Changing the state structure carelessly can lead to inconsistent or erroneous results. Usually, you c
using mutation or crossover functions, instead of changing the state structure in a plot function or output fun
ga output functions can change the state structure (see The State Structure). Be careful when
changing values in this structure, as you can pass inconsistent data back to ga.
Tip If your output structure changes the Population field, then be sure to update the Score field, and possib
they contain consistent information.
To update the Score field after changing the Population field, first calculate the fitness
function values of the population, then calculate the fitness scaling for the population.
See Fitness Scaling Options.
Display to Command Window Options
Level of display ('Display') specifies how much information is displayed at the command
line while the genetic algorithm is running. The available options are
Off ('off') — No output is displayed.
Iterative ('iter') — Information is displayed at each iteration.
Diagnose ('diagnose') — Information is displayed at each iteration. In addition, the diagnostic
lists some problem information and the options that have been changed from the defaults.
Final ('final') — The reason for stopping is displayed.
Both Iterative and Diagnose display the following information:
Generation — Generation number
f-count — Cumulative number of fitness function evaluations
Best f(x) — Best fitness function value
Mean f(x) — Mean fitness function value
Stall generations — Number of generations since the last improvement of the fitness function
When a nonlinear constraint function has been specified, Iterative and Diagnose do not
display the Mean f(x), but will additionally display:
Max Constraint — Maximum nonlinear constraint violation
The default value of Level of display is
Off in the Optimization app
'final' in options created using optimoptions
Vectorize and Parallel Options (User Function Evaluation)
You can choose to have your fitness and constraint functions evaluated in serial, parallel, or
in a vectorized fashion. These options are available in the User function evaluation section
of theOptions pane of the Optimization app, or by setting
the 'UseVectorized' and 'UseParallel' options with optimoptions.
When Evaluate fitness and constraint functions ('UseVectorized') is in
serial (false), ga calls the fitness function on one individual at a time as it loops through the
population. (At the command line, this assumes 'UseParallel' is at its default value of false.)
When Evaluate fitness and constraint functions ('UseVectorized')
is vectorized (true), ga calls the fitness function on the entire population at once, i.e., in a
single call to the fitness function.
If there are nonlinear constraints, the fitness function and the nonlinear constraints all need to
be vectorized in order for the algorithm to compute in a vectorized manner.
See Vectorize the Fitness Function for an example.
When Evaluate fitness and constraint functions (UseParallel) is in parallel (true), ga calls
the fitness function in parallel, using the parallel environment you established (see How to
Use Parallel Processing). At the command line, set UseParallel to false to compute serially.
Note: You cannot simultaneously use vectorized and parallel computations. If you set 'UseParallel' to true a
and constraint functions in a vectorized manner, not in parallel.
How Fitness and Constraint Functions Are Evaluated
UseVectorized = false U