Class CycleCrossover<T>
- java.lang.Object
-
- org.apache.commons.math4.legacy.genetics.CycleCrossover<T>
-
- Type Parameters:
T
- generic type of theAbstractListChromosome
s for crossover
- All Implemented Interfaces:
CrossoverPolicy
public class CycleCrossover<T> extends Object implements CrossoverPolicy
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:
- start with the first gene of parent 1
- look at the gene at the same position of parent 2
- go to the position with the same gene in parent 1
- add this gene index to the cycle
- repeat the steps 2-5 until we arrive at the starting gene of this 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 onAbstractListChromosome
, and therefore it is parameterized by T. Moreover, the chromosomes must have same lengths.- Since:
- 3.1
- See Also:
- Cycle Crossover Operator
-
-
Constructor Summary
Constructors Constructor Description CycleCrossover()
Creates a newCycleCrossover
policy.CycleCrossover(boolean randomStart)
Creates a newCycleCrossover
policy using the givenrandomStart
behavior.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ChromosomePair
crossover(Chromosome first, Chromosome second)
Perform a crossover operation on the given chromosomes.boolean
isRandomStart()
Returns whether the starting index is chosen randomly or set to zero.protected ChromosomePair
mate(AbstractListChromosome<T> first, AbstractListChromosome<T> second)
Helper forcrossover(Chromosome, Chromosome)
.
-
-
-
Constructor Detail
-
CycleCrossover
public CycleCrossover()
Creates a newCycleCrossover
policy.
-
CycleCrossover
public CycleCrossover(boolean randomStart)
Creates a newCycleCrossover
policy using the givenrandomStart
behavior.- Parameters:
randomStart
- whether the start index shall be chosen randomly or be set to 0
-
-
Method Detail
-
isRandomStart
public boolean isRandomStart()
Returns whether the starting index is chosen randomly or set to zero.- Returns:
true
if the starting index is chosen randomly,false
otherwise
-
crossover
public ChromosomePair crossover(Chromosome first, Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException
Perform a crossover operation on the given chromosomes.- Specified by:
crossover
in interfaceCrossoverPolicy
- Parameters:
first
- the first chromosome.second
- the second chromosome.- Returns:
- the pair of new chromosomes that resulted from the crossover.
- Throws:
MathIllegalArgumentException
- if the chromosomes are not an instance ofAbstractListChromosome
DimensionMismatchException
- if the length of the two chromosomes is different
-
mate
protected ChromosomePair mate(AbstractListChromosome<T> first, AbstractListChromosome<T> second) throws DimensionMismatchException
Helper forcrossover(Chromosome, Chromosome)
. Performs the actual crossover.- Parameters:
first
- the first chromosomesecond
- the second chromosome- Returns:
- the pair of new chromosomes that resulted from the crossover
- Throws:
DimensionMismatchException
- if the length of the two chromosomes is different
-
-