limit.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.0.0).
03  * Copyright (c) 2007-2014 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@gmx.at)
19  */
20 package org.jenetics.engine;
21 
22 import java.util.function.Predicate;
23 
24 import org.jenetics.internal.util.require;
25 
26 /**
27  * This class contains factory methods for creating predicates, which can be
28  * used for limiting the evolution stream.
29  *
30  @see EvolutionStream#limit(Predicate)
31  *
32  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
33  @since 3.0
34  @version 3.0 &mdash; <em>$Date: 2014-12-28 $</em>
35  */
36 public final class limit {
37     private limit() {require.noInstance();}
38 
39 
40     /**
41      * Return a predicate, which will truncate the evolution stream if no
42      * better phenotype could be found after the given number of
43      * {@code generations}.
44      *
45      * [code]
46      * final Phenotype&lt;DoubleGene, Double&gt; result = engine.stream()
47      *      // Truncate the evolution stream after 5 "steady" generations.
48      *     .limit(bySteadyFitness(5))
49      *      // The evolution will stop after maximal 100 generations.
50      *     .limit(100)
51      *     .collect(toBestPhenotype());
52      * [/code]
53      *
54      @param generations the number of <i>steady</i> generations
55      @param <C> the fitness type
56      @return a predicate which truncate the evolution stream if no better
57      *         phenotype could be found after a give number of
58      *         {@code generations}
59      @throws IllegalArgumentException if the generation is smaller than
60      *         one.
61      */
62     public static <C extends Comparable<? super C>>
63     Predicate<EvolutionResult<?, C>> bySteadyFitness(final int generations) {
64         return new SteadyFitnessLimit<>(generations);
65     }
66 
67 }