001 /*
002 * Java Genetic Algorithm Library (jenetics-3.0.0).
003 * Copyright (c) 2007-2014 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.exp;
023 import static java.lang.String.format;
024 import static org.jenetics.internal.math.arithmetic.divide;
025 import static org.jenetics.internal.math.arithmetic.normalize;
026 import static org.jenetics.internal.math.statistics.max;
027 import static org.jenetics.internal.util.Equality.eq;
028
029 import org.jenetics.internal.util.Equality;
030 import org.jenetics.internal.util.Hash;
031
032 /**
033 * <p>
034 * In this {@code Selector}, the probability for selection is defined as.
035 * </p>
036 * <p><img
037 * src="doc-files/boltzmann-formula1.gif"
038 * alt="P(i)=\frac{\textup{e}^{b\cdot f_i}}{Z}"
039 * >
040 * </p>
041 * where <i>b</i> controls the selection intensity, and
042 * <p><img
043 * src="doc-files/boltzmann-formula2.gif"
044 * alt="Z=\sum_{j=1}^{n}\textrm{e}^{f_j}"
045 * >.
046 * </p>
047 *
048 * <i>f</i><sub><i>j</i></sub> denotes the fitness value of the
049 * <i>j<sup>th</sup></i> individual.
050 * <br>
051 * Positive values of <i>b</i> increases the selection probability of the phenotype
052 * with high fitness values. Negative values of <i>b</i> increases the selection
053 * probability of phenotypes with low fitness values. If <i>b</i> is zero the
054 * selection probability of all phenotypes is set to <sup>1</sup>/<sub>N</sub>.
055 *
056 * @param <G> the gene type.
057 * @param <N> the BoltzmannSelector requires a number type.
058 *
059 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
060 * @since 1.0
061 * @version 3.0 — <em>$Date: 2014-12-28 $</em>
062 */
063 public final class BoltzmannSelector<
064 G extends Gene<?, G>,
065 N extends Number & Comparable<? super N>
066 >
067 extends ProbabilitySelector<G, N>
068 {
069
070 private final double _b;
071
072 /**
073 * Create a new BoltzmanSelector with the given <i>b</i> value. <b>High
074 * absolute values of <i>b</i> can create numerical overflows while
075 * calculating the selection probabilities.</b>
076 *
077 * @param b the <i>b</i> value of this BoltzmanSelector
078 */
079 public BoltzmannSelector(final double b) {
080 _b = b;
081 }
082
083 /**
084 * Create a new BoltzmannSelector with a default beta of 4.0.
085 */
086 public BoltzmannSelector() {
087 this(4.0);
088 }
089
090 @Override
091 protected double[] probabilities(
092 final Population<G, N> population,
093 final int count
094 ) {
095 assert (population != null) : "Population must not be null. ";
096 assert (count > 0) : "Population to select must be greater than zero. ";
097
098 // Copy the fitness values to probabilities arrays.
099 final double[] probabilities = new double[population.size()];
100 for (int i = population.size(); --i >= 0;) {
101 probabilities[i] = population.get(i).getFitness().doubleValue();
102 }
103
104 // Scale the fitness values to avoid overflows.
105 divide(probabilities, max(probabilities));
106
107 for (int i = probabilities.length; --i >= 0;) {
108 probabilities[i] = exp(_b*probabilities[i]);
109 }
110
111 normalize(probabilities);
112 assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
113
114 return probabilities;
115 }
116
117 @Override
118 public int hashCode() {
119 return Hash.of(getClass()).and(_b).value();
120 }
121
122 @Override
123 public boolean equals(final Object obj) {
124 return Equality.of(this, obj).test(selector -> eq(_b, selector._b));
125 }
126
127 @Override
128 public String toString() {
129 return format("BoltzmannSelector[b=%f]", _b);
130 }
131
132 }
|