001 /*
002 * Java Genetic Algorithm Library (jenetics-4.1.0).
003 * Copyright (c) 2007-2018 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 */
020 package io.jenetics.engine;
021
022 import java.util.function.Function;
023 import java.util.function.Predicate;
024 import java.util.function.Supplier;
025 import java.util.stream.Stream;
026
027 import io.jenetics.Gene;
028 import io.jenetics.internal.engine.EvolutionStreamImpl;
029
030 /**
031 * The {@code EvolutionStream} class extends the Java {@link Stream} and adds a
032 * method for limiting the evolution by a given predicate.
033 *
034 * @see java.util.stream.Stream
035 * @see Engine
036 * @see EvolutionStreamable
037 *
038 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
039 * @since 3.0
040 * @version 4.1
041 */
042 public interface EvolutionStream<
043 G extends Gene<?, G>,
044 C extends Comparable<? super C>
045 >
046 extends Stream<EvolutionResult<G, C>>
047 {
048
049 /**
050 * Returns a stream consisting of the elements of this stream, truncated
051 * when the given {@code proceed} predicate returns {@code false}.
052 * <p>
053 * <i>General usage example:</i>
054 * <pre>{@code
055 * final Phenotype<DoubleGene, Double> result = engine.stream()
056 * // Truncate the evolution stream after 5 "steady" generations.
057 * .limit(bySteadyFitness(5))
058 * // The evolution will stop after maximal 100 generations.
059 * .limit(100)
060 * .collect(toBestPhenotype());
061 * }</pre>
062 *
063 * @see Limits
064 *
065 * @param proceed the predicate which determines whether the stream is
066 * truncated or not. <i>If the predicate returns {@code false}, the
067 * evolution stream is truncated.</i>
068 * @return the new stream
069 * @throws NullPointerException if the given predicate is {@code null}.
070 */
071 public EvolutionStream<G, C>
072 limit(final Predicate<? super EvolutionResult<G, C>> proceed);
073
074 /**
075 * Create a new {@code EvolutionStream} from the given {@code start}
076 * population and {@code evolution} function. The main purpose of this
077 * factory method is to simplify the creation of an {@code EvolutionStream}
078 * from an own evolution (GA) engine.
079 *
080 * <pre>{@code
081 * final Supplier<EvolutionStart<DoubleGene, Double>> start = ...
082 * final EvolutionStream<DoubleGene, Double> stream =
083 * EvolutionStream.of(start, new MySpecialEngine());
084 * }</pre>
085 *
086 * A more complete example for would look like as:
087 *
088 * <pre>{@code
089 * public final class SpecialEngine {
090 *
091 * // The fitness function.
092 * private static Double fitness(final Genotype<DoubleGene> gt) {
093 * return gt.getGene().getAllele();
094 * }
095 *
096 * // Create new evolution start object.
097 * private static EvolutionStart<DoubleGene, Double>
098 * start(final int populationSize, final long generation) {
099 * final Population<DoubleGene, Double> population =
100 * Genotype.of(DoubleChromosome.of(0, 1)).instances()
101 * .map(gt -> Phenotype.of(gt, generation, SpecialEngine::fitness))
102 * .limit(populationSize)
103 * .collect(Population.toPopulation());
104 *
105 * return EvolutionStart.of(population, generation);
106 * }
107 *
108 * // The special evolution function.
109 * private static EvolutionResult<DoubleGene, Double>
110 * evolve(final EvolutionStart<DoubleGene, Double> start) {
111 * // Your special evolution implementation comes here!
112 * return null;
113 * }
114 *
115 * public static void main(final String[] args) {
116 * final Genotype<DoubleGene> best = EvolutionStream
117 * .of(() -> start(50, 0), SpecialEngine::evolve)
118 * .limit(Limits.bySteadyFitness(10))
119 * .limit(1000)
120 * .collect(EvolutionResult.toBestGenotype());
121 *
122 * System.out.println(String.format("Best Genotype: %s", best));
123 * }
124 * }
125 * }</pre>
126 *
127 *
128 * @since 3.1
129 *
130 * @param <G> the gene type
131 * @param <C> the fitness type
132 * @param start the evolution start
133 * @param evolution the evolution function
134 * @return a new {@code EvolutionStream} with the given {@code start} and
135 * {@code evolution} function
136 * @throws java.lang.NullPointerException if one of the arguments is
137 * {@code null}
138 */
139 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
140 EvolutionStream<G, C> of(
141 final Supplier<EvolutionStart<G, C>> start,
142 final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> evolution
143 ) {
144 return new EvolutionStreamImpl<>(start, evolution);
145 }
146
147 }
|