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.math4.legacy.genetics; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.apache.commons.math4.legacy.exception.DimensionMismatchException; 023import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; 024import org.apache.commons.math4.legacy.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 @Override 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 List<T> child1Rep = new ArrayList<>(length); 109 final List<T> child2Rep = new ArrayList<>(length); 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}