001 /*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 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.Double.compare;
023 import static java.lang.Math.pow;
024 import static java.lang.String.format;
025
026 import io.jenetics.util.Seq;
027
028 /**
029 * <p>
030 * An alternative to the "weak" {@code LinearRankSelector} is to assign
031 * survival probabilities to the sorted individuals using an exponential
032 * function.
033 * </p>
034 * <p><img
035 * src="doc-files/exponential-rank-selector.svg"
036 * alt="P(i)=\left(c-1\right)\frac{c^{i-1}}{c^{N}-1}"
037 * >,
038 * </p>
039 * where <i>c</i> must within the range {@code [0..1)}.
040 *
041 * <p>
042 * A small value of <i>c</i> increases the probability of the best phenotypes to
043 * be selected. If <i>c</i> is set to zero, the selection probability of the best
044 * phenotype is set to one. The selection probability of all other phenotypes is
045 * zero. A value near one equalizes the selection probabilities.
046 * </p>
047 * <p>
048 * This selector sorts the population in descending order while calculating the
049 * selection probabilities.
050 * </p>
051 *
052 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
053 * @since 1.0
054 * @version 5.0
055 */
056 public final class ExponentialRankSelector<
057 G extends Gene<?, G>,
058 C extends Comparable<? super C>
059 >
060 extends ProbabilitySelector<G, C>
061 {
062
063 private final double _c;
064
065 /**
066 * Create a new exponential rank selector.
067 *
068 * @param c the <i>c</i> value.
069 * @throws IllegalArgumentException if {@code c} is not within the range
070 * {@code [0..1)}.
071 */
072 public ExponentialRankSelector(final double c) {
073 super(true);
074
075 if (compare(c, 0) < 0 || compare(c, 1) >= 0) {
076 throw new IllegalArgumentException(format(
077 "Value %f is out of range [0..1): ", c
078 ));
079 }
080 _c = c;
081 }
082
083 /**
084 * Create a new selector with the default value of 0.975.
085 */
086 public ExponentialRankSelector() {
087 this(0.975);
088 }
089
090 /**
091 * This method sorts the population in descending order while calculating the
092 * selection probabilities.
093 */
094 @Override
095 protected double[] probabilities(
096 final Seq<Phenotype<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 String toString() {
116 return format("%s[c=%f]", getClass().getSimpleName(), _c);
117 }
118
119 }
|