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