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.math.genetics; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 023 /** 024 * One point crossover policy. A random crossover point is selected and the 025 * first part from each parent is copied to the corresponding child, and the 026 * second parts are copied crosswise. 027 * 028 * Example: 029 * <pre> 030 * -C- denotes a crossover point 031 * -C- -C- 032 * p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1) 033 * \------------/ \-----/ \------------/ \-----/ 034 * || (*) || (**) 035 * VV (**) VV (*) 036 * /------------\ /-----\ /------------\ /-----\ 037 * c1 = (1 0 1 0 0 1 | 1 1 1) X p2 = (0 1 1 0 1 0 | 0 1 1) 038 * </pre> 039 * 040 * This policy works only on {@link AbstractListChromosome}, and therefore it 041 * is parametrized by T. Moreover, the chromosomes must have same lengths. 042 * 043 * @param <T> generic type of the {@link AbstractListChromosome}s for crossover 044 * @since 2.0 045 * @version $Revision: 903046 $ $Date: 2010-01-25 21:07:26 -0500 (Mon, 25 Jan 2010) $ 046 * 047 */ 048 public class OnePointCrossover<T> implements CrossoverPolicy { 049 050 /** 051 * Performs one point crossover. A random crossover point is selected and the 052 * first part from each parent is copied to the corresponding child, and the 053 * second parts are copied crosswise. 054 * 055 * Example: 056 * -C- denotes a crossover point 057 * -C- -C- 058 * p1 = (1 0 1 0 0 1 | 0 1 1) X p2 = (0 1 1 0 1 0 | 1 1 1) 059 * \------------/ \-----/ \------------/ \-----/ 060 * || (*) || (**) 061 * VV (**) VV (*) 062 * /------------\ /-----\ /------------\ /-----\ 063 * c1 = (1 0 1 0 0 1 | 1 1 1) X p2 = (0 1 1 0 1 0 | 0 1 1) 064 * 065 * @param first first parent (p1) 066 * @param second second parent (p2) 067 * @return pair of two children (c1,c2) 068 */ 069 @SuppressWarnings("unchecked") // OK because of instanceof checks 070 public ChromosomePair crossover(Chromosome first, Chromosome second) { 071 if (! (first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) { 072 throw new IllegalArgumentException("One point crossover works on FixedLengthChromosomes only."); 073 } 074 return crossover((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); 075 } 076 077 078 /** 079 * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. 080 * 081 * @param first the first chromosome. 082 * @param second the second chromosome. 083 * @return the pair of new chromosomes that resulted from the crossover. 084 */ 085 private ChromosomePair crossover(AbstractListChromosome<T> first, AbstractListChromosome<T> second) { 086 int length = first.getLength(); 087 if (length != second.getLength()) 088 throw new IllegalArgumentException("Both chromosomes must have same lengths."); 089 090 // array representations of the parents 091 List<T> parent1Rep = first.getRepresentation(); 092 List<T> parent2Rep = second.getRepresentation(); 093 // and of the children 094 ArrayList<T> child1Rep = new ArrayList<T> (first.getLength()); 095 ArrayList<T> child2Rep = new ArrayList<T> (second.getLength()); 096 097 // select a crossover point at random (0 and length makes no sense) 098 int crossoverIndex = 1 + (GeneticAlgorithm.getRandomGenerator().nextInt(length-2)); 099 100 // copy the first part 101 for (int i = 0; i < crossoverIndex; i++) { 102 child1Rep.add(parent1Rep.get(i)); 103 child2Rep.add(parent2Rep.get(i)); 104 } 105 // and switch the second part 106 for (int i = crossoverIndex; i < length; i++) { 107 child1Rep.add(parent2Rep.get(i)); 108 child2Rep.add(parent1Rep.get(i)); 109 } 110 111 return new ChromosomePair( 112 first.newFixedLengthChromosome(child1Rep), 113 second.newFixedLengthChromosome(child2Rep) 114 ); 115 } 116 117 }