org.apache.commons.math3.genetics

## Class NPointCrossover<T>

- java.lang.Object
- - org.apache.commons.math3.genetics.NPointCrossover<T>

- Type Parameters:
    `T` - generic type of the `AbstractListChromosome`s for crossover
  All Implemented Interfaces:
    CrossoverPolicy

      public class NPointCrossover<T>
      extends java.lang.Object
      implements CrossoverPolicy

  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
  Version:
    $Id: NPointCrossover.java 1385297 2012-09-16 16:05:57Z tn $

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private int` | `crossoverPoints` The number of crossover points.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NPointCrossover(int crossoverPoints)` Creates a new `NPointCrossover` policy using the given number of points.

  - ### Method Summary

    Methods
    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)`.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### crossoverPoints

          private final int crossoverPoints

      The number of crossover points.

  - ### Constructor Detail

    - #### NPointCrossover

          public NPointCrossover(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` - the number of crossover points
      Throws:
        `NotStrictlyPositiveException` - if the number of `crossoverPoints` is not strictly positive

  - ### Method Detail

    - #### getCrossoverPoints

          public int getCrossoverPoints()

      Returns the number of crossover points used by this `CrossoverPolicy`.
      Returns:
        the number of crossover points

    - #### crossover

          public ChromosomePair crossover(Chromosome first,
                                 Chromosome second)
                                   throws DimensionMismatchException,
                                          MathIllegalArgumentException

      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)

      Specified by:
        `crossover` in interface `CrossoverPolicy`
      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 of `AbstractListChromosome`
        `DimensionMismatchException` - if the length of the two chromosomes is different

    - #### mate

          private ChromosomePair mate(AbstractListChromosome<T> first,
                            AbstractListChromosome<T> second)
                               throws DimensionMismatchException,
                                      NumberIsTooLargeException

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

Copyright (c) 2003-2013 Apache Software Foundation