@Override public EvolutionStream<G, C> stream(final Supplier<EvolutionStart<G, C>> start) { return EvolutionStreamable.this.stream(start).limit(proceed.get()); }
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 Engine engine = Engine .builder(MixedGenotype::fitness, ENCODING) .build(); final Phenotype best = (Phenotype)engine.stream() .limit(10) .collect(EvolutionResult.toBestPhenotype()); System.out.println(best); }
public static void main(final String[] args) { final Engine<AnyGene<Dimension>, Double> engine = Engine .builder(Squares::area, Codecs.ofScalar(Squares::nextDimension)) .build(); final Phenotype<AnyGene<Dimension>, Double> pt = engine.stream() .limit(50) .collect(EvolutionResult.toBestPhenotype()); System.out.println(pt); }
public static void main(final String[] args) { final ISeq<Vec<double[]>> front = ENGINE.stream() .limit(2500) .collect(MOEA.toParetoSet(IntRange.of(1000, 1100))) .map(Phenotype::getFitness); }
public static void main(final String[] args) { final OnesCounting problem = new OnesCounting(15, 0.13); final Engine<BitGene, Integer> engine = Engine.builder(problem).build(); final ISeq<BitGene> result = problem.codec().decoder().apply( engine.stream() .limit(10) .collect(EvolutionResult.toBestGenotype()) ); System.out.println(result); }
public static void main(final String[] args) { final Engine<AnyGene<LocalDate>, Integer> engine = Engine .builder(LastMonday::fitness, CODEC) .offspringSelector(new RouletteWheelSelector<>()) .build(); final Phenotype<AnyGene<LocalDate>, Integer> best = engine.stream() .limit(50) .collect(EvolutionResult.toBestPhenotype()); System.out.println(best); }
public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine .builder(DynamicGenotype::fitness, ENCODING) .alterers(new DynamicMutator<>(0.25)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(20) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestFitness()); }
@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); }
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)); } }
public static void main(final String[] args) { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(/*Dummy fitness function*/gt -> Double.NaN, gtf) .evaluator(new RxEvaluator<>(RxEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
@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 adapt1() { final EvolutionStream<IntegerGene, Integer> stream = new AdaptiveEngine<IntegerGene, Integer>(r -> streamable(2)) .stream(); final int[] array = stream.limit(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 generationCount(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(generations) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test public void emptyStreamCollectEvolutionResult() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNull(result); }
public static void main(final String[] args) { final ISeq<Integer> orders = buildOrders(); final Codec<ISeq<Integer>, EnumGene<Integer>> codec = buildCodec(orders); final Engine<EnumGene<Integer>, Integer> engine = buildEngine(codec); final Genotype<EnumGene<Integer>> collect = engine.stream() .limit(1000) .collect(EvolutionResult.toBestGenotype()); final ISeq<Integer> best = codec.decode(collect); System.out.println(best); System.out.println(fitness(best)); }
@Test public void emptyStreamCollectGenotype() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final Genotype<DoubleGene> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestGenotype()); Assert.assertNull(result); }
@Test public void emptyStreamCollectPhenotype() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final Phenotype<DoubleGene, Double> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestPhenotype()); Assert.assertNull(result); }
@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(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()); }