| 
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.lang.Math.pow;
 023 import static java.lang.String.format;
 024 import static org.jenetics.internal.util.Equality.eq;
 025
 026 import org.jenetics.internal.util.Equality;
 027 import org.jenetics.internal.util.Hash;
 028
 029 /**
 030  * <p>
 031  * An alternative to the "weak" {@code LinearRankSelector} is to assign
 032  * survival probabilities to the sorted individuals using an exponential
 033  * function.
 034  * </p>
 035  * <p><img
 036  *        src="doc-files/exponential-rank-selector.gif"
 037  *        alt="P(i)=\left(c-1\right)\frac{c^{i-1}}{c^{N}-1}"
 038  *     >,
 039  * </p>
 040  * where <i>c</i> must within the range {@code [0..1)}.
 041  *
 042  * <p>
 043  * A small value of <i>c</i> increases the probability of the best phenotypes to
 044  * be selected. If <i>c</i> is set to zero, the selection probability of the best
 045  * phenotype is set to one. The selection probability of all other phenotypes is
 046  * zero. A value near one equalizes the selection probabilities.
 047  * </p>
 048  * <p>
 049  * This selector sorts the population in descending order while calculating the
 050  * selection probabilities.
 051  * </p>
 052  *
 053  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 054  * @since 1.0
 055  * @version 2.0 — <em>$Date: 2014-12-28 $</em>
 056  */
 057 public final class ExponentialRankSelector<
 058     G extends Gene<?, G>,
 059     C extends Comparable<? super C>
 060 >
 061     extends ProbabilitySelector<G, C>
 062 {
 063
 064     private final double _c;
 065
 066     /**
 067      * Create a new exponential rank selector.
 068      *
 069      * @param c the <i>c</i> value.
 070      * @throws IllegalArgumentException if {@code c} is not within the range
 071      *         {@code [0..1)}.
 072      */
 073     public ExponentialRankSelector(final double c) {
 074         super(true);
 075
 076         if (c < 0.0 || c >= 1.0) {
 077             throw new IllegalArgumentException(format(
 078                 "Value %s is out of range [0..1): ", c
 079             ));
 080         }
 081         _c = c;
 082     }
 083
 084     /**
 085      * Create a new selector with default value of 0.975.
 086      */
 087     public ExponentialRankSelector() {
 088         this(0.975);
 089     }
 090
 091     /**
 092      * This method sorts the population in descending order while calculating the
 093      * selection probabilities. (The method {@link Population#populationSort()} is called
 094      * by this method.)
 095      */
 096     @Override
 097     protected double[] probabilities(
 098         final Population<G, C> population,
 099         final int count
 100     ) {
 101         assert(population != null) : "Population can not be null. ";
 102         assert(count > 0) : "Population to select must be greater than zero. ";
 103
 104         //Sorted population required.
 105         population.populationSort();
 106
 107         final double N = population.size();
 108         final double[] probabilities = new double[population.size()];
 109
 110         final double b = (_c - 1.0)/(pow(_c, N) - 1.0);
 111         for (int i = 0; i < probabilities.length; ++i) {
 112             probabilities[i] = pow(_c, i)*b;
 113         }
 114
 115         assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
 116         return probabilities;
 117     }
 118
 119     @Override
 120     public int hashCode() {
 121         return Hash.of(getClass()).and(_c).value();
 122     }
 123
 124     @Override
 125     public boolean equals(final Object obj) {
 126         return Equality.of(this, obj).test(s -> eq(_c, s._c));
 127     }
 128
 129     @Override
 130     public String toString() {
 131         return format("%s[c=%f]", getClass().getSimpleName(), _c);
 132     }
 133
 134 }
 |