@Override public boolean test(final EvolutionResult<G, ?> result) { final ISeq<DoubleMoments> stat = statistics(result.getPopulation()); return result.getTotalGenerations() <= 1 || stat.stream() .filter(_convergence) .count() <= _convergenceRate*stat.size(); }
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() )); }
/** * Create a new <b>infinite</b> evolution iterator starting with a * previously evolved {@link EvolutionResult}. The iterator is initialized * with the population of the given {@code result} and its total generation * {@link EvolutionResult#getTotalGenerations()}. * * @deprecated Marked for removal in the next major version. * @param result the previously evolved {@code EvolutionResult} * @return a new evolution stream, which continues a previous one * @throws NullPointerException if the given evolution {@code result} is * {@code null} */ @Deprecated public default Iterator<EvolutionResult<G, C>> iterator(final EvolutionResult<G, C> result) { return iterator(EvolutionStart.of( result.getPopulation(), result.getGeneration() )); }
@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) ); }
return result -> { final Factory<Genotype<G>> factory = result .getPopulation().get(0) .getGenotype();
return result -> { final Factory<Genotype<G>> factory = result .getPopulation().get(0) .getGenotype();
private static double var(final EvolutionResult<DoubleGene, Double> result) { return result != null ? result.getPopulation().stream() .map(Phenotype::getFitness) .collect(DoubleMoments.toDoubleMoments()) .getVariance() : 0.0; }
@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; } }
void add(final EvolutionResult<G, C> result) { if (_front == null) { _optimize = result.getOptimize(); _front = new ParetoFront<>(this::dominance); } final ISeq<Phenotype<G, C>> front = front( result.getPopulation(), this::dominance ); _front.addAll(front.asList()); trim(); }
stream(final EvolutionResult<G, C> result) { return stream(EvolutionStart.of( result.getPopulation(), result.getGeneration() ));
final Seq<Phenotype<G, C>> population = result.getPopulation(); final Seq<Genotype<G>> genotypes = result.getGenotypes(); final Set<Genotype<G>> elements = new HashSet<>(genotypes.asList());
@Test public void toUniquePopulation2() { 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, Integer.MAX_VALUE))); final EvolutionResult<IntegerGene, Integer> result = result(genotypes); final EvolutionResult<IntegerGene, Integer> unified = unifier.apply(result); Assert.assertNotEquals(unified, result); Assert.assertEquals( unified.getGenotypes().stream().collect(Collectors.toSet()).size(), unified.getPopulation().size() ); Assert.assertEquals( result.getPopulation().size(), unified.getPopulation().size() ); }
@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() ); }
engine.stream() .limit(Limits.bySteadyFitness(50)) .peek(er -> population.set(er.getPopulation())) .collect(EvolutionResult.toBestGenotype()) );
@Test public void constantPopulationForZeroSurvivors() { final int populationSize = 20; final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .populationSize(populationSize) .survivorsSize(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 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 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 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())); }
@Test public void streamWithInitialGenotypes() { final Problem<Integer, IntegerGene, Integer> problem = Problem.of( a -> a, Codec.of( Genotype.of(IntegerChromosome.of(0, 1000)), g -> g.getGene().getAllele() ) ); final int genotypeCount = 10; final int max = 1000; final ISeq<Genotype<IntegerGene>> genotypes = IntRange.of(1, genotypeCount) .stream() .mapToObj(i -> IntegerChromosome.of(IntegerGene.of(max, 0, max))) .map(Genotype::of) .collect(ISeq.toISeq()); final Engine<IntegerGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<IntegerGene, Integer> result = engine.stream(genotypes) .limit(1) .collect(EvolutionResult.toBestEvolutionResult()); final long maxCount = result.getPopulation().stream() .filter(pt -> pt.getFitness() == max) .count(); Assert.assertTrue(maxCount >= genotypeCount); }
.peek(er -> pop.addAll(er.getPopulation() .map(Phenotype::getFitness).asList())) .collect(MOEA.toParetoSet(IntRange.of(20, 25)))