org.jenetics.LinearRankSelector Maven / Gradle / Ivy
Show all versions of org.jenetics Show documentation
/*
* Java Genetic Algorithm Library (jenetics-3.1.0).
* Copyright (c) 2007-2015 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 ([email protected])
*/
package org.jenetics;
import static java.lang.String.format;
import static org.jenetics.internal.util.Equality.eq;
import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;
/**
*
* In linear-ranking selection the individuals are sorted according to their
* fitness values. The rank N is assignee to the best individual and the
* rank 1 to the worst individual. The selection probability P(i) of
* individual i is linearly assigned to the individuals according to
* their rank.
*
*
*
*
* Here n-/N is the probability of the worst
* individual to be selected and n+/N the
* probability of the best individual to be selected. As the population size is
* held constant, the conditions n+ = 2 - n-
* and n- >= 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.
*
*
* T. Blickle, L. Thiele, A comparison of selection schemes used
* in evolutionary algorithms, Technical Report, ETH Zurich, 1997, page 37.
*
* http://citeseer.ist.psu.edu/blickle97comparison.html
*
*
*
* @author Franz Wilhelmstötter
* @since 1.0
* @version 2.0
*/
public final class LinearRankSelector<
G extends Gene, G>,
C extends Comparable super C>
>
extends ProbabilitySelector
{
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. (The method {@link Population#populationSort()} is called
* by this method.)
*/
@Override
protected double[] probabilities(
final Population population,
final int count
) {
assert(population != null) : "Population can not be null. ";
assert(count > 0) : "Population to select must be greater than zero. ";
//Sort the population.
population.populationSort();
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;
}
}
assert (sum2one(probabilities)) : "Probabilities doesn't sum to one.";
return probabilities;
}
@Override
public int hashCode() {
return Hash.of(getClass()).and(_nminus).and(_nplus).value();
}
@Override
public boolean equals(final Object obj) {
return Equality.of(this, obj).test(selector ->
eq(_nminus, selector._nminus) &&
eq(_nplus, selector._nplus)
);
}
@Override
public String toString() {
return format(
"%s[(n-)=%f, (n+)=%f]",
getClass().getSimpleName(), _nminus, _nplus
);
}
}