001 /*
002 * Java Genetic Algorithm Library (jenetics-3.9.0).
003 * Copyright (c) 2007-2017 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.Math.pow;
023 import static java.lang.String.format;
024
025 import org.jenetics.internal.util.Hash;
026
027 /**
028 * <p>
029 * An alternative to the "weak" {@code LinearRankSelector} is to assign
030 * survival probabilities to the sorted individuals using an exponential
031 * function.
032 * </p>
033 * <p><img
034 * src="doc-files/exponential-rank-selector.gif"
035 * alt="P(i)=\left(c-1\right)\frac{c^{i-1}}{c^{N}-1}"
036 * >,
037 * </p>
038 * where <i>c</i> must within the range {@code [0..1)}.
039 *
040 * <p>
041 * A small value of <i>c</i> increases the probability of the best phenotypes to
042 * be selected. If <i>c</i> is set to zero, the selection probability of the best
043 * phenotype is set to one. The selection probability of all other phenotypes is
044 * zero. A value near one equalizes the selection probabilities.
045 * </p>
046 * <p>
047 * This selector sorts the population in descending order while calculating the
048 * selection probabilities.
049 * </p>
050 *
051 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
052 * @since 1.0
053 * @version 2.0
054 */
055 public final class ExponentialRankSelector<
056 G extends Gene<?, G>,
057 C extends Comparable<? super C>
058 >
059 extends ProbabilitySelector<G, C>
060 {
061
062 private final double _c;
063
064 /**
065 * Create a new exponential rank selector.
066 *
067 * @param c the <i>c</i> value.
068 * @throws IllegalArgumentException if {@code c} is not within the range
069 * {@code [0..1)}.
070 */
071 public ExponentialRankSelector(final double c) {
072 super(true);
073
074 if (c < 0.0 || c >= 1.0) {
075 throw new IllegalArgumentException(format(
076 "Value %s is out of range [0..1): ", c
077 ));
078 }
079 _c = c;
080 }
081
082 /**
083 * Create a new selector with default value of 0.975.
084 */
085 public ExponentialRankSelector() {
086 this(0.975);
087 }
088
089 /**
090 * This method sorts the population in descending order while calculating the
091 * selection probabilities. (The method {@link Population#populationSort()} is called
092 * by this method.)
093 */
094 @Override
095 protected double[] probabilities(
096 final Population<G, C> population,
097 final int count
098 ) {
099 assert population != null : "Population must not be null. ";
100 assert !population.isEmpty() : "Population is empty.";
101 assert count > 0 : "Population to select must be greater than zero. ";
102
103 final double N = population.size();
104 final double[] probabilities = new double[population.size()];
105
106 final double b = (_c - 1.0)/(pow(_c, N) - 1.0);
107 for (int i = 0; i < probabilities.length; ++i) {
108 probabilities[i] = pow(_c, i)*b;
109 }
110
111 return probabilities;
112 }
113
114 @Override
115 public int hashCode() {
116 return Hash.of(getClass()).and(_c).value();
117 }
118
119 @Override
120 public boolean equals(final Object obj) {
121 return obj instanceof ExponentialRankSelector &&
122 eq(((ExponentialRankSelector)obj)._c, _c);
123 }
124
125 @Override
126 public String toString() {
127 return format("%s[c=%f]", getClass().getSimpleName(), _c);
128 }
129
130 }
|