/*
 * 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.Math.pow;
import static java.lang.String.format;
import static io.jenetics.internal.util.Hashes.hash;

import java.util.Objects;

import io.jenetics.util.Seq;

/**
 * <p>
 * An alternative to the "weak" {@code LinearRankSelector} is to assign
 * survival probabilities to the sorted individuals using an exponential
 * function.
 * </p>
 * <p><img
 *        src="doc-files/exponential-rank-selector.gif"
 *        alt="P(i)=\left(c-1\right)\frac{c^{i-1}}{c^{N}-1}"
 *     >,
 * </p>
 * where <i>c</i> must within the range {@code [0..1)}.
 *
 * <p>
 * A small value of <i>c</i> increases the probability of the best phenotypes to
 * be selected. If <i>c</i> is set to zero, the selection probability of the best
 * phenotype is set to one. The selection probability of all other phenotypes is
 * zero. A value near one equalizes the selection probabilities.
 * </p>
 * <p>
 * This selector sorts the population in descending order while calculating the
 * selection probabilities.
 * </p>
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 4.0
 */
public final class ExponentialRankSelector<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	extends ProbabilitySelector<G, C>
{

	private final double _c;

	/**
	 * Create a new exponential rank selector.
	 *
	 * @param c the <i>c</i> value.
	 * @throws IllegalArgumentException if {@code c} is not within the range
	 *         {@code [0..1)}.
	 */
	public ExponentialRankSelector(final double c) {
		super(true);

		if (c < 0.0 || c >= 1.0) {
			throw new IllegalArgumentException(format(
				"Value %s is out of range [0..1): ", c
			));
		}
		_c = c;
	}

	/**
	 * Create a new selector with default value of 0.975.
	 */
	public ExponentialRankSelector() {
		this(0.975);
	}

	/**
	 * 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()];

		final double b = (_c - 1.0)/(pow(_c, N) - 1.0);
		for (int i = 0; i < probabilities.length; ++i) {
			probabilities[i] = pow(_c, i)*b;
		}

		return probabilities;
	}

	@Override
	public int hashCode() {
		return hash(_c);
	}

	@Override
	public boolean equals(final Object obj) {
		return obj == this ||
			obj instanceof ExponentialRankSelector &&
			Objects.equals(((ExponentialRankSelector) obj)._c, _c);
	}

	@Override
	public String toString() {
		return format("%s[c=%f]", getClass().getSimpleName(), _c);
	}

}

@@ -1890,11 +1890,21 @@
 ion 
-4.0
+!__version__!
 %0A */
@@ -3292,253 +3292,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public int hashCode() %7B%0A%09%09return hash(_c);%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 ExponentialRankSelector &&%0A%09%09%09Objects.equals(((ExponentialRankSelector) obj)._c, _c);%0A%09%7D%0A%0A
 %09@Ov

Remove 'equals' and 'hashCode' from 'ExponentialRankSelector'.