All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jenetics.LinearRankSelector Maven / Gradle / Ivy

There is a newer version: 8.1.0
Show newest version
/*
 * 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.String.format;

import io.jenetics.util.Seq;

/**
 * 

* 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. *

*

P(i)=\frac{1}{N}\left(n^{-}+\left(n^{+}-n^{-}\right)\frac{i-1}{N-1}\right) *

* * 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 5.0 */ public final class LinearRankSelector< G extends Gene, C extends Comparable > 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. */ @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()]; 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 String toString() { return format( "%s[(n-)=%f, (n+)=%f]", getClass().getSimpleName(), _nminus, _nplus ); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy