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.ext;
021
022import static java.lang.String.format;
023import static java.util.Objects.requireNonNull;
024
025import io.jenetics.Gene;
026import io.jenetics.Optimize;
027import io.jenetics.Phenotype;
028import io.jenetics.Selector;
029import io.jenetics.stat.MinMax;
030import io.jenetics.util.ISeq;
031import io.jenetics.util.MSeq;
032import io.jenetics.util.Seq;
033
034/**
035 * Selector implementation which is part of the
036 * <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
037 * algorithm. The <i>Weasel program</i> is a thought experiment by Richard
038 * Dawkins to illustrate the functioning of the evolution: random <i>mutation</i>
039 * combined with non-random cumulative <i>selection</i>.
040 * <p>
041 * The selector always returns populations which only contains "{@code count}"
042 * instances of the <i>best</i> {@link Phenotype}.
043 * </p>
044 * {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
045 * {@snippet lang="java":
046 * final Engine<CharacterGene, Integer> engine = Engine.builder(problem)
047 *      // Set the 'WeaselSelector'.
048 *     .selector(new WeaselSelector<>())
049 *      // Disable survivors selector.
050 *     .offspringFraction(1)
051 *      // Set the 'WeaselMutator'.
052 *     .alterers(new WeaselMutator<>(0.05))
053 *     .build();
054 * }
055 *
056 * @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
057 * @see WeaselMutator
058 *
059 * @param <G> the gene type
060 * @param <C> the fitness result type
061 *
062 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
063 * @since 3.5
064 * @version 5.0
065 */
066public class WeaselSelector<
067        G extends Gene<?, G>,
068        C extends Comparable<? super C>
069>
070        implements Selector<G, C>
071{
072        @Override
073        public ISeq<Phenotype<G, C>> select(
074                final Seq<Phenotype<G, C>> population,
075                final int count,
076                final Optimize opt
077        ) {
078                requireNonNull(population, "Population");
079                requireNonNull(opt, "Optimization");
080                if (count < 0) {
081                        throw new IllegalArgumentException(format(
082                                "Selection count must be greater or equal then zero, but was %s",
083                                count
084                        ));
085                }
086
087                final MinMax<Phenotype<G, C>> minMax = population.stream()
088                        .collect(MinMax.toMinMax(opt.ascending()));
089
090                final MSeq<Phenotype<G, C>> result = MSeq.ofLength(count);
091                return result.fill(minMax::max).toISeq();
092        }
093
094        @Override
095        public String toString() {
096                return "WeaselSelector";
097        }
098
099}