Example usage for org.apache.commons.math3.exception.util LocalizedFormats INVALID_FIXED_LENGTH_CHROMOSOME

List of usage examples for org.apache.commons.math3.exception.util LocalizedFormats INVALID_FIXED_LENGTH_CHROMOSOME

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception.util LocalizedFormats INVALID_FIXED_LENGTH_CHROMOSOME.

Prototype

LocalizedFormats INVALID_FIXED_LENGTH_CHROMOSOME

To view the source code for org.apache.commons.math3.exception.util LocalizedFormats INVALID_FIXED_LENGTH_CHROMOSOME.

Click Source Link

Usage

From source file:eu.tsp.sal.NPointCrossover.java

/**
 * 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)://from w  ww . j  a v a  2  s .  c  om
 * <pre>
 * -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)
 * </pre>
 *
 * @param first first parent (p1)
 * @param second second parent (p2)
 * @return pair of two children (c1,c2)
 * @throws MathIllegalArgumentException iff one of the chromosomes is
 *   not an instance of {@link AbstractListChromosome}
 * @throws DimensionMismatchException if the length of the two chromosomes is different
 */
@SuppressWarnings("unchecked") // OK because of instanceof checks
public ChromosomePair crossover(final Chromosome first, final Chromosome second)
        throws DimensionMismatchException, MathIllegalArgumentException {

    if (!(first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) {
        throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME);
    }
    return mate((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second);
}