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.engine;
021
022import io.jenetics.Gene;
023import io.jenetics.Phenotype;
024import io.jenetics.util.ISeq;
025import io.jenetics.util.Seq;
026
027/**
028 * This interface allows defining different strategies for evaluating the
029 * fitness functions of a given population. <em>Normally</em> there is no
030 * need for <em>overriding</em> the default evaluation strategy, but it might
031 * be necessary if you have performance problems and a <em>batched</em>
032 * fitness evaluation would solve the problem.
033 * <p>
034 * The implementer is free to do the evaluation <em>in place</em>, or create
035 * new {@link Phenotype} instance and return the newly created one. A simple
036 * serial evaluator can easily implement:
037 *
038 * {@snippet lang="java":
039 * final Function<? super Genotype<G>, ? extends C> fitness = null; // @replace substring='null' replacement="..."
040 * final Evaluator<G, C> evaluator = population -> population
041 *     .map(pt -> pt.eval(fitness))
042 *     .asISeq();
043 *
044 * final Engine<G, C> engine = new Engine.Builder<>(evaluator, genotypeFactory)
045 *     .build();
046 * }
047 *
048 * @apiNote
049 * The size of the returned, evaluated, phenotype sequence must be exactly
050 * the size of the input phenotype sequence, and all phenotypes must have a
051 * fitness value assigned ({@code assert population.forAll(Phenotype::isEvaluated);}).
052 * It is allowed to return the input sequence, after evaluation, as well as a newly
053 * created one.
054 *
055 * @see Evaluators
056 * @see Engine
057 *
058 * @param <G> the gene type
059 * @param <C> the fitness result type
060 *
061 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
062 * @version 5.0
063 * @since 4.2
064 */
065@FunctionalInterface
066public interface Evaluator<
067        G extends Gene<?, G>,
068        C extends Comparable<? super C>
069> {
070
071        /**
072         * Evaluates the fitness values of the given {@code population}. The
073         * given {@code population} might contain already evaluated individuals.
074         * It is the responsibility of the implementer to filter out already
075         * evaluated individuals, if desired.
076         *
077         * @param population the population to evaluate
078         * @return the evaluated population. Implementers are free to return the
079         *         input population or a newly created one.
080         */
081        ISeq<Phenotype<G, C>> eval(final Seq<Phenotype<G, C>> population);
082
083}