001/*
002 * Java Genetic Algorithm Library (jenetics-9.0.0).
003 * Copyright (c) 2007-2026 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 * {@snippet lang="java":
038 * final Function<? super Genotype<G>, ? extends C> fitness = null; // @replace substring='null' replacement="..."
039 * final Evaluator<G, C> evaluator = population -> population
040 *     .map(pt -> pt.eval(fitness))
041 *     .asISeq();
042 *
043 * final Engine<G, C> engine = new Engine.Builder<>(evaluator, genotypeFactory)
044 *     .build();
045 * }
046 *
047 * @apiNote
048 * The size of the returned, evaluated, phenotype sequence must be exactly
049 * the size of the input phenotype sequence, and all phenotypes must have a
050 * fitness value assigned ({@code assert population.forAll(Phenotype::isEvaluated);}).
051 * It is allowed to return the input sequence, after evaluation, as well as a newly
052 * created one.
053 *
054 * @see Evaluators
055 * @see Engine
056 *
057 * @param <G> the gene type
058 * @param <C> the fitness result type
059 *
060 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
061 * @version 5.0
062 * @since 4.2
063 */
064@FunctionalInterface
065public interface Evaluator<
066        G extends Gene<?, G>,
067        C extends Comparable<? super C>
068> {
069
070        /**
071         * Evaluates the fitness values of the given {@code population}. The
072         * given {@code population} might contain already evaluated individuals.
073         * It is the responsibility of the implementer to filter out already
074         * evaluated individuals, if desired.
075         *
076         * @param population the population to evaluate
077         * @return the evaluated population. Implementers are free to return the
078         *         input population or a newly created one.
079         */
080        ISeq<Phenotype<G, C>> eval(final Seq<Phenotype<G, C>> population);
081
082}