001 /*
002 * Java Genetic Algorithm Library (jenetics-1.5.0).
003 * Copyright (c) 2007-2013 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.util.object.eq;
024 import static org.jenetics.util.object.hashCodeOf;
025
026 import javolution.lang.Immutable;
027
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> >= 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 1.0 — <em>$Date: 2013-12-05 $</em>
062 */
063 public final class LinearRankSelector<
064 G extends Gene<?, G>,
065 C extends Comparable<? super C>
066 >
067 extends ProbabilitySelector<G, C>
068 implements Immutable
069 {
070 private final double _nminus;
071 private final double _nplus;
072
073 /**
074 * Create a new LinearRankSelector with {@code nminus := 0.5}.
075 */
076 public LinearRankSelector() {
077 this(0.5);
078 }
079
080 /**
081 * Create a new LinearRankSelector with the given values for {@code nminus}.
082 *
083 * @param nminus {@code nminus/N} is the probability of the worst phenotype
084 * to be selected.
085 * @throws IllegalArgumentException if {@code nminus < 0}.
086 */
087 public LinearRankSelector(final double nminus) {
088 if (nminus < 0) {
089 throw new IllegalArgumentException(format(
090 "nminus is smaller than zero: %s", nminus
091 ));
092 }
093
094 _nminus = nminus;
095 _nplus = 2 - _nminus;
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 for (int i = probabilities.length; --i >= 0;) {
118 probabilities[probabilities.length - i - 1] =
119 (_nminus + ((_nplus - _nminus)*i)/(N - 1))/N;
120 }
121
122 assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
123 return probabilities;
124 }
125
126 @Override
127 public int hashCode() {
128 return hashCodeOf(getClass()).and(_nminus).and(_nplus).value();
129 }
130
131 @Override
132 public boolean equals(final Object obj) {
133 if (obj == this) {
134 return true;
135 }
136 if (!(obj instanceof LinearRankSelector<?, ?>)) {
137 return false;
138 }
139
140 final LinearRankSelector<?, ?> selector = (LinearRankSelector<?, ?>)obj;
141 return eq(_nminus, selector._nminus) && eq(_nplus, selector._nplus);
142 }
143
144 @Override
145 public String toString() {
146 return format(
147 "%s[n-=%f, n+=%f]",
148 getClass().getSimpleName(), _nminus, _nplus
149 );
150 }
151
152 }
153
|