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