LinearRankSelector.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.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.String.format;
023 import static org.jenetics.internal.util.object.eq;
024 
025 import javolution.lang.Immutable;
026 
027 import org.jenetics.internal.util.HashBuilder;
028 
029 
030 /**
031  <p>
032  * In linear-ranking selection the individuals are sorted according to their
033  * fitness values. The rank <i>N</i> is assignee to the best individual and the
034  * rank 1 to the worst individual. The selection probability <i>P(i)</i>  of
035  * individual <i>i</i> is linearly assigned to the individuals according to
036  * their rank.
037  </p>
038  <p/><img
039  *        src="doc-files/linear-rank-selector.gif"
040  *        alt="P(i)=\frac{1}{N}\left(n^{-}+\left(n^{+}-n^{-}\right)\frac{i-1}{N-1}\right)"
041  *     >
042  </p>
043  *
044  * Here <i>n</i><sup><i>-</i></sup>/<i>N</i> is the probability of the worst
045  * individual to be    selected and <i>n</i><sup><i>+</i></sup>/<i>N</i> the
046  * probability of the best individual to be selected. As the population size is
047  * held constant, the conditions <i>n</i><sup><i>+</i></sup> = 2 - <i>n</i><sup><i>-</i></sup>
048  * and <i>n</i><sup><i>-</i></sup> &gt;= 0 must be fulfilled. Note that all individuals
049  * get a different rank, i.e., a different selection probability, even if the
050  * have the same fitness value. <p/>
051  *
052  <i>
053  * T. Blickle, L. Thiele, A comparison of selection schemes used
054  * in evolutionary algorithms, Technical Report, ETH Zurich, 1997, page 37.
055  * <a href="http://citeseer.ist.psu.edu/blickle97comparison.html">
056  *    http://citeseer.ist.psu.edu/blickle97comparison.html
057  </a>
058  </i>
059  *
060  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
061  @since 1.0
062  @version 1.0 &mdash; <em>$Date: 2014-03-01 $</em>
063  */
064 public final class LinearRankSelector<
065     extends Gene<?, G>,
066     extends Comparable<? super C>
067 >
068     extends ProbabilitySelector<G, C>
069     implements Immutable
070 {
071     private final double _nminus;
072     private final double _nplus;
073 
074     /**
075      * Create a new LinearRankSelector with {@code nminus := 0.5}.
076      */
077     public LinearRankSelector() {
078         this(0.5);
079     }
080 
081     /**
082      * Create a new LinearRankSelector with the given values for {@code nminus}.
083      *
084      @param nminus {@code nminus/N} is the probability of the worst phenotype
085      *         to be selected.
086      @throws IllegalArgumentException if {@code nminus < 0}.
087      */
088     public LinearRankSelector(final double nminus) {
089         if (nminus < 0) {
090             throw new IllegalArgumentException(format(
091                 "nminus is smaller than zero: %s", nminus
092             ));
093         }
094 
095         _nminus = nminus;
096         _nplus = - _nminus;
097     }
098 
099     /**
100      * This method sorts the population in descending order while calculating the
101      * selection probabilities. (The method {@link Population#sort()} is called
102      * by this method.)
103      */
104     @Override
105     protected double[] probabilities(
106         final Population<G, C> population,
107         final int count
108     ) {
109         assert(population != null"Population can not be null. ";
110         assert(count > 0"Population to select must be greater than zero. ";
111 
112         //Sort the population.
113         population.sort();
114 
115         final double N = population.size();
116         final double[] probabilities = new double[population.size()];
117 
118         for (int i = probabilities.length; --i >= 0;) {
119             probabilities[probabilities.length - i - 1=
120                 (_nminus + ((_nplus - _nminus)*i)/(N - 1))/N;
121         }
122 
123         assert (sum2one(probabilities)) "Probabilities doesn't sum to one.";
124         return probabilities;
125     }
126 
127     @Override
128     public int hashCode() {
129         return HashBuilder.of(getClass()).and(_nminus).and(_nplus).value();
130     }
131 
132     @Override
133     public boolean equals(final Object obj) {
134         if (obj == this) {
135             return true;
136         }
137         if (!(obj instanceof LinearRankSelector<?, ?>)) {
138             return false;
139         }
140 
141         final LinearRankSelector<?, ?> selector = (LinearRankSelector<?, ?>)obj;
142         return eq(_nminus, selector._nminus&& eq(_nplus, selector._nplus);
143     }
144 
145     @Override
146     public String toString() {
147         return format(
148             "%s[n-=%f, n+=%f]",
149             getClass().getSimpleName(), _nminus, _nplus
150         );
151     }
152 
153 }