source: src/main/java/agents/anac/y2019/harddealer/math3/genetics/TournamentSelection.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 4.5 KB
Line 
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 */
17package agents.anac.y2019.harddealer.math3.genetics;
18
19import java.util.ArrayList;
20import java.util.List;
21
22import agents.anac.y2019.harddealer.math3.exception.MathIllegalArgumentException;
23import agents.anac.y2019.harddealer.math3.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 */
33public 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 public ChromosomePair select(final Population population) throws MathIllegalArgumentException {
58 return new ChromosomePair(tournament((ListPopulation) population),
59 tournament((ListPopulation) population));
60 }
61
62 /**
63 * Helper for {@link #select(Population)}. Draw {@link #arity} random chromosomes without replacement from the
64 * population, and then select the fittest chromosome among them.
65 *
66 * @param population the population from which the chromosomes are chosen.
67 * @return the selected chromosome.
68 * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size
69 */
70 private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException {
71 if (population.getPopulationSize() < this.arity) {
72 throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY,
73 arity, population.getPopulationSize());
74 }
75 // auxiliary population
76 ListPopulation tournamentPopulation = new ListPopulation(this.arity) {
77 /** {@inheritDoc} */
78 public Population nextGeneration() {
79 // not useful here
80 return null;
81 }
82 };
83
84 // create a copy of the chromosome list
85 List<Chromosome> chromosomes = new ArrayList<Chromosome> (population.getChromosomes());
86 for (int i=0; i<this.arity; i++) {
87 // select a random individual and add it to the tournament
88 int rind = GeneticAlgorithm.getRandomGenerator().nextInt(chromosomes.size());
89 tournamentPopulation.addChromosome(chromosomes.get(rind));
90 // do not select it again
91 chromosomes.remove(rind);
92 }
93 // the winner takes it all
94 return tournamentPopulation.getFittestChromosome();
95 }
96
97 /**
98 * Gets the arity (number of chromosomes drawn to the tournament).
99 *
100 * @return arity of the tournament
101 */
102 public int getArity() {
103 return arity;
104 }
105
106 /**
107 * Sets the arity (number of chromosomes drawn to the tournament).
108 *
109 * @param arity arity of the tournament
110 */
111 public void setArity(final int arity) {
112 this.arity = arity;
113 }
114
115}
Note: See TracBrowser for help on using the repository browser.