/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics;

import static java.lang.Math.exp;
import static java.lang.String.format;
import static org.jenetics.internal.math.arithmetic.divide;
import static org.jenetics.internal.math.arithmetic.normalize;
import static org.jenetics.internal.math.statistics.max;
import static org.jenetics.internal.util.Equality.eq;

import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;

/**
 * <p>
 * In this {@code Selector}, the probability for selection is defined as.
 * </p>
 * <p><img
 *        src="doc-files/boltzmann-formula1.gif"
 *        alt="P(i)=\frac{\textup{e}^{b\cdot f_i}}{Z}"
 *     >
 * </p>
 * where <i>b</i> controls the selection intensity, and
 * <p><img
 *        src="doc-files/boltzmann-formula2.gif"
 *        alt="Z=\sum_{j=1}^{n}\textrm{e}^{f_j}"
 *     >.
 * </p>
 *
 * <i>f</i><sub><i>j</i></sub> denotes the fitness value of the
 * <i>j<sup>th</sup></i> individual.
 * <br>
 * Positive values of <i>b</i> increases the selection probability of the phenotype
 * with high fitness values. Negative values of <i>b</i> increases the selection
 * probability of phenotypes with low fitness values. If <i>b</i> is zero the
 * selection probability of all phenotypes is set to <sup>1</sup>/<sub>N</sub>.
 *
 * @param <G> the gene type.
 * @param <N> the BoltzmannSelector requires a number type.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 3.0 &mdash; <em>$Date: 2014-08-15 $</em>
 */
public final class BoltzmannSelector<
	G extends Gene<?, G>,
	N extends Number & Comparable<? super N>
>
	extends ProbabilitySelector<G, N>
{

	private final double _b;

	/**
	 * Create a new BoltzmanSelector with the given <i>b</i> value. <b>High
	 * absolute values of <i>b</i> can create numerical overflows while
	 * calculating the selection probabilities.</b>
	 *
	 * @param b the <i>b</i> value of this BoltzmanSelector
	 */
	public BoltzmannSelector(final double b) {
		_b = b;
	}

	/**
	 * Create a new BoltzmannSelector with a default beta of 3.0.
	 */
	public BoltzmannSelector() {
		this(3.0);
	}

	@Override
	protected double[] probabilities(
		final Population<G, N> population,
		final int count
	) {
		assert (population != null) : "Population must not be null. ";
		assert (count > 0) : "Population to select must be greater than zero. ";

		// Copy the fitness values to probabilities arrays.
		final double[] probabilities = new double[population.size()];
		for (int i = population.size(); --i >= 0;) {
			probabilities[i] = population.get(i).getFitness().doubleValue();
		}

		// Scale the fitness values to avoid overflows.
		divide(probabilities, max(probabilities));

		for (int i = probabilities.length; --i >= 0;) {
			probabilities[i] = exp(_b*probabilities[i]);
		}

		normalize(probabilities);

		assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
		return probabilities;
	}

	@Override
	public int hashCode() {
		return Hash.of(getClass()).and(_b).value();
	}

	@Override
	public boolean equals(final Object obj) {
		return Equality.of(this, obj).test(selector -> eq(_b, selector._b));
	}

	@Override
	public String toString() {
		return format("BoltzmannSelector[b=%f]", _b);
	}

}

@@ -2231,10 +2231,10 @@
 -08-
-15
+28
  $%3C/
@@ -2794,17 +2794,17 @@
 beta of 
-3
+4
 .0.%0A%09 */
@@ -2841,17 +2841,17 @@
 %0A%09%09this(
-3
+4
 .0);%0A%09%7D%0A

Change default parameter.