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.Math.exp;
023 import static java.lang.String.format;
024 import static org.jenetics.util.math.divide;
025 import static org.jenetics.util.math.normalize;
026 import static org.jenetics.util.math.statistics.max;
027 import static org.jenetics.util.object.eq;
028 import static org.jenetics.util.object.hashCodeOf;
029
030 import javolution.lang.Immutable;
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 1.0 — <em>$Date: 2013-11-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 implements Immutable
069 {
070
071 private final double _b;
072
073 /**
074 * Create a new BolzmanSelector with the given <i>b</i> value. <b>High
075 * absolute values of <i>b</i> can create numerical overflows while
076 * calculating the selection probabilities.</b>
077 *
078 * @param b the <i>b</i> value of this BolzmanSelector
079 */
080 public BoltzmannSelector(final double b) {
081 _b = b;
082 }
083
084 /**
085 * Create a new BoltzmannSelector with a default beta of 0.2.
086 */
087 public BoltzmannSelector() {
088 this(0.2);
089 }
090
091 @Override
092 protected double[] probabilities(
093 final Population<G, N> population,
094 final int count
095 ) {
096 assert (population != null) : "Population must not be null. ";
097 assert (count > 0) : "Population to select must be greater than zero. ";
098
099 // Copy the fitness values to probabilities arrays.
100 final double[] probabilities = new double[population.size()];
101 for (int i = population.size(); --i >= 0;) {
102 probabilities[i] = population.get(i).getFitness().doubleValue();
103 }
104
105 // Scale the fitness values to avoid overflows.
106 divide(probabilities, max(probabilities));
107
108 for (int i = probabilities.length; --i >= 0;) {
109 probabilities[i] = exp(_b*probabilities[i]);
110 }
111
112 normalize(probabilities);
113
114 assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
115 return probabilities;
116 }
117
118 @Override
119 public int hashCode() {
120 return hashCodeOf(getClass()).and(_b).value();
121 }
122
123 @Override
124 public boolean equals(final Object obj) {
125 if (obj == this) {
126 return true;
127 }
128 if (obj == null || obj.getClass() != getClass()) {
129 return false;
130 }
131
132 final BoltzmannSelector<?, ?> selector = (BoltzmannSelector<?, ?>)obj;
133 return eq(_b, selector._b);
134 }
135
136 @Override
137 public String toString() {
138 return format("BoltzmannSelector[b=%f]", _b);
139 }
140
141 }
142
143
144
|