001 /*
002 * Java Genetic Algorithm Library (jenetics-3.0.0).
003 * Copyright (c) 2007-2014 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 2.0 — <em>$Date: 2014-12-28 $</em>
050 */
051 public 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 }
060
061 /**
062 * This method sorts the population in descending order while calculating the
063 * selection probabilities. (The method {@link Population#populationSort()} is called
064 * by this method.)
065 */
066 @Override
067 public Population<G, N> select(
068 final Population<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 final Population<G, N> selection = new Population<>(count);
081 if (count == 0) {
082 return selection;
083 }
084
085 final double[] probabilities = probabilities(population, count, opt);
086 assert (population.size() == probabilities.length);
087
088 //Calculating the equally spaces random points.
089 final double delta = 1.0/count;
090 final double[] points = new double[count];
091 points[0] = RandomRegistry.getRandom().nextDouble()*delta;
092 for (int i = 1; i < count; ++i) {
093 points[i] = delta*i;
094 }
095
096 int j = 0;
097 double prop = 0;
098 for (int i = 0; i < count; ++i) {
099 while (points[i] > prop) {
100 prop += probabilities[j];
101 ++j;
102 }
103 selection.add(population.get(j%population.size()));
104 }
105
106 return selection;
107 }
108
109 @Override
110 protected double[] probabilities(
111 final Population<G, N> population,
112 final int count
113 ) {
114 population.populationSort();
115 return super.probabilities(population, count);
116 }
117
118 @Override
119 public int hashCode() {
120 return Hash.of(getClass()).and(super.hashCode()).value();
121 }
122
123 @Override
124 public boolean equals(final Object obj) {
125 return Equality.of(this, obj).test(super::equals);
126 }
127
128 @Override
129 public String toString() {
130 return getClass().getSimpleName();
131 }
132
133 }
|