/** * 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()); }
public static void main(final String[] args) throws InterruptedException { final Runner<String, BitGene, Double> runner = Runner.of( param -> KNAPSACK(toPopulationSize(param)), param -> Limits.byFixedGeneration(toGeneration(param)), TRIAL_METER, Writer.text(), Reader.text(), args ); runner.start(); runner.join(); }
@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); }
@Test public void adapt3() { final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); }
@Test public void concat1() { final EvolutionStreamable<DoubleGene, Double> engine = ConcatEngine.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(40)) .limit(100) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.intValue(), 30); Assert.assertEquals(result.getTotalGenerations(), 30); }
@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 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 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}); }
@Test(dataProvider = "generations") public void generationLimit(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.stream() .limit(Limits.byFixedGeneration(generations)) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test public void cycle3() { final EvolutionStream<IntegerGene, Integer> stream = CyclicEngine.of( streamable(2), streamable(2) ) .stream(); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); }
@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()); }
public static void main(final String[] args) throws InterruptedException { final Runner<Tuple2<Long, Integer>, BitGene, Double> runner = Runner.of( param -> KNAPSACK(param._2), param -> Limits.byFixedGeneration(param._1), TRIAL_METER, Writer.text().map(Object::toString), Reader.text().map(p -> Tuple2.of(toGeneration(p), toPopulationSize(p))), args ); runner.start(); runner.join(); }
@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}); }
@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() ); }
public static void main(final String[] args) { final RectFill problem = new RectFill(new Rect(0, 100, 0, 100)); final Engine<AnyGene<Rect>, Double> engine = Engine.builder(problem) .individualCreationRetries(10) .genotypeValidator(gt -> true) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new SinglePointCrossover<>()) .build(); final ISeq<Rect> best = problem.codec().decode( engine.stream() .limit(byFixedGeneration(10)) .collect(EvolutionResult.toBestGenotype()) ); System.out.println(best); }
@Test public void adaptInit() { final Chromosome<IntegerGene> ch = IntegerChromosome.of(IntegerGene.of(5, 0, 1000)); final Genotype<IntegerGene> gt = Genotype.of(ch); final EvolutionInit<IntegerGene> init = EvolutionInit.of( ISeq.<Genotype<IntegerGene>>of(gt), 1L ); final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(init); final int[] array = stream .limit(Limits.byFixedGeneration(12)) .limit(10) .mapToInt(r -> r.getGenotypes().get(0).getGene().intValue()) .toArray(); Assert.assertEquals(array, new int[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); }
public static void main(final String[] args) { final Problem<int[][], IntegerGene, Integer> problem = Problem.of( Matrix::fitness, Codec.of( Genotype.of(IntegerChromosome.of(IntRange.of(0, 10), 3), 3), gt -> gt.stream() .map(ch -> ch.stream() .mapToInt(IntegerGene::intValue).toArray()) .toArray(int[][]::new) ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem).build(); final Genotype<IntegerGene> gt = engine.stream() .limit(Limits.byFixedGeneration(20)) .collect(EvolutionResult.toBestGenotype()); final int[][] best = problem.codec().decode(gt); print(best); }
@Test public void concat5() { final EvolutionStream<IntegerGene, Integer> stream = ConcatEngine.of( streamable(3), streamable(4), streamable(5), streamable(15), streamable(15) ) .stream() .limit(Limits.byFixedGeneration(15)); final int[] array = stream .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, 13, 14, 15}); }