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