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