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) 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(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()); }
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()); }
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 FutureEvaluator<>(FutureEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
@Test public void bestCollector() { final int bestMaxValue = IntStream.range(0, 100) .mapToObj(value -> newResult(Optimize.MAXIMUM, value)) .collect(toBestEvolutionResult()) .getBestFitness(); Assert.assertEquals(bestMaxValue, 99); final int bestMinValue = IntStream.range(0, 100) .mapToObj(value -> newResult(Optimize.MINIMUM, value)) .collect(EvolutionResult.toBestGenotype()) .getGene().getAllele(); Assert.assertEquals(bestMinValue, 0); }
@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 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(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); }
@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(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(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()); }
@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()); }
@Test public void phenotypeValidator() { final int populationSize = 100; final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .phenotypeValidator(pt -> false) .populationSize(populationSize) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(result.getInvalidCount(), populationSize); }
@Test public void genotypeValidator() { final int populationSize = 100; final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .genotypeValidator(pt -> false) .populationSize(populationSize) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(result.getInvalidCount(), populationSize); }
@Test public void survivorsFractionZero() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .survivorsFraction(0) .build(); engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }
@Test public void offspringFractionOne() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .offspringFraction(1) .build(); engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }
@Test public void offspringFractionZero() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .offspringFraction(0) .build(); engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }
@Test public void survivorsFractionOne() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .survivorsFraction(1) .build(); engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }