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.String.format;
023 import static org.jenetics.internal.util.Equality.eq;
024
025 import org.jenetics.internal.util.Equality;
026 import org.jenetics.internal.util.Hash;
027
028 /**
029 * <p>
030 * In linear-ranking selection the individuals are sorted according to their
031 * fitness values. The rank <i>N</i> is assignee to the best individual and the
032 * rank 1 to the worst individual. The selection probability <i>P(i)</i> of
033 * individual <i>i</i> is linearly assigned to the individuals according to
034 * their rank.
035 * </p>
036 * <p><img
037 * src="doc-files/linear-rank-selector.gif"
038 * alt="P(i)=\frac{1}{N}\left(n^{-}+\left(n^{+}-n^{-}\right)\frac{i-1}{N-1}\right)"
039 * >
040 * </p>
041 *
042 * Here <i>n</i><sup><i>-</i></sup>/<i>N</i> is the probability of the worst
043 * individual to be selected and <i>n</i><sup><i>+</i></sup>/<i>N</i> the
044 * probability of the best individual to be selected. As the population size is
045 * held constant, the conditions <i>n</i><sup><i>+</i></sup> = 2 - <i>n</i><sup><i>-</i></sup>
046 * and <i>n</i><sup><i>-</i></sup> >= 0 must be fulfilled. Note that all individuals
047 * get a different rank, i.e., a different selection probability, even if the
048 * have the same fitness value. <p>
049 *
050 * <i>
051 * T. Blickle, L. Thiele, A comparison of selection schemes used
052 * in evolutionary algorithms, Technical Report, ETH Zurich, 1997, page 37.
053 * <a href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.15.9584&rank=1">
054 * http://citeseer.ist.psu.edu/blickle97comparison.html
055 * </a>
056 * </i>
057 *
058 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
059 * @since 1.0
060 * @version 2.0 — <em>$Date: 2014-12-28 $</em>
061 */
062 public final class LinearRankSelector<
063 G extends Gene<?, G>,
064 C extends Comparable<? super C>
065 >
066 extends ProbabilitySelector<G, C>
067 {
068 private final double _nminus;
069 private final double _nplus;
070
071 /**
072 * Create a new LinearRankSelector with the given values for {@code nminus}.
073 *
074 * @param nminus {@code nminus/N} is the probability of the worst phenotype
075 * to be selected.
076 * @throws IllegalArgumentException if {@code nminus < 0}.
077 */
078 public LinearRankSelector(final double nminus) {
079 super(true);
080
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 = 2 - _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#populationSort()} 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.populationSort();
113
114 final double N = population.size();
115 final double[] probabilities = new double[population.size()];
116
117 if (N == 1) {
118 probabilities[0] = 1;
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 Hash.of(getClass()).and(_nminus).and(_nplus).value();
133 }
134
135 @Override
136 public boolean equals(final Object obj) {
137 return Equality.of(this, obj).test(selector ->
138 eq(_nminus, selector._nminus) &&
139 eq(_nplus, selector._nplus)
140 );
141 }
142
143 @Override
144 public String toString() {
145 return format(
146 "%s[(n-)=%f, (n+)=%f]",
147 getClass().getSimpleName(), _nminus, _nplus
148 );
149 }
150
151 }
|