001 /*
002 * Java Genetic Algorithm Library (jenetics-4.0.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@gmail.com)
019 */
020 package io.jenetics;
021
022 import static java.util.Objects.requireNonNull;
023
024 import io.jenetics.internal.util.Equality;
025 import io.jenetics.internal.util.Hash;
026 import io.jenetics.util.ISeq;
027 import io.jenetics.util.MSeq;
028 import io.jenetics.util.RandomRegistry;
029 import io.jenetics.util.Seq;
030
031 /**
032 * {@code StochasticUniversalSelector} is a method for selecting a
033 * population according to some given probability in a way that minimize chance
034 * fluctuations. It can be viewed as a type of roulette game where now we have
035 * P equally spaced points which we spin.
036 *
037 * <p>
038 * <img src="doc-files/StochasticUniversalSelection.svg" width="400"
039 * alt="Selector">
040 * </p>
041 *
042 * The figure above shows how the stochastic-universal selection works; <i>n</i>
043 * is the number of individuals to select.
044 *
045 * @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Stochastic_universal_sampling">
046 * Wikipedia: Stochastic universal sampling
047 * </a>
048 *
049 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
050 * @since 1.0
051 * @version 4.0
052 */
053 public class StochasticUniversalSelector<
054 G extends Gene<?, G>,
055 N extends Number & Comparable<? super N>
056 >
057 extends RouletteWheelSelector<G, N>
058 {
059
060 public StochasticUniversalSelector() {
061 super(true);
062 }
063
064 /**
065 * This method sorts the population in descending order while calculating the
066 * selection probabilities.
067 */
068 @Override
069 public ISeq<Phenotype<G, N>> select(
070 final Seq<Phenotype<G, N>> population,
071 final int count,
072 final Optimize opt
073 ) {
074 requireNonNull(population, "Population");
075 if (count < 0) {
076 throw new IllegalArgumentException(
077 "Selection count must be greater or equal then zero, but was " +
078 count
079 );
080 }
081
082 if (count == 0 || population.isEmpty()) {
083 return ISeq.empty();
084 }
085
086 final MSeq<Phenotype<G, N>> selection = MSeq.ofLength(count);
087
088 final Seq<Phenotype<G, N>> pop = _sorted
089 ? population.asISeq().copy().sort(POPULATION_COMPARATOR)
090 : population;
091
092 final double[] probabilities = probabilities(pop, count, opt);
093 assert pop.size() == probabilities.length;
094
095 //Calculating the equally spaces random points.
096 final double delta = 1.0/count;
097 final double[] points = new double[count];
098 points[0] = RandomRegistry.getRandom().nextDouble()*delta;
099 for (int i = 1; i < count; ++i) {
100 points[i] = delta*i;
101 }
102
103 int j = 0;
104 double prop = 0;
105 for (int i = 0; i < count; ++i) {
106 while (points[i] > prop) {
107 prop += probabilities[j];
108 ++j;
109 }
110
111 selection.set(i, pop.get(j%pop.size()));
112 }
113
114 return selection.toISeq();
115 }
116
117 @Override
118 public int hashCode() {
119 return Hash.of(getClass()).and(super.hashCode()).value();
120 }
121
122 @Override
123 public boolean equals(final Object obj) {
124 return Equality.of(this, obj).test(super::equals);
125 }
126
127 @Override
128 public String toString() {
129 return getClass().getSimpleName();
130 }
131
132 }
|