TournamentSelection.java

  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. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  21. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;

  22. /**
  23.  * Tournament selection scheme. Each of the two selected chromosomes is selected
  24.  * based on n-ary tournament -- this is done by drawing {@link #arity} random
  25.  * chromosomes without replacement from the population, and then selecting the
  26.  * fittest chromosome among them.
  27.  *
  28.  * @since 2.0
  29.  */
  30. public class TournamentSelection implements SelectionPolicy {

  31.     /** number of chromosomes included in the tournament selections. */
  32.     private int arity;

  33.     /**
  34.      * Creates a new TournamentSelection instance.
  35.      *
  36.      * @param arity how many chromosomes will be drawn to the tournament
  37.      */
  38.     public TournamentSelection(final int arity) {
  39.         this.arity = arity;
  40.     }

  41.     /**
  42.      * Select two chromosomes from the population. Each of the two selected
  43.      * chromosomes is selected based on n-ary tournament -- this is done by
  44.      * drawing {@link #arity} random chromosomes without replacement from the
  45.      * population, and then selecting the fittest chromosome among them.
  46.      *
  47.      * @param population the population from which the chromosomes are chosen.
  48.      * @return the selected chromosomes.
  49.      * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size
  50.      */
  51.     @Override
  52.     public ChromosomePair select(final Population population) throws MathIllegalArgumentException {
  53.         return new ChromosomePair(tournament((ListPopulation) population),
  54.                                   tournament((ListPopulation) population));
  55.     }

  56.     /**
  57.      * Helper for {@link #select(Population)}. Draw {@link #arity} random chromosomes without replacement from the
  58.      * population, and then select the fittest chromosome among them.
  59.      *
  60.      * @param population the population from which the chromosomes are chosen.
  61.      * @return the selected chromosome.
  62.      * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size
  63.      */
  64.     private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException {
  65.         if (population.getPopulationSize() < this.arity) {
  66.             throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY,
  67.                                                    arity, population.getPopulationSize());
  68.         }
  69.         // auxiliary population
  70.         ListPopulation tournamentPopulation = new ListPopulation(this.arity) {
  71.             /** {@inheritDoc} */
  72.             @Override
  73.             public Population nextGeneration() {
  74.                 // not useful here
  75.                 return null;
  76.             }
  77.         };

  78.         // create a copy of the chromosome list
  79.         List<Chromosome> chromosomes = new ArrayList<> (population.getChromosomes());
  80.         for (int i=0; i<this.arity; i++) {
  81.             // select a random individual and add it to the tournament
  82.             int rind = GeneticAlgorithm.getRandomGenerator().nextInt(chromosomes.size());
  83.             tournamentPopulation.addChromosome(chromosomes.get(rind));
  84.             // do not select it again
  85.             chromosomes.remove(rind);
  86.         }
  87.         // the winner takes it all
  88.         return tournamentPopulation.getFittestChromosome();
  89.     }

  90.     /**
  91.      * Gets the arity (number of chromosomes drawn to the tournament).
  92.      *
  93.      * @return arity of the tournament
  94.      */
  95.     public int getArity() {
  96.         return arity;
  97.     }

  98.     /**
  99.      * Sets the arity (number of chromosomes drawn to the tournament).
  100.      *
  101.      * @param arity arity of the tournament
  102.      */
  103.     public void setArity(final int arity) {
  104.         this.arity = arity;
  105.     }
  106. }