Class OnePointCrossover<T>
java.lang.Object
org.apache.commons.math3.genetics.OnePointCrossover<T>
- Type Parameters:
T- generic type of theAbstractListChromosomes for crossover
- All Implemented Interfaces:
CrossoverPolicy
One point crossover policy. A random crossover point is selected and the
first part from each parent is copied to the corresponding child, and the
second parts are copied crosswise.
Example:
-C- denotes a crossover point
-C- -C-
p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1)
\------------/ \-----/ \------------/ \-----/
|| (*) || (**)
VV (**) VV (*)
/------------\ /-----\ /------------\ /-----\
c1 = (1 0 1 0 0 1 | 1 1 1) X c2 = (0 1 1 0 1 0 | 0 1 1)
This policy works only on AbstractListChromosome, and therefore it
is parameterized by T. Moreover, the chromosomes must have same lengths.- Since:
- 2.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate ChromosomePaircrossover(AbstractListChromosome<T> first, AbstractListChromosome<T> second) Helper forcrossover(Chromosome, Chromosome).crossover(Chromosome first, Chromosome second) Performs one point crossover.
-
Constructor Details
-
OnePointCrossover
public OnePointCrossover()
-
-
Method Details
-
crossover
public ChromosomePair crossover(Chromosome first, Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException Performs one point crossover. A random crossover point is selected and the first part from each parent is copied to the corresponding child, and the second parts are copied crosswise. Example:-C- denotes a crossover point -C- -C- p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1) \------------/ \-----/ \------------/ \-----/ || (*) || (**) VV (**) VV (*) /------------\ /-----\ /------------\ /-----\ c1 = (1 0 1 0 0 1 | 1 1 1) X c2 = (0 1 1 0 1 0 | 0 1 1)- Specified by:
crossoverin interfaceCrossoverPolicy- Parameters:
first- first parent (p1)second- second parent (p2)- Returns:
- pair of two children (c1,c2)
- Throws:
MathIllegalArgumentException- iff one of the chromosomes is not an instance ofAbstractListChromosomeDimensionMismatchException- if the length of the two chromosomes is different
-
crossover
private ChromosomePair crossover(AbstractListChromosome<T> first, AbstractListChromosome<T> second) throws DimensionMismatchException Helper forcrossover(Chromosome, Chromosome). Performs the actual crossover.- Parameters:
first- the first chromosome.second- 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
-