@Override public void accept(final EvolutionResult<?, C> result) { accept(result.getDurations()); _killed.accept(result.getKillCount()); _invalids.accept(result.getInvalidCount()); _altered.accept(result.getAlterCount()); result.getPopulation() .forEach(pt -> accept(pt, result.getGeneration())); }
EvolutionResult<G, C> with(final ISeq<Phenotype<G, C>> population) { return EvolutionResult.of( getOptimize(), population, getGeneration(), getTotalGenerations(), getDurations(), getKillCount(), getInvalidCount(), getAlterCount() ); }
protected double[] fitness(final P param) { final Predicate<? super EvolutionResult<G, N>> terminator = _terminator.apply(param); final long start = System.currentTimeMillis(); final EvolutionResult<G, N> result = _engine.apply(param).stream() .limit(terminator) .collect(EvolutionResult.toBestEvolutionResult()); final long end = System.currentTimeMillis(); return new double[] { result.getTotalGenerations(), result.getBestFitness() != null ? result.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end - start }; }
@Override public boolean test(final EvolutionResult<?, C> result) { final boolean proceed = _proceed && result.getOptimize().compare(_threshold, result.getBestFitness()) >= 0; try { return _proceed; } finally { _proceed = proceed; } }
@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 // 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 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); }
void update(final EvolutionResult<?, Double> result) { final String generation = Long.toString(result.getGeneration()); final String bestFitness = _format.format(result.getBestFitness()); final double age = result.getPopulation().stream() .collect(averagingLong(p -> p.getAge(result.getGeneration()))); _generationTextField.setText(generation); _bestFitnessTextField.setText(bestFitness); _populationAgeTextField.setText(_format.format(age)); _evaluationTimeTextField.setText(format( result.getDurations().getEvaluationDuration() )); }
@Test public void toUniquePopulation() { final int populationSize = 100; final Engine<IntegerGene, Integer> engine = Engine .builder(a -> a.getGene().getAllele(), IntegerChromosome.of(0, 10)) .populationSize(populationSize) .mapping(EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, Integer.MAX_VALUE)))) .build(); final EvolutionResult<IntegerGene, Integer> result = engine.stream() .limit(10) .peek(r -> { if (r.getGenotypes().stream().collect(Collectors.toSet()).size() != populationSize) { throw new AssertionError(format( "Expected unique population size %d, but got %d.", populationSize, r.getGenotypes().stream().collect(Collectors.toSet()).size() )); } }) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(result.getPopulation().size(), populationSize); }
@Override public boolean test(final EvolutionResult<?, N> result) { final DoubleMomentStatistics fitness = new DoubleMomentStatistics(); result.getPopulation() .forEach(p -> fitness.accept(p.getFitness().doubleValue())); return result.getTotalGenerations() <= 1 || _proceed.test( result.getBestFitness() != null ? result.getBestFitness().doubleValue() : Double.NaN, DoubleMoments.of(fitness) ); }
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 toUniquePopulation3() { final ISeq<Genotype<IntegerGene>> genotypes = Genotype.of(IntegerChromosome.of(0, 10)).instances() .limit(100) .collect(ISeq.toISeq()); final UnaryOperator<EvolutionResult<IntegerGene, Integer>> unifier = EvolutionResult.toUniquePopulation( Genotype.of(IntegerChromosome.of(0, 10))); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); final EvolutionResult<IntegerGene, Integer> unified = unifier.apply(result); Assert.assertNotEquals(unified, result); Assert.assertTrue( unified.getGenotypes().stream().collect(Collectors.toSet()).size() < unified.getPopulation().size() ); Assert.assertEquals( result.getPopulation().size(), unified.getPopulation().size() ); }
@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); }
final Seq<Phenotype<G, C>> population = result.getPopulation(); final Seq<Genotype<G>> genotypes = result.getGenotypes(); final Set<Genotype<G>> elements = new HashSet<>(genotypes.asList()); uniques = result.with( Stream.concat(elements.stream(), genotypes.stream()) .limit(population.size()) .map(gt -> population.get(0).newInstance( factory.newInstance(), result.getGeneration())) .collect(ISeq.toISeq()) );
@Test public void constantPopulationForZeroOffspring() { final int populationSize = 20; final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .populationSize(populationSize) .offspringSize(0) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .peek(r -> { if (r.getPopulation().size() != populationSize) { throw new AssertionError(format( "Expected population size %d, but got %d.", populationSize, r.getPopulation().size() )); } }) .collect(EvolutionResult.toBestEvolutionResult()); }
@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}); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // The base engine tries to approximate to good solution in current // environment. final Engine<BitGene, Double> baseEngine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // The 'diversity' engine tries to broaden the search space again. final Engine<BitGene, Double> diversityEngine = baseEngine.builder() .alterers(new Mutator<>(0.5)) .build(); // Concatenates the two engines into one cyclic engine. final EvolutionStreamable<BitGene, Double> engine = CyclicEngine.of( // This engine stops the evolution after 10 non-improving // generations and hands over to the diversity engine. baseEngine.limit(() -> Limits.bySteadyFitness(10)), // The higher mutation rate of this engine broadens the search // space for 15 generations and hands over to the base engine. diversityEngine.limit(15) ); final EvolutionResult<BitGene, Double> best = engine.stream() // The evolution is stopped after 50 non-improving generations. .limit(bySteadyFitness(50)) .collect(toBestEvolutionResult()); System.out.println(best.getTotalGenerations()); System.out.println(best.getBestPhenotype()); }
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); }
@Override public boolean test(final EvolutionResult<?, N> result) { final DoubleMoments moments = result.getPopulation().stream() .collect(toDoubleMoments(pt -> pt.getFitness().doubleValue())); boolean proceed = true; if (_fitness == null) { _fitness = result.getBestFitness(); _stableGenerations = 1; } else { final Optimize opt = result.getOptimize(); if (opt.compare(_fitness, result.getBestFitness()) >= 0) { proceed = ++_stableGenerations <= _generations; } else { _fitness = result.getBestFitness(); _stableGenerations = 1; } } return proceed; } }
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()); }