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