001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math3.genetics;
018
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import org.apache.commons.math3.exception.DimensionMismatchException;
023 import org.apache.commons.math3.exception.MathIllegalArgumentException;
024 import org.apache.commons.math3.exception.util.LocalizedFormats;
025
026
027 /**
028 * One point crossover policy. A random crossover point is selected and the
029 * first part from each parent is copied to the corresponding child, and the
030 * second parts are copied crosswise.
031 *
032 * Example:
033 * <pre>
034 * -C- denotes a crossover point
035 * -C- -C-
036 * p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1)
037 * \------------/ \-----/ \------------/ \-----/
038 * || (*) || (**)
039 * VV (**) VV (*)
040 * /------------\ /-----\ /------------\ /-----\
041 * c1 = (1 0 1 0 0 1 | 1 1 1) X c2 = (0 1 1 0 1 0 | 0 1 1)
042 * </pre>
043 *
044 * This policy works only on {@link AbstractListChromosome}, and therefore it
045 * is parameterized by T. Moreover, the chromosomes must have same lengths.
046 *
047 * @param <T> generic type of the {@link AbstractListChromosome}s for crossover
048 * @since 2.0
049 * @version $Id: OnePointCrossover.java 1416643 2012-12-03 19:37:14Z tn $
050 *
051 */
052 public class OnePointCrossover<T> implements CrossoverPolicy {
053
054 /**
055 * Performs one point crossover. A random crossover point is selected and the
056 * first part from each parent is copied to the corresponding child, and the
057 * second parts are copied crosswise.
058 *
059 * Example:
060 * <pre>
061 * -C- denotes a crossover point
062 * -C- -C-
063 * p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1)
064 * \------------/ \-----/ \------------/ \-----/
065 * || (*) || (**)
066 * VV (**) VV (*)
067 * /------------\ /-----\ /------------\ /-----\
068 * c1 = (1 0 1 0 0 1 | 1 1 1) X c2 = (0 1 1 0 1 0 | 0 1 1)
069 * </pre>
070 *
071 * @param first first parent (p1)
072 * @param second second parent (p2)
073 * @return pair of two children (c1,c2)
074 * @throws MathIllegalArgumentException iff one of the chromosomes is
075 * not an instance of {@link AbstractListChromosome}
076 * @throws DimensionMismatchException if the length of the two chromosomes is different
077 */
078 @SuppressWarnings("unchecked") // OK because of instanceof checks
079 public ChromosomePair crossover(final Chromosome first, final Chromosome second)
080 throws DimensionMismatchException, MathIllegalArgumentException {
081
082 if (! (first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) {
083 throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME);
084 }
085 return crossover((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second);
086 }
087
088
089 /**
090 * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover.
091 *
092 * @param first the first chromosome.
093 * @param second the second chromosome.
094 * @return the pair of new chromosomes that resulted from the crossover.
095 * @throws DimensionMismatchException if the length of the two chromosomes is different
096 */
097 private ChromosomePair crossover(final AbstractListChromosome<T> first,
098 final AbstractListChromosome<T> second) throws DimensionMismatchException {
099 final int length = first.getLength();
100 if (length != second.getLength()) {
101 throw new DimensionMismatchException(second.getLength(), length);
102 }
103
104 // array representations of the parents
105 final List<T> parent1Rep = first.getRepresentation();
106 final List<T> parent2Rep = second.getRepresentation();
107 // and of the children
108 final ArrayList<T> child1Rep = new ArrayList<T> (first.getLength());
109 final ArrayList<T> child2Rep = new ArrayList<T> (second.getLength());
110
111 // select a crossover point at random (0 and length makes no sense)
112 final int crossoverIndex = 1 + (GeneticAlgorithm.getRandomGenerator().nextInt(length-2));
113
114 // copy the first part
115 for (int i = 0; i < crossoverIndex; i++) {
116 child1Rep.add(parent1Rep.get(i));
117 child2Rep.add(parent2Rep.get(i));
118 }
119 // and switch the second part
120 for (int i = crossoverIndex; i < length; i++) {
121 child1Rep.add(parent2Rep.get(i));
122 child2Rep.add(parent1Rep.get(i));
123 }
124
125 return new ChromosomePair(first.newFixedLengthChromosome(child1Rep),
126 second.newFixedLengthChromosome(child2Rep));
127 }
128
129 }