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