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 */ 017package org.apache.commons.math3.genetics; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.apache.commons.math3.exception.DimensionMismatchException; 023import org.apache.commons.math3.exception.MathIllegalArgumentException; 024import 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 * 050 */ 051public class OnePointCrossover<T> implements CrossoverPolicy { 052 053 /** 054 * Performs one point crossover. A random crossover point is selected and the 055 * first part from each parent is copied to the corresponding child, and the 056 * second parts are copied crosswise. 057 * 058 * Example: 059 * <pre> 060 * -C- denotes a crossover point 061 * -C- -C- 062 * p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1) 063 * \------------/ \-----/ \------------/ \-----/ 064 * || (*) || (**) 065 * VV (**) VV (*) 066 * /------------\ /-----\ /------------\ /-----\ 067 * c1 = (1 0 1 0 0 1 | 1 1 1) X c2 = (0 1 1 0 1 0 | 0 1 1) 068 * </pre> 069 * 070 * @param first first parent (p1) 071 * @param second second parent (p2) 072 * @return pair of two children (c1,c2) 073 * @throws MathIllegalArgumentException iff one of the chromosomes is 074 * not an instance of {@link AbstractListChromosome} 075 * @throws DimensionMismatchException if the length of the two chromosomes is different 076 */ 077 @SuppressWarnings("unchecked") // OK because of instanceof checks 078 public ChromosomePair crossover(final Chromosome first, final Chromosome second) 079 throws DimensionMismatchException, MathIllegalArgumentException { 080 081 if (! (first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) { 082 throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); 083 } 084 return crossover((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); 085 } 086 087 088 /** 089 * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. 090 * 091 * @param first the first chromosome. 092 * @param second the second chromosome. 093 * @return the pair of new chromosomes that resulted from the crossover. 094 * @throws DimensionMismatchException if the length of the two chromosomes is different 095 */ 096 private ChromosomePair crossover(final AbstractListChromosome<T> first, 097 final AbstractListChromosome<T> second) throws DimensionMismatchException { 098 final int length = first.getLength(); 099 if (length != second.getLength()) { 100 throw new DimensionMismatchException(second.getLength(), length); 101 } 102 103 // array representations of the parents 104 final List<T> parent1Rep = first.getRepresentation(); 105 final List<T> parent2Rep = second.getRepresentation(); 106 // and of the children 107 final List<T> child1Rep = new ArrayList<T>(length); 108 final List<T> child2Rep = new ArrayList<T>(length); 109 110 // select a crossover point at random (0 and length makes no sense) 111 final int crossoverIndex = 1 + (GeneticAlgorithm.getRandomGenerator().nextInt(length-2)); 112 113 // copy the first part 114 for (int i = 0; i < crossoverIndex; i++) { 115 child1Rep.add(parent1Rep.get(i)); 116 child2Rep.add(parent2Rep.get(i)); 117 } 118 // and switch the second part 119 for (int i = crossoverIndex; i < length; i++) { 120 child1Rep.add(parent2Rep.get(i)); 121 child2Rep.add(parent1Rep.get(i)); 122 } 123 124 return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), 125 second.newFixedLengthChromosome(child2Rep)); 126 } 127 128}