io.jenetics.ExponentialRankSelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jenetics Show documentation
Show all versions of jenetics Show documentation
Jenetics - Java Genetic Algorithm Library
/*
* Java Genetic Algorithm Library (jenetics-7.1.2).
* Copyright (c) 2007-2023 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 io.jenetics;
import static java.lang.Double.compare;
import static java.lang.Math.pow;
import static java.lang.String.format;
import io.jenetics.util.Seq;
/**
*
* An alternative to the "weak" {@code LinearRankSelector} is to assign
* survival probabilities to the sorted individuals using an exponential
* function.
*
* ,
*
* where c must within the range {@code [0..1)}.
*
*
* A small value of c increases the probability of the best phenotypes to
* be selected. If c 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.
*
*
* This selector sorts the population in descending order while calculating the
* selection probabilities.
*
*
* @author Franz Wilhelmstötter
* @since 1.0
* @version 5.0
*/
public final class ExponentialRankSelector<
G extends Gene, G>,
C extends Comparable super C>
>
extends ProbabilitySelector
{
private final double _c;
/**
* Create a new exponential rank selector.
*
* @param c the c value.
* @throws IllegalArgumentException if {@code c} is not within the range
* {@code [0..1)}.
*/
public ExponentialRankSelector(final double c) {
super(true);
if (compare(c, 0) < 0 || compare(c, 1) >= 0) {
throw new IllegalArgumentException(format(
"Value %f 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> 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 String toString() {
return format("%s[c=%f]", getClass().getSimpleName(), _c);
}
}