@Override public EvolutionStream<G, C> stream(final Supplier<EvolutionStart<G, C>> start) { return EvolutionStreamable.this.stream(start).limit(proceed.get()); }
/** * Starts the evolution worker with the given evolution result callback. The * callback may be null. * * @param callback the {@code EvolutionResult} callback. The first parameter * contains the current result and the second the best. */ public void start( final BiConsumer< EvolutionResult<PolygonGene, Double>, EvolutionResult<PolygonGene, Double>> callback ) { final Thread thread = new Thread(() -> { final MinMax<EvolutionResult<PolygonGene, Double>> best = MinMax.of(); _engine.stream() .limit(result -> !Thread.currentThread().isInterrupted()) .peek(best) .forEach(r -> { waiting(); if (callback != null) { callback.accept(r, best.getMax()); } }); }); thread.start(); _thread = thread; }
private EvolutionResult<BitGene, Double> run( final EvolutionResult<BitGene, Double> last, final AtomicBoolean proceed ) { System.out.println("Starting evolution with existing result."); return (last != null ? ENGINE.stream(last) : ENGINE.stream()) .limit(r -> proceed.get()) .collect(EvolutionResult.toBestEvolutionResult()); }
public static void main(final String[] args) { final Genotype<DoubleGene> best = EvolutionStream .of(() -> start(50, 0), SpecialEngine::evolve) .limit(Limits.bySteadyFitness(10)) .limit(1000) .collect(EvolutionResult.toBestGenotype()); System.out.println(String.format("Best Genotype: %s", best)); } }
@Test public void concat3() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of(streamable(5)) .stream() .limit(Limits.byFixedGeneration(3)); final int[] array = stream .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3}); }
@Test public void spliterator() { final Engine<DoubleGene, Double> engine = Engine .builder( gt -> gt.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionStream<DoubleGene, Double> stream = engine.stream() .limit(Limits.byFixedGeneration(10)); final Spliterator<EvolutionResult<DoubleGene, Double>> spliterator = stream.spliterator(); final long count = StreamSupport.stream(spliterator, false) .count(); Assert.assertEquals(count, 10); }
@Test(dataProvider = "durations") public void test(final Integer millis) { final CountClock clock = new CountClock(); final Duration duration = Duration.ofMillis(millis); final AtomicInteger count = new AtomicInteger(); stream() .limit(Limits.byExecutionTime(duration, clock)) .forEach(s -> count.incrementAndGet()); Assert.assertEquals(count.get(), millis.intValue()); Assert.assertEquals(clock.count, count.get() + 1); }
_engines.get(0) .stream(init) .peek(er -> other.set(er.toEvolutionStart())) .spliterator() ); _engines.get(0) .stream(init) .peek(er -> other.set(er.toEvolutionStart())) .spliterator() ); .map(engine -> engine .stream(other::get) .peek(er -> other.set(er.toEvolutionStart()))) .map(BaseStream::spliterator) .collect(Collectors.toList())
@Test public void parallelStream() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine .stream() .limit(Limits.byFixedGeneration(1000)) .parallel() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertTrue( result.getTotalGenerations() >= 1000, "Total generation must be bigger than 1000: " + result.getTotalGenerations() ); }
@Test(dataProvider = "generations") public void engineGenerationLimit1(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)) .stream() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
private Spliterator<EvolutionResult<G, C>> generate( final EvolutionInit<G> init, final EvolutionResult<G, C> result ) { return result == null ? _engine.apply(null) .stream(init) .spliterator() : _engine.apply(result) .stream(result.toEvolutionStart()) .spliterator(); }
@Override public EvolutionStream<G, C> stream(final Supplier<EvolutionStart<G, C>> start) { return EvolutionStream.of(evolutionStart(start), this::evolve); }
@Test public void adapt2() { final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }
@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 ); }
@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()); }
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(); }
@Override public EvolutionStream<G, C> stream(final EvolutionInit<G> init) { return EvolutionStreamable.this.stream(init).limit(proceed.get()); } };
public static void main(final String[] args) throws IOException { final EvolutionResult<DoubleGene, Double> rescue = ENGINE.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); final Path path = Paths.get("result.bin"); IO.object.write(rescue, path); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> result = ENGINE .stream((EvolutionResult<DoubleGene, Double>)IO.object.read(path)) .limit(Limits.bySteadyFitness(20)) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result); }
public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine .builder( RastriginFunction::fitness, // Codec for 'x' vector. Codecs.ofVector(DoubleRange.of(-R, R), N)) .populationSize(500) .optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03), new MeanAlterer<>(0.6)) .build(); final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber(); final Phenotype<DoubleGene, Double> best = engine.stream() .limit(bySteadyFitness(7)) .peek(statistics) .collect(toBestPhenotype()); System.out.println(statistics); System.out.println(best); } }
@Test public void cycle2() { final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.of( streamable(2), streamable(2) ) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); }