Package org.apache.commons.math3.genetics

# Class UniformCrossover<T>

java.lang.Object
org.apache.commons.math3.genetics.UniformCrossover<T>

Type Parameters:
  `T` - generic type of the `AbstractListChromosome`s for crossover

All Implemented Interfaces:
  `CrossoverPolicy`

public class UniformCrossover<T> extends Object implements CrossoverPolicy
Perform Uniform Crossover [UX] on the specified chromosomes. A fixed mixing ratio is used to combine genes from the first and second parents, e.g. using a ratio of 0.5 would result in approximately 50% of genes coming from each parent. This is typically a poor method of crossover, but empirical evidence suggests that it is more exploratory and results in a larger part of the problem space being searched.

This crossover policy evaluates each gene of the parent chromosomes by chosing a uniform random number `p` in the range [0, 1]. If `p` < `ratio`, the parent genes are swapped. This means with a ratio of 0.7, 30% of the genes from the first parent and 70% from the second parent will be selected for the first offspring (and vice versa for the second offspring).

This policy works only on `AbstractListChromosome`, and therefore it is parameterized by T. Moreover, the chromosomes must have same lengths.

Since:
  3.1
See Also:
  - Crossover techniques (Wikipedia)
  - Crossover (Obitko.com)
  - Uniform crossover

## Constructor Summary

Constructors
Constructor
Description
`UniformCrossover(double ratio)`
Creates a new `UniformCrossover` policy using the given mixing ratio.

## Method Summary

Modifier and Type
Method
Description
`ChromosomePair`
`crossover(Chromosome first, Chromosome second)`
Perform a crossover operation on the given chromosomes.
`double`
`getRatio()`
Returns the mixing ratio used by this `CrossoverPolicy`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### UniformCrossover

public UniformCrossover(double ratio) throws OutOfRangeException
Creates a new `UniformCrossover` policy using the given mixing ratio.

Parameters:
  `ratio` - the mixing ratio
Throws:
  `OutOfRangeException` - if the mixing ratio is outside the [0, 1] range

## Method Details

### getRatio

public double getRatio()
Returns the mixing ratio used by this `CrossoverPolicy`.

Returns:
  the mixing ratio

### crossover

public ChromosomePair crossover(Chromosome first, Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException
Perform a crossover operation on the given chromosomes.

Specified by:
  `crossover` in interface `CrossoverPolicy`
Parameters:
  `first` - the first chromosome.
  `second` - the second chromosome.
Returns:
  the pair of new chromosomes that resulted from the crossover.
Throws:
  `MathIllegalArgumentException` - iff one of the chromosomes is not an instance of `AbstractListChromosome`
  `DimensionMismatchException` - if the length of the two chromosomes is different