org.apache.commons.math3.genetics
T
- generic type of the AbstractListChromosome
s for crossoverpublic class NPointCrossover<T> extends java.lang.Object implements CrossoverPolicy
-C- denotes a crossover point -C- -C- -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 VV (*) VV /----\ /--------\ /-----\ /----\ /--------\ /-----\ c1 = (1 0 | 1 0 1 0 | 0 1 1) X c2 = (0 1 | 1 0 0 1 | 0 1 1)This policy works only on
AbstractListChromosome
, and therefore it
is parameterized by T. Moreover, the chromosomes must have same lengths.Modifier and Type | Field and Description |
---|---|
private int |
crossoverPoints
The number of crossover points.
|
Constructor and Description |
---|
NPointCrossover(int crossoverPoints)
Creates a new
NPointCrossover policy using the given number of points. |
Modifier and Type | Method and Description |
---|---|
ChromosomePair |
crossover(Chromosome first,
Chromosome second)
Performs a N-point crossover.
|
int |
getCrossoverPoints()
Returns the number of crossover points used by this
CrossoverPolicy . |
private ChromosomePair |
mate(AbstractListChromosome<T> first,
AbstractListChromosome<T> second)
Helper for
crossover(Chromosome, Chromosome) . |
public NPointCrossover(int crossoverPoints) throws NotStrictlyPositiveException
NPointCrossover
policy using the given number of points.
Note: the number of crossover points must be < chromosome length - 1
.
This condition can only be checked at runtime, as the chromosome length is not known in advance.
crossoverPoints
- the number of crossover pointsNotStrictlyPositiveException
- if the number of crossoverPoints
is not strictly positivepublic int getCrossoverPoints()
CrossoverPolicy
.public ChromosomePair crossover(Chromosome first, Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException
-C- denotes a crossover point -C- -C- -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 VV (*) VV /----\ /--------\ /-----\ /----\ /--------\ /-----\ c1 = (1 0 | 1 0 1 0 | 0 1 1) X c2 = (0 1 | 1 0 0 1 | 0 1 1)
crossover
in interface CrossoverPolicy
first
- first parent (p1)second
- second parent (p2)MathIllegalArgumentException
- iff one of the chromosomes is
not an instance of AbstractListChromosome
DimensionMismatchException
- if the length of the two chromosomes is differentprivate ChromosomePair mate(AbstractListChromosome<T> first, AbstractListChromosome<T> second) throws DimensionMismatchException, NumberIsTooLargeException
crossover(Chromosome, Chromosome)
. Performs the actual crossover.first
- the first chromosomesecond
- the second chromosomeDimensionMismatchException
- if the length of the two chromosomes is differentNumberIsTooLargeException
- if the number of crossoverPoints is too large for the actual chromosomesCopyright (c) 2003-2013 Apache Software Foundation