001 /*
002 * Java Genetic Algorithm Library (jenetics-3.8.0).
003 * Copyright (c) 2007-2017 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics;
021
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static java.util.stream.Collectors.maxBy;
025
026 import java.util.Random;
027 import java.util.stream.IntStream;
028 import java.util.stream.Stream;
029
030 import org.jenetics.internal.util.Equality;
031 import org.jenetics.internal.util.Hash;
032
033 import org.jenetics.util.RandomRegistry;
034
035 /**
036 * In tournament selection the best individual from a random sample of <i>s</i>
037 * individuals is chosen from the population <i>P<sub>g</sub></i>. The samples
038 * are drawn with replacement. An individual will win a tournament only if its
039 * fitness is greater than the fitness of the other <i>s-1</i> competitors.
040 * Note that the worst individual never survives, and the best individual wins
041 * in all the tournaments it participates. The selection pressure can be varied
042 * by changing the tournament size <i>s</i> . For large values of <i>s</i>, weak
043 * individuals have less chance being selected.
044 *
045 * @see <a href="http://en.wikipedia.org/wiki/Tournament_selection">Tournament selection</a>
046 *
047 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
048 * @since 1.0
049 * @version 2.0
050 */
051 public class TournamentSelector<
052 G extends Gene<?, G>,
053 C extends Comparable<? super C>
054 >
055 implements Selector<G, C>
056 {
057
058 private final int _sampleSize;
059
060 /**
061 * Create a tournament selector with the give sample size. The sample size
062 * must be greater than one.
063 *
064 * @param sampleSize the number of individuals involved in one tournament
065 * @throws IllegalArgumentException if the sample size is smaller than two.
066 */
067 public TournamentSelector(final int sampleSize) {
068 if (sampleSize < 2) {
069 throw new IllegalArgumentException(
070 "Sample size must be greater than one, but was " + sampleSize
071 );
072 }
073 _sampleSize = sampleSize;
074 }
075
076 /**
077 * Create a tournament selector with sample size two.
078 */
079 public TournamentSelector() {
080 this(2);
081 }
082
083 @Override
084 public Population<G, C> select(
085 final Population<G, C> population,
086 final int count,
087 final Optimize opt
088 ) {
089 requireNonNull(population, "Population");
090 requireNonNull(opt, "Optimization");
091 if (count < 0) {
092 throw new IllegalArgumentException(format(
093 "Selection count must be greater or equal then zero, but was %s",
094 count
095 ));
096 }
097
098 final Random random = RandomRegistry.getRandom();
099 return population.isEmpty()
100 ? new Population<>(0)
101 : new Population<G, C>(count)
102 .fill(() -> select(population, opt, _sampleSize, random), count);
103 }
104
105 private Phenotype<G, C> select(
106 final Population<G, C> population,
107 final Optimize opt,
108 final int sampleSize,
109 final Random random
110 ) {
111 final int N = population.size();
112 return Stream.generate(() -> population.get(random.nextInt(N)))
113 .limit(sampleSize)
114 .collect(maxBy(opt.ascending())).get();
115 }
116
117 @Override
118 public int hashCode() {
119 return Hash.of(getClass()).and(_sampleSize).value();
120 }
121
122 @Override
123 public boolean equals(final Object obj) {
124 return Equality.of(this, obj).test(s -> _sampleSize == s._sampleSize);
125 }
126
127 @Override
128 public String toString() {
129 return format("%s[s=%d]", getClass().getSimpleName(), _sampleSize);
130 }
131
132 }
|