/*
 * 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@gmail.com)
 */
package io.jenetics;

import static java.lang.String.format;
import static io.jenetics.internal.util.Hashes.hash;

import io.jenetics.util.Seq;

/**
 * <p>
 * In linear-ranking selection the individuals are sorted according to their
 * fitness values. The rank <i>N</i> is assignee to the best individual and the
 * rank 1 to the worst individual. The selection probability <i>P(i)</i>  of
 * individual <i>i</i> is linearly assigned to the individuals according to
 * their rank.
 * </p>
 * <p><img
 *        src="doc-files/linear-rank-selector.gif"
 *        alt="P(i)=\frac{1}{N}\left(n^{-}+\left(n^{+}-n^{-}\right)\frac{i-1}{N-1}\right)"
 *     >
 * </p>
 *
 * Here <i>n</i><sup><i>-</i></sup>/<i>N</i> is the probability of the worst
 * individual to be	selected and <i>n</i><sup><i>+</i></sup>/<i>N</i> the
 * probability of the best individual to be selected. As the population size is
 * held constant, the conditions <i>n</i><sup><i>+</i></sup> = 2 - <i>n</i><sup><i>-</i></sup>
 * and <i>n</i><sup><i>-</i></sup> &gt;= 0 must be fulfilled. Note that all individuals
 * get a different rank, i.e., a different selection probability, even if the
 * have the same fitness value. <p>
 *
 * <i>
 * T. Blickle, L. Thiele, A comparison of selection schemes used
 * in evolutionary algorithms, Technical Report, ETH Zurich, 1997, page 37.
 * <a href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.15.9584">
 *	http://citeseer.ist.psu.edu/blickle97comparison.html
 * </a>
 * </i>
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 2.0
 */
public final class LinearRankSelector<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	extends ProbabilitySelector<G, C>
{
	private final double _nminus;
	private final double _nplus;

	/**
	 * Create a new LinearRankSelector with the given values for {@code nminus}.
	 *
	 * @param nminus {@code nminus/N} is the probability of the worst phenotype
	 *         to be selected.
	 * @throws IllegalArgumentException if {@code nminus < 0}.
	 */
	public LinearRankSelector(final double nminus) {
		super(true);

		if (nminus < 0) {
			throw new IllegalArgumentException(format(
				"nminus is smaller than zero: %s", nminus
			));
		}

		_nminus = nminus;
		_nplus = 2 - _nminus;
	}

	/**
	 * Create a new LinearRankSelector with {@code nminus := 0.5}.
	 */
	public LinearRankSelector() {
		this(0.5);
	}

	/**
	 * This method sorts the population in descending order while calculating the
	 * selection probabilities.
	 */
	@Override
	protected double[] probabilities(
		final Seq<Phenotype<G, C>> population,
		final int count
	) {
		assert population != null : "Population must not be null. ";
		assert !population.isEmpty() : "Population is empty.";
		assert count > 0 : "Population to select must be greater than zero. ";

		final double N = population.size();
		final double[] probabilities = new double[population.size()];

		if (N == 1) {
			probabilities[0] = 1;
		} else {
			for (int i = probabilities.length; --i >= 0; ) {
				probabilities[probabilities.length - i - 1] =
					(_nminus + (_nplus - _nminus)*i/(N - 1))/N;
			}
		}

		return probabilities;
	}

	@Override
	public int hashCode() {
		return hash(_nminus, hash(_nplus));
	}

	@Override
	public boolean equals(final Object obj) {
		return obj == this ||
			obj instanceof LinearRankSelector &&
			Double.compare(((LinearRankSelector) obj)._nminus, _nminus) == 0 &&
			Double.compare(((LinearRankSelector)obj)._nplus, _nplus) == 0;
	}

	@Override
	public String toString() {
		return format(
			"%s[(n-)=%f, (n+)=%f]",
			getClass().getSimpleName(), _nminus, _nplus
		);
	}

}

@@ -801,61 +801,8 @@
 mat;
-%0Aimport static io.jenetics.internal.util.Hashes.hash;
 %0A%0Aim
@@ -839,16 +839,17 @@
 *%0A * %3Cp%3E
+s
 %0A * In l
@@ -2289,19 +2289,29 @@
 version 
-2.0
+!__version__!
 %0A */%0Apub
@@ -3858,16 +3858,16 @@
 %09%7D%0A%09%09%7D%0A%0A
+
 %09%09return
@@ -3890,345 +3890,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public int hashCode() %7B%0A%09%09return hash(_nminus, hash(_nplus));%0A%09%7D%0A%0A%09@Override%0A%09public boolean equals(final Object obj) %7B%0A%09%09return obj == this %7C%7C%0A%09%09%09obj instanceof LinearRankSelector &&%0A%09%09%09Double.compare(((LinearRankSelector) obj)._nminus, _nminus) == 0 &&%0A%09%09%09Double.compare(((LinearRankSelector)obj)._nplus, _nplus) == 0;%0A%09%7D%0A%0A
 %09@Ov

Remove 'equals' and 'hashCode' from 'LinearRankSelector'.