001/*
002 * Java Genetic Algorithm Library (jenetics-8.3.0).
003 * Copyright (c) 2007-2025 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 */
020package io.jenetics;
021
022import static java.util.Objects.requireNonNull;
023
024import io.jenetics.util.ISeq;
025import io.jenetics.util.MSeq;
026import io.jenetics.util.RandomRegistry;
027import io.jenetics.util.Seq;
028
029/**
030 * {@code StochasticUniversalSelector} is a method for selecting a
031 * population according to some given probability in a way that minimizes 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@gmail.com">Franz Wilhelmstötter</a>
048 * @since 1.0
049 * @version 5.0
050 */
051public class StochasticUniversalSelector<
052        G extends Gene<?, G>,
053        N extends Number & Comparable<? super N>
054>
055        extends RouletteWheelSelector<G, N>
056{
057
058        public StochasticUniversalSelector() {
059                super(false);
060        }
061
062        /**
063         * This method sorts the population in descending order while calculating the
064         * selection probabilities.
065         */
066        @Override
067        public ISeq<Phenotype<G, N>> select(
068                final Seq<Phenotype<G, N>> population,
069                final int count,
070                final Optimize opt
071        ) {
072                requireNonNull(population, "Population");
073                if (count < 0) {
074                        throw new IllegalArgumentException(
075                                "Selection count must be greater or equal then zero, but was " +
076                                count
077                        );
078                }
079
080                if (count == 0 || population.isEmpty()) {
081                        return ISeq.empty();
082                }
083
084                final MSeq<Phenotype<G, N>> selection = MSeq.ofLength(count);
085
086                final double[] probabilities = probabilities(population, count, opt);
087                assert population.size() == probabilities.length;
088
089                //Calculating the equal spaces of random points.
090                final double delta = 1.0/count;
091                final double[] points = new double[count];
092                points[0] = RandomRegistry.random().nextDouble()*delta;
093                for (int i = 1; i < count; ++i) {
094                        points[i] = points[i - 1] + delta;
095                }
096
097                int j = 0;
098                double cumProb = probabilities[0];
099                for (int i = 0; i < count; ++i) {
100                        while (points[i] > cumProb) {
101                                ++j;
102                                cumProb += probabilities[j%population.size()];
103                        }
104
105                        selection.set(i, population.get(j%population.size()));
106                }
107
108                return selection.toISeq();
109        }
110
111}