001/*
002 * Java Genetic Algorithm Library (jenetics-8.1.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics;
021
022import static java.lang.String.format;
023
024import io.jenetics.util.Seq;
025
026/**
027 * <p>
028 * In linear-ranking selection the individuals are sorted according to their
029 * fitness values. The rank <i>N</i> is assignee to the best individual and the
030 * rank 1 to the worst individual. The selection probability <i>P(i)</i>  of
031 * individual <i>i</i> is linearly assigned to the individuals according to
032 * their rank.
033 * </p>
034 * <p><img
035 *        src="doc-files/linear-rank-selector.svg"
036 *        alt="P(i)=\frac{1}{N}\left(n^{-}+\left(n^{+}-n^{-}\right)\frac{i-1}{N-1}\right)"
037 *     >
038 * </p>
039 *
040 * Here <i>n</i><sup><i>-</i></sup>/<i>N</i> is the probability of the worst
041 * individual to be     selected and <i>n</i><sup><i>+</i></sup>/<i>N</i> the
042 * probability of the best individual to be selected. As the population size is
043 * held constant, the conditions <i>n</i><sup><i>+</i></sup> = 2 - <i>n</i><sup><i>-</i></sup>
044 * and <i>n</i><sup><i>-</i></sup> &gt;= 0 must be fulfilled. Note that all individuals
045 * get a different rank, i.e., a different selection probability, even if the
046 * have the same fitness value. <p>
047 *
048 * <i>
049 * T. Blickle, L. Thiele, A comparison of selection schemes used
050 * in evolutionary algorithms, Technical Report, ETH Zurich, 1997, page 37.
051 * <a href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.15.9584">
052 *      http://citeseer.ist.psu.edu/blickle97comparison.html
053 * </a>
054 * </i>
055 *
056 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
057 * @since 1.0
058 * @version 5.0
059 */
060public final class LinearRankSelector<
061        G extends Gene<?, G>,
062        C extends Comparable<? super C>
063>
064        extends ProbabilitySelector<G, C>
065{
066        private final double _nminus;
067        private final double _nplus;
068
069        /**
070         * Create a new LinearRankSelector with the given values for {@code nminus}.
071         *
072         * @param nminus {@code nminus/N} is the probability of the worst phenotype
073         *         to be selected.
074         * @throws IllegalArgumentException if {@code nminus < 0}.
075         */
076        public LinearRankSelector(final double nminus) {
077                super(true);
078
079                if (nminus < 0) {
080                        throw new IllegalArgumentException(format(
081                                "nminus is smaller than zero: %s", nminus
082                        ));
083                }
084
085                _nminus = nminus;
086                _nplus = 2 - _nminus;
087        }
088
089        /**
090         * Create a new LinearRankSelector with {@code nminus := 0.5}.
091         */
092        public LinearRankSelector() {
093                this(0.5);
094        }
095
096        /**
097         * This method sorts the population in descending order while calculating the
098         * selection probabilities.
099         */
100        @Override
101        protected double[] probabilities(
102                final Seq<Phenotype<G, C>> population,
103                final int count
104        ) {
105                assert population != null : "Population must not be null. ";
106                assert !population.isEmpty() : "Population is empty.";
107                assert count > 0 : "Population to select must be greater than zero. ";
108
109                final double N = population.size();
110                final double[] probabilities = new double[population.size()];
111
112                if (N == 1) {
113                        probabilities[0] = 1;
114                } else {
115                        for (int i = probabilities.length; --i >= 0; ) {
116                                probabilities[probabilities.length - i - 1] =
117                                        (_nminus + (_nplus - _nminus)*i/(N - 1))/N;
118                        }
119                }
120
121                return probabilities;
122        }
123
124        @Override
125        public String toString() {
126                return format(
127                        "%s[(n-)=%f, (n+)=%f]",
128                        getClass().getSimpleName(), _nminus, _nplus
129                );
130        }
131
132}