/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial individuals. If an empty {@code Iterable} is given, the engines * genotype factory is used for creating the population. * * @param genotypes the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code genotypes} is * {@code null}. */ public default EvolutionStream<G, C> stream(final Iterable<Genotype<G>> genotypes) { return stream(genotypes, 1); }
/** * Return a new {@code EvolutionStreamable} instance where all created * {@code EvolutionStream}s are limited to the given number of generations. * * @param generations the number of generations after the created evolution * streams are truncated * @return a new evolution streamable object * @throws IllegalArgumentException if the given {@code generations} is * smaller than zero. */ public default EvolutionStreamable<G, C> limit(final long generations) { return limit(() -> Limits.byFixedGeneration(generations)); }
@Test(dataProvider = "generations") public void engineGenerationLimit2(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine .limit(() -> Limits.byFixedGeneration(generations)) .limit(() -> Limits.byFixedGeneration(Math.min(generations, 5))) .stream() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(Math.min(generations, 5), result.getTotalGenerations()); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * evolution start. If an empty {@code Population} is given, the engines genotype * factory is used for creating the population. The given population might * be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @param start the data the evolution stream starts with * @return a new <b>infinite</b> evolution iterator * @throws java.lang.NullPointerException if the given evolution * {@code start} is {@code null}. */ public default EvolutionStream<G, C> stream(final EvolutionStart<G, C> start) { return stream(() -> start); }
@Override public EvolutionStream<G, C> stream(final Supplier<EvolutionStart<G, C>> start) { return EvolutionStreamable.this.stream(start).limit(proceed.get()); }
@Override public EvolutionStream<G, C> stream(final EvolutionInit<G> init) { return EvolutionStreamable.this.stream(init).limit(proceed.get()); } };
private Spliterator<EvolutionResult<G, C>> generate( final Supplier<EvolutionStart<G, C>> start, final EvolutionResult<G, C> result ) { final EvolutionStart<G, C> es = result == null ? start.get() : result.toEvolutionStart(); return _engine.apply(result) .stream(es) .spliterator(); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial population. If an empty {@code Population} is given, the engines * genotype factory is used for creating the population. The given population * might be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @param population the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. */ public default EvolutionStream<G, C> stream(final ISeq<Phenotype<G, C>> population) { return stream(EvolutionStart.of(population, 1)); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial population. If an empty {@code Population} is given, the engines * genotype factory is used for creating the population. The given population * might be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @param population the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @param generation the generation the stream starts from; must be greater * than zero. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ public default EvolutionStream<G, C> stream( final ISeq<Phenotype<G, C>> population, final long generation ) { return stream(EvolutionStart.of(population, generation)); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with a newly * created population. This method is a shortcut for * <pre>{@code * final EvolutionStream<G, C> stream = streamable * .stream(() -> EvolutionStart.of(ISeq.empty(), 1)); * }</pre> * * @return a new evolution stream. */ public default EvolutionStream<G, C> stream() { return stream(() -> EvolutionStart.of(ISeq.empty(), 1)); }
result = singletonList( _engines.get(0) .stream(init) .peek(er -> other.set(er.toEvolutionStart())) .spliterator() .stream(init) .peek(er -> other.set(er.toEvolutionStart())) .spliterator() _engines.subList(1, _engines.size()).stream() .map(engine -> engine .stream(other::get) .peek(er -> other.set(er.toEvolutionStart()))) .map(BaseStream::spliterator)
@Override public EvolutionStream<G, C> stream(final Supplier<EvolutionStart<G, C>> start) { final AtomicReference<EvolutionStart<G, C>> other = new AtomicReference<>(null); return new EvolutionStreamImpl<G, C>( new ConcatSpliterator<>( _engines.stream() .map(engine -> engine .stream(() -> start(start, other)) .peek(result -> other.set(result.toEvolutionStart()))) .map(BaseStream::spliterator) .collect(Collectors.toList()) ), false ); }
private Supplier<Spliterator<EvolutionResult<G, C>>> toSpliterator( final EvolutionStreamable<G, C> engine, final EvolutionInit<G> init, final AtomicReference<EvolutionStart<G, C>> other, final AtomicBoolean first ) { return () -> { if (first.get()) { first.set(false); return engine.stream(init) .peek(result -> other.set(result.toEvolutionStart())) .spliterator(); } else { return engine.stream(other::get) .peek(result -> other.set(result.toEvolutionStart())) .spliterator(); } }; }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial individuals. If an empty {@code Iterable} is given, the engines * genotype factory is used for creating the population. * * @param genotypes the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @param generation the generation the stream starts from; must be greater * than zero. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code genotypes} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ public default EvolutionStream<G, C> stream( final Iterable<Genotype<G>> genotypes, final long generation ) { return stream(EvolutionInit.of(ISeq.of(genotypes), generation)); }
private Supplier<Spliterator<EvolutionResult<G, C>>> toSpliterator( final EvolutionStreamable<G, C> engine, final Supplier<EvolutionStart<G, C>> start, final AtomicReference<EvolutionStart<G, C>> other ) { return () -> engine.stream(() -> start(start, other)) .peek(result -> other.set(result.toEvolutionStart())) .spliterator(); }
return stream(EvolutionStart.of( result.getPopulation(), result.getGeneration()
@Test public void cycle1() { final EvolutionStreamable<DoubleGene, Double> engine = CyclicEngine.of( _engine1.limit(10), _engine2.limit(10), _engine3.limit(10) ); final AtomicLong count = new AtomicLong(0); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.intValue(), 100); Assert.assertEquals(result.getTotalGenerations(), 100); }
@Test public void cycle2() { final EvolutionStreamable<DoubleGene, Double> engine = CyclicEngine.of( _engine1.limit(10), _engine2.limit(10), _engine3.limit(10) ); final AtomicLong count = new AtomicLong(0); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFixedGeneration(50)) .limit(100) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.intValue(), 50); Assert.assertEquals(result.getTotalGenerations(), 50); }
@Test public void cycle3() { final EvolutionStreamable<DoubleGene, Double> engine = CyclicEngine.of( _engine1.limit(10), _engine2.limit(10), _engine3.limit(10) ); final AtomicLong count = new AtomicLong(0); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFixedGeneration(50)) .limit(15) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.intValue(), 15); Assert.assertEquals(result.getTotalGenerations(), 15); }