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