_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())
@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(); } }; }
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(); }
/** * 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; }
@Test public void limit0() { final Engine<DoubleGene, Double> engine = Engine .builder( gt -> gt.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final AtomicLong count = new AtomicLong(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(new CountLimit(0)) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.get(), 0L); Assert.assertNull(result); }
@Test // https://github.com/jenetics/jenetics/issues/318 public void initialFitnessConvergence() { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine.builder(problem).build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFitnessThreshold(0.3)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }
@Test public void limit1() { final Engine<DoubleGene, Double> engine = Engine .builder( gt -> gt.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final AtomicLong count = new AtomicLong(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(new CountLimit(1)) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.get(), 1L); Assert.assertEquals(result.getTotalGenerations(), 1L); }
@Test // https://github.com/jenetics/jenetics/issues/318 public void initialPopulationConvergence() { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine.builder(problem).build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byPopulationConvergence(0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }
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 // https://github.com/jenetics/jenetics/issues/318 public void initialFitnessConvergence() { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine.builder(problem).build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFitnessConvergence(1, 2, 0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }
engine.stream() .limit(Limits.bySteadyFitness(50)) .peek(er -> population.set(er.getPopulation())) .collect(EvolutionResult.toBestGenotype()) );
@Test // https://github.com/jenetics/jenetics/issues/318 public void initialGeneConvergence() { RandomRegistry.using(Random32.of(() -> 234), random -> { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine .builder(problem) .executor(Runnable::run) .build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byGeneConvergence(0.03, 0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }); }
@Test public void limit() { final Engine<DoubleGene, Double> engine = Engine .builder( gt -> gt.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final AtomicLong count = new AtomicLong(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(new CountLimit(1000)) .limit(new CountLimit(100)) .limit(new CountLimit(10)) .limit(new CountLimit(100)) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.get(), 10L); Assert.assertEquals(result.getTotalGenerations(), 10L); }
public static void main(String[] args) throws Exception { final CharSeq chars = CharSeq.of("A-Z "); final Factory<Genotype<CharacterGene>> gtf = Genotype.of( CharacterChromosome.of(chars, TARGET.length()) ); final Engine<CharacterGene, Integer> engine = Engine .builder(WeaselProgram::score, gtf) .populationSize(150) .selector(new WeaselSelector<>()) .offspringFraction(1) .alterers(new WeaselMutator<>(0.05)) .build(); final Phenotype<CharacterGene, Integer> result = engine.stream() .limit(byFitnessThreshold(TARGET.length() - 1)) .peek(r -> System.out.println( r.getTotalGenerations() + ": " + r.getBestPhenotype())) .collect(toBestPhenotype()); System.out.println(result); }