1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.math4.legacy.genetics; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; 23 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; 24 25 /** 26 * Tournament selection scheme. Each of the two selected chromosomes is selected 27 * based on n-ary tournament -- this is done by drawing {@link #arity} random 28 * chromosomes without replacement from the population, and then selecting the 29 * fittest chromosome among them. 30 * 31 * @since 2.0 32 */ 33 public class TournamentSelection implements SelectionPolicy { 34 35 /** number of chromosomes included in the tournament selections. */ 36 private int arity; 37 38 /** 39 * Creates a new TournamentSelection instance. 40 * 41 * @param arity how many chromosomes will be drawn to the tournament 42 */ 43 public TournamentSelection(final int arity) { 44 this.arity = arity; 45 } 46 47 /** 48 * Select two chromosomes from the population. Each of the two selected 49 * chromosomes is selected based on n-ary tournament -- this is done by 50 * drawing {@link #arity} random chromosomes without replacement from the 51 * population, and then selecting the fittest chromosome among them. 52 * 53 * @param population the population from which the chromosomes are chosen. 54 * @return the selected chromosomes. 55 * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size 56 */ 57 @Override 58 public ChromosomePair select(final Population population) throws MathIllegalArgumentException { 59 return new ChromosomePair(tournament((ListPopulation) population), 60 tournament((ListPopulation) population)); 61 } 62 63 /** 64 * Helper for {@link #select(Population)}. Draw {@link #arity} random chromosomes without replacement from the 65 * population, and then select the fittest chromosome among them. 66 * 67 * @param population the population from which the chromosomes are chosen. 68 * @return the selected chromosome. 69 * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size 70 */ 71 private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException { 72 if (population.getPopulationSize() < this.arity) { 73 throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY, 74 arity, population.getPopulationSize()); 75 } 76 // auxiliary population 77 ListPopulation tournamentPopulation = new ListPopulation(this.arity) { 78 /** {@inheritDoc} */ 79 @Override 80 public Population nextGeneration() { 81 // not useful here 82 return null; 83 } 84 }; 85 86 // create a copy of the chromosome list 87 List<Chromosome> chromosomes = new ArrayList<> (population.getChromosomes()); 88 for (int i=0; i<this.arity; i++) { 89 // select a random individual and add it to the tournament 90 int rind = GeneticAlgorithm.getRandomGenerator().nextInt(chromosomes.size()); 91 tournamentPopulation.addChromosome(chromosomes.get(rind)); 92 // do not select it again 93 chromosomes.remove(rind); 94 } 95 // the winner takes it all 96 return tournamentPopulation.getFittestChromosome(); 97 } 98 99 /** 100 * Gets the arity (number of chromosomes drawn to the tournament). 101 * 102 * @return arity of the tournament 103 */ 104 public int getArity() { 105 return arity; 106 } 107 108 /** 109 * Sets the arity (number of chromosomes drawn to the tournament). 110 * 111 * @param arity arity of the tournament 112 */ 113 public void setArity(final int arity) { 114 this.arity = arity; 115 } 116 }