Evaluator.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-5.1.0).
03  * Copyright (c) 2007-2019 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.engine;
21 
22 import io.jenetics.Gene;
23 import io.jenetics.Phenotype;
24 import io.jenetics.util.ISeq;
25 import io.jenetics.util.Seq;
26 
27 /**
28  * This interface allows to define different strategies for evaluating the
29  * fitness functions of a given population. <em>Normally</em>, there is no
30  * need for <em>overriding</em> the default evaluation strategy, but it might
31  * be necessary if you have performance problems and a <em>batched</em>
32  * fitness evaluation would solve the problem.
33  <p>
34  * The implementer is free to do the evaluation <em>in place</em>, or create
35  * new {@link Phenotype} instance and return the newly created one. A simple
36  * serial evaluator can easily implemented:
37  *
38  <pre>{@code
39  * final Function<? super Genotype<G>, ? extends C> fitness = ...;
40  * final Evaluator<G, C> evaluator = population -> population
41  *     .map(pt -> pt.eval(fitness))
42  *     .asISeq();
43  *
44  * final Engine<G, C> engine = new Engine.Builder(evaluator, genotypeFactory)
45  *     .build();
46  * }</pre>
47  *
48  * @apiNote
49  * The size of the returned, evaluated, phenotype sequence must be exactly
50  * the size of the input phenotype sequence and all phenotypes must have a
51  * fitness value assigned ({@code assert population.forAll(Phenotype::isEvaluated);}).
52  * It is allowed to return the input sequence, after evaluation, as well a newly
53  * created one.
54  *
55  @see Evaluators
56  @see Engine
57  *
58  @param <G> the gene type
59  @param <C> the fitness result type
60  *
61  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
62  @version 5.0
63  @since 4.2
64  */
65 @FunctionalInterface
66 public interface Evaluator<
67     extends Gene<?, G>,
68     extends Comparable<? super C>
69 {
70 
71     /**
72      * Evaluates the fitness values of the given {@code population}. The
73      * given {@code population} might contain already evaluated individuals.
74      * It is the responsibility of the implementer to filter out already
75      * evaluated individuals, if desired.
76      *
77      @param population the population to evaluate
78      @return the evaluated population. Implementers are free to return the
79      *         the input population or a newly created one.
80      */
81     public ISeq<Phenotype<G, C>> eval(final Seq<Phenotype<G, C>> population);
82 
83 }