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