EvolutionStreamable.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.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 static java.util.Objects.requireNonNull;
023 
024 import java.util.function.Predicate;
025 import java.util.function.Supplier;
026 
027 import io.jenetics.Gene;
028 import io.jenetics.Genotype;
029 import io.jenetics.Phenotype;
030 import io.jenetics.util.ISeq;
031 
032 /**
033  * This interface defines the capability of creating {@link EvolutionStream}s
034  * from a given {@link EvolutionStart} object. It also decouples the engine's
035  * capability from the capability to create evolution streams. The purpose of
036  * this interface is similar to the {@link Iterable} interface.
037  *
038  @see EvolutionStream
039  *
040  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
041  @version 4.1
042  @since 4.1
043  */
044 public interface EvolutionStreamable<
045     extends Gene<?, G>,
046     extends Comparable<? super C>
047 {
048 
049     /**
050      * Create a new, possibly <em>infinite</em>, evolution stream with the given
051      * evolution start. If an empty {@code Population} is given, the engines
052      * genotype factory is used for creating the population. The given
053      * population might be the result of an other engine and this method allows
054      * to start the evolution with the outcome of an different engine.
055      * The fitness function and the fitness scaler are replaced by the one
056      * defined for this engine.
057      *
058      @param start the data the evolution stream starts with
059      @return a new <b>infinite</b> evolution stream
060      @throws java.lang.NullPointerException if the given evolution
061      *         {@code start} is {@code null}.
062      */
063     public EvolutionStream<G, C>
064     stream(final Supplier<EvolutionStart<G, C>> start);
065 
066     /**
067      * Create a new, possibly <em>infinite</em>, evolution stream with the given
068      * initial value. If an empty {@code Population} is given, the engines genotype
069      * factory is used for creating the population. The given population might
070      * be the result of an other engine and this method allows to start the
071      * evolution with the outcome of an different engine. The fitness function
072      * and the fitness scaler are replaced by the one defined for this engine.
073      *
074      @param init the data the evolution stream is initialized with
075      @return a new <b>infinite</b> evolution stream
076      @throws java.lang.NullPointerException if the given evolution
077      *         {@code start} is {@code null}.
078      */
079     public EvolutionStream<G, C> stream(final EvolutionInit<G> init);
080 
081     /* *************************************************************************
082      * Default interface methods.
083      * ************************************************************************/
084 
085     /**
086      * Create a new, possibly <em>infinite</em>, evolution stream with a newly
087      * created population. This method is a shortcut for
088      <pre>{@code
089      * final EvolutionStream<G, C> stream = streamable
090      *     .stream(() -> EvolutionStart.of(ISeq.empty(), 1));
091      * }</pre>
092      *
093      @return a new evolution stream.
094      */
095     public default EvolutionStream<G, C> stream() {
096         return stream(() -> EvolutionStart.of(ISeq.empty()1));
097     }
098 
099     /**
100      * Create a new, possibly <em>infinite</em>, evolution stream with the given
101      * evolution start. If an empty {@code Population} is given, the engines genotype
102      * factory is used for creating the population. The given population might
103      * be the result of an other engine and this method allows to start the
104      * evolution with the outcome of an different engine. The fitness function
105      * and the fitness scaler are replaced by the one defined for this engine.
106      *
107      @param start the data the evolution stream starts with
108      @return a new <b>infinite</b> evolution iterator
109      @throws java.lang.NullPointerException if the given evolution
110      *         {@code start} is {@code null}.
111      */
112     public default EvolutionStream<G, C>
113     stream(final EvolutionStart<G, C> start) {
114         return stream(() -> start);
115     }
116 
117     /**
118      * Create a new {@code EvolutionStream} starting with a previously evolved
119      {@link EvolutionResult}. The stream is initialized with the population
120      * of the given {@code result} and its total generation
121      {@link EvolutionResult#getTotalGenerations()}.
122      *
123      <pre>{@code
124      * private static final Problem<Double, DoubleGene, Double>
125      * PROBLEM = Problem.of(
126      *     x -> cos(0.5 + sin(x))*cos(x),
127      *     Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI))
128      * );
129      *
130      * private static final Engine<DoubleGene, Double>
131      * ENGINE = Engine.builder(PROBLEM)
132      *     .optimize(Optimize.MINIMUM)
133      *     .offspringSelector(new RouletteWheelSelector<>())
134      *     .build();
135      *
136      * public static void main(final String[] args) throws IOException {
137      *     // Result of the first evolution run.
138      *     final EvolutionResult<DoubleGene, Double> rescue = ENGINE.stream()
139      *         .limit(Limits.bySteadyFitness(10))
140      *         .collect(EvolutionResult.toBestEvolutionResult());
141      *
142      *     // Save the result of the first run into a file.
143      *     final Path path = Paths.get("result.bin");
144      *     IO.object.write(rescue, path);
145      *
146      *     // Load the previous result and continue evolution.
147      *     \@SuppressWarnings("unchecked")
148      *     final EvolutionResult<DoubleGene, Double> result = ENGINE
149      *         .stream((EvolutionResult<DoubleGene, Double>)IO.object.read(path))
150      *         .limit(Limits.bySteadyFitness(20))
151      *         .collect(EvolutionResult.toBestEvolutionResult());
152      *
153      *     System.out.println(result.getBestPhenotype());
154      * }
155      * }</pre>
156      *
157      * The example above shows how to save an {@link EvolutionResult} from a
158      * first run, save it to disk and continue the evolution.
159      *
160      @param result the previously evolved {@code EvolutionResult}
161      @return a new evolution stream, which continues a previous one
162      @throws NullPointerException if the given evolution {@code result} is
163      *         {@code null}
164      */
165     public default EvolutionStream<G, C>
166     stream(final EvolutionResult<G, C> result) {
167         return stream(EvolutionStart.of(
168             result.getPopulation(),
169             result.getGeneration()
170         ));
171     }
172 
173     /**
174      * Create a new, possibly <em>infinite</em>, evolution stream with the given
175      * initial population. If an empty {@code Population} is given, the engines
176      * genotype factory is used for creating the population. The given population
177      * might be the result of an other engine and this method allows to start the
178      * evolution with the outcome of an different engine. The fitness function
179      * and the fitness scaler are replaced by the one defined for this engine.
180      *
181      @param population the initial individuals used for the evolution stream.
182      *        Missing individuals are created and individuals not needed are
183      *        skipped.
184      @param generation the generation the stream starts from; must be greater
185      *        than zero.
186      @return a new evolution stream.
187      @throws java.lang.NullPointerException if the given {@code population} is
188      *         {@code null}.
189      @throws IllegalArgumentException if the given {@code generation} is
190      *         smaller then one
191      */
192     public default EvolutionStream<G, C> stream(
193         final ISeq<Phenotype<G, C>> population,
194         final long generation
195     ) {
196         return stream(EvolutionStart.of(population, generation));
197     }
198 
199     /**
200      * Create a new, possibly <em>infinite</em>, evolution stream with the given
201      * initial population. If an empty {@code Population} is given, the engines
202      * genotype factory is used for creating the population. The given population
203      * might be the result of an other engine and this method allows to start the
204      * evolution with the outcome of an different engine. The fitness function
205      * and the fitness scaler are replaced by the one defined for this engine.
206      *
207      @param population the initial individuals used for the evolution stream.
208      *        Missing individuals are created and individuals not needed are
209      *        skipped.
210      @return a new evolution stream.
211      @throws java.lang.NullPointerException if the given {@code population} is
212      *         {@code null}.
213      */
214     public default EvolutionStream<G, C>
215     stream(final ISeq<Phenotype<G, C>> population) {
216         return stream(EvolutionStart.of(population, 1));
217     }
218 
219     /**
220      * Create a new, possibly <em>infinite</em>, evolution stream with the given
221      * initial individuals. If an empty {@code Iterable} is given, the engines
222      * genotype factory is used for creating the population.
223      *
224      @param genotypes the initial individuals used for the evolution stream.
225      *        Missing individuals are created and individuals not needed are
226      *        skipped.
227      @param generation the generation the stream starts from; must be greater
228      *        than zero.
229      @return a new evolution stream.
230      @throws java.lang.NullPointerException if the given {@code genotypes} is
231      *         {@code null}.
232      @throws IllegalArgumentException if the given {@code generation} is
233      *         smaller then one
234      */
235     public default EvolutionStream<G, C> stream(
236         final Iterable<Genotype<G>> genotypes,
237         final long generation
238     ) {
239         return stream(EvolutionInit.of(ISeq.of(genotypes), generation));
240     }
241 
242     /**
243      * Create a new, possibly <em>infinite</em>, evolution stream with the given
244      * initial individuals. If an empty {@code Iterable} is given, the engines
245      * genotype factory is used for creating the population.
246      *
247      @param genotypes the initial individuals used for the evolution stream.
248      *        Missing individuals are created and individuals not needed are
249      *        skipped.
250      @return a new evolution stream.
251      @throws java.lang.NullPointerException if the given {@code genotypes} is
252      *         {@code null}.
253      */
254     public default EvolutionStream<G, C>
255     stream(final Iterable<Genotype<G>> genotypes) {
256         return stream(genotypes, 1);
257     }
258 
259     /**
260      * Return a new {@code EvolutionStreamable} instance where all created
261      * {@code EvolutionStream}s are limited by the given predicate. Since some
262      * predicates has to maintain internal state, a predicate {@code Supplier}
263      * must be given instead a plain limiting predicate.
264      *
265      @param proceed the limiting predicate supplier.
266      @return a new evolution streamable object
267      @throws NullPointerException if the give {@code predicate} is {@code null}
268      */
269     public default EvolutionStreamable<G, C>
270     limit(final Supplier<Predicate<? super EvolutionResult<G, C>>> proceed) {
271         requireNonNull(proceed);
272 
273         return new EvolutionStreamable<G, C>() {
274             @Override
275             public EvolutionStream<G, C>
276             stream(final Supplier<EvolutionStart<G, C>> start) {
277                 return EvolutionStreamable.this.stream(start).limit(proceed.get());
278             }
279 
280             @Override
281             public EvolutionStream<G, C> stream(final EvolutionInit<G> init) {
282                 return EvolutionStreamable.this.stream(init).limit(proceed.get());
283             }
284         };
285     }
286 
287     /**
288      * Return a new {@code EvolutionStreamable} instance where all created
289      * {@code EvolutionStream}s are limited to the given number of generations.
290      *
291      @param generations the number of generations after the created evolution
292      *        streams are truncated
293      @return a new evolution streamable object
294      @throws IllegalArgumentException if the given {@code generations} is
295      *         smaller than zero.
296      */
297     public default EvolutionStreamable<G, C> limit(final long generations) {
298         return limit(() -> Limits.byFixedGeneration(generations));
299     }
300 
301 }