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