@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 // https://github.com/jenetics/jenetics/issues/420 public void bestFitnessResult() { final Genotype<IntegerGene> genotype = Genotype.of(IntegerChromosome.of(0, 10)); final AtomicInteger ai = new AtomicInteger(); final Function<Genotype<IntegerGene>, Integer> ff = x -> ai.incrementAndGet(); final int threshold = 100; final Integer result = Engine.builder(ff, genotype) .build() .stream() .limit(Limits.byFitnessThreshold(threshold)) .collect(EvolutionResult.toBestEvolutionResult()) .getBestFitness(); Assert.assertNotNull(result); Assert.assertTrue( result >= 100, format("Expected value >= %s, but got %s", threshold, result) ); }
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); }