Top Description Fields Constructors Methods
org.apache.commons.math3.genetics

public Class CycleCrossover<T>

extends Object
implements CrossoverPolicy
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.genetics.CrossoverPolicy
Type Parameters
<T>
generic type of the AbstractListChromosomes for crossover
Imports
java.util.ArrayList, .HashSet, .List, .Set, org.apache.commons.math3.exception.DimensionMismatchException, .MathIllegalArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats

Cycle Crossover [CX] builds offspring from ordered chromosomes by identifying cycles between two parent chromosomes. To form the children, the cycles are copied from the respective parents.

To form a cycle the following procedure is applied:

  1. start with the first gene of parent 1
  2. look at the gene at the same position of parent 2
  3. go to the position with the same gene in parent 1
  4. add this gene index to the cycle
  5. repeat the steps 2-5 until we arrive at the starting gene of this cycle
The indices that form a cycle are then used to form the children in alternating order, i.e. in cycle 1, the genes of parent 1 are copied to child 1, while in cycle 2 the genes of parent 1 are copied to child 2, and so forth ...

Example (zero-start cycle):
p1 = (8 4 7 3 6 2 5 1 9 0)    X   c1 = (8 1 2 3 4 5 6 7 9 0)
p2 = (0 1 2 3 4 5 6 7 8 9)    X   c2 = (0 4 7 3 6 2 5 1 8 9)

cycle 1: 8 0 9
cycle 2: 4 1 7 2 5 6
cycle 3: 3
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
Cycle Crossover Operator

Field Summary

Modifier and TypeField and Description
private final boolean
randomStart

If the start index shall be chosen randomly.

Constructor Summary

AccessConstructor and Description
public
CycleCrossover()

Creates a new CycleCrossover policy.

public
CycleCrossover(final boolean
whether the start index shall be chosen randomly or be set to 0
randomStart
)

Creates a new CycleCrossover policy using the given randomStart behavior.

Method Summary

Modifier and TypeMethod and Description
public ChromosomePair
crossover(final Chromosome
the first chromosome.
first
,
final Chromosome
the second chromosome.
second
)

Implements org.apache.commons.math3.genetics.CrossoverPolicy.crossover.

Perform a crossover operation on the given chromosomes.

public boolean

Returns:

true if the starting index is chosen randomly, false otherwise
isRandomStart
()

Returns whether the starting index is chosen randomly or set to zero.

protected ChromosomePair

Returns:

the pair of new chromosomes that resulted from the crossover
mate
(final AbstractListChromosome<T>
the first chromosome
first
,
final AbstractListChromosome<T>
the second chromosome
second
)

Helper for crossover(Chromosome, Chromosome).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

randomStartback to summary
private final boolean randomStart

If the start index shall be chosen randomly.

Constructor Detail

CycleCrossoverback to summary
public CycleCrossover()

Creates a new CycleCrossover policy.

CycleCrossoverback to summary
public CycleCrossover(final boolean randomStart)

Creates a new CycleCrossover policy using the given randomStart behavior.

Parameters
randomStart:boolean

whether the start index shall be chosen randomly or be set to 0

Method Detail

crossoverback to summary
public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException

Implements org.apache.commons.math3.genetics.CrossoverPolicy.crossover.

Doc from org.apache.commons.math3.genetics.CrossoverPolicy.crossover.

Perform a crossover operation on the given chromosomes.

Parameters
first:Chromosome

the first chromosome.

second:Chromosome

the second chromosome.

Returns:ChromosomePair

the pair of new chromosomes that resulted from the crossover.

Annotations
@SuppressWarnings:unchecked
Exceptions
DimensionMismatchException:
if the length of the two chromosomes is different
MathIllegalArgumentException:
if the chromosomes are not an instance of AbstractListChromosome
isRandomStartback to summary
public boolean isRandomStart()

Returns whether the starting index is chosen randomly or set to zero.

Returns:boolean

true if the starting index is chosen randomly, false otherwise

mateback to summary
protected ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second) throws DimensionMismatchException

Helper for crossover(Chromosome, Chromosome). Performs the actual crossover.

Parameters
first:AbstractListChromosome<T>

the first chromosome

second:AbstractListChromosome<T>

the second chromosome

Returns:ChromosomePair

the pair of new chromosomes that resulted from the crossover

Exceptions
DimensionMismatchException:
if the length of the two chromosomes is different