001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.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 io.jenetics.util.ISeq;
023import io.jenetics.util.Seq;
024
025/**
026 * Selectors are responsible for selecting a given number of individuals from
027 * the population. The selectors are used to divide the population into
028 * survivors and offspring. The selectors for offspring and for the survivors
029 * can be chosen independently.
030 * {@snippet lang="java":
031 * final Engine<DoubleGene, Double> engine = Engine
032 *     .builder(gtf, ff)
033 *     .offspringSelector(new RouletteWheelSelector<>())
034 *     .survivorsSelector(new TournamentSelector<>())
035 *     .build();
036 * }
037 *
038 * @param <G> The gene type this GA evaluates,
039 * @param <C> The result type (of the fitness function).
040 *
041 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
042 * @since 1.0
043 * @version 4.0
044 */
045@FunctionalInterface
046public interface Selector<
047        G extends Gene<?, G>,
048        C extends Comparable<? super C>
049> {
050
051        /**
052         * Select phenotypes from the Population.
053         *
054         * @param population The population to select from.
055         * @param count The number of phenotypes to select.
056         * @param opt Determines whether the individuals with higher fitness values
057         *        or lower fitness values must be selected. This parameter determines
058         *        whether the GA maximizes or minimizes the fitness function.
059         * @return The selected phenotypes (a new Population).
060         * @throws NullPointerException if the arguments is {@code null}.
061         * @throws IllegalArgumentException if the select count is smaller than zero.
062         */
063        ISeq<Phenotype<G, C>> select(
064                final Seq<Phenotype<G, C>> population,
065                final int count,
066                final Optimize opt
067        );
068
069}