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

public Class NPointCrossover<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, .List, org.apache.commons.math3.exception.DimensionMismatchException, .MathIllegalArgumentException, .NotStrictlyPositiveException, .NumberIsTooLargeException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.random.RandomGenerator

N-point crossover policy. For each iteration 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 (2-point crossover):
-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.
Since
3.1

Field Summary

Modifier and TypeField and Description
private final int
crossoverPoints

The number of crossover points.

Constructor Summary

AccessConstructor and Description
public
NPointCrossover(final int
the number of crossover points
crossoverPoints
)

Creates a new NPointCrossover policy using the given number of points.

Method Summary

Modifier and TypeMethod and Description
public ChromosomePair

Returns:

pair of two children (c1,c2)
crossover
(final Chromosome
first parent (p1)
first
,
final Chromosome
second parent (p2)
second
)

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

Performs a N-point crossover.

public int

Returns:

the number of crossover points
getCrossoverPoints
()

Returns the number of crossover points used by this CrossoverPolicy.

private 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

crossoverPointsback to summary
private final int crossoverPoints

The number of crossover points.

Constructor Detail

NPointCrossoverback to summary
public NPointCrossover(final int crossoverPoints) throws NotStrictlyPositiveException

Creates a new 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.

Parameters
crossoverPoints:int

the number of crossover points

Exceptions
NotStrictlyPositiveException:
if the number of crossoverPoints is not strictly positive

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.

Performs a N-point crossover. N random crossover points are selected and are used to divide the parent chromosomes into segments. The segments are copied in alternate order from the two parents to the corresponding child chromosomes. Example (2-point crossover):

-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)
Parameters
first:Chromosome

first parent (p1)

second:Chromosome

second parent (p2)

Returns:ChromosomePair

pair of two children (c1,c2)

Annotations
@SuppressWarnings:unchecked
Exceptions
DimensionMismatchException:
if the length of the two chromosomes is different
MathIllegalArgumentException:
iff one of the chromosomes is not an instance of AbstractListChromosome
getCrossoverPointsback to summary
public int getCrossoverPoints()

Returns the number of crossover points used by this CrossoverPolicy.

Returns:int

the number of crossover points

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

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
NumberIsTooLargeException:
if the number of crossoverPoints is too large for the actual chromosomes