001 /*
002 * Java Genetic Algorithm Library (jenetics-1.5.0).
003 * Copyright (c) 2007-2013 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.Math.abs;
023 import static org.jenetics.util.math.pow;
024 import static org.jenetics.util.math.ulpDistance;
025 import static org.jenetics.util.math.statistics.min;
026 import static org.jenetics.util.math.statistics.sum;
027 import static org.jenetics.util.object.hashCodeOf;
028
029 import java.util.Arrays;
030
031 import javolution.lang.Immutable;
032
033
034 /**
035 * The roulette-wheel selector is also known as fitness proportional selector,
036 * but in the <em>Jenetics</em> library it is implemented as probability selector.
037 * The fitness value <i>f<sub>i</sub></i> is used to calculate the selection
038 * probability of individual <i>i</i>.
039 *
040 * @see <a href="http://en.wikipedia.org/wiki/Roulette_wheel_selection">
041 * Wikipedia: Roulette wheel selection
042 * </a>
043 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044 * @since 1.0
045 * @version 1.0 — <em>$Date: 2013-12-08 $</em>
046 */
047 public class RouletteWheelSelector<
048 G extends Gene<?, G>,
049 N extends Number & Comparable<? super N>
050 >
051 extends ProbabilitySelector<G, N>
052 implements Immutable
053 {
054
055 private static final long MAX_ULP_DISTANCE = pow(10, 9);
056
057 public RouletteWheelSelector() {
058 }
059
060 @Override
061 protected double[] probabilities(
062 final Population<G, N> population,
063 final int count
064 ) {
065 assert(population != null) : "Population can not be null. ";
066 assert(count > 0) : "Population to select must be greater than zero. ";
067
068 // Copy the fitness values to probabilities arrays.
069 final double[] probabilities = new double[population.size()];
070 for (int i = population.size(); --i >= 0;) {
071 probabilities[i] = population.get(i).getFitness().doubleValue();
072 }
073
074 final double worst = Math.min(min(probabilities), 0.0);
075 final double sum = sum(probabilities) - worst*population.size();
076
077 if (abs(ulpDistance(sum, 0.0)) > MAX_ULP_DISTANCE) {
078 for (int i = population.size(); --i >= 0;) {
079 probabilities[i] = (probabilities[i] - worst)/sum;
080 }
081 } else {
082 Arrays.fill(probabilities, 1.0/population.size());
083 }
084
085 assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
086 return probabilities;
087 }
088
089 @Override
090 public int hashCode() {
091 return hashCodeOf(getClass()).value();
092 }
093
094 @Override
095 public boolean equals(final Object obj) {
096 return obj == this || obj != null && obj.getClass() == getClass();
097 }
098
099 @Override
100 public String toString() {
101 return getClass().getSimpleName();
102 }
103
104 }
105
106
107
108
|