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