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