@Override public boolean test(final EvolutionResult<?, C> result) { if (!_proceed) return false; if (_fitness == null) { _fitness = result.getBestFitness(); _stable = 1; } else { final Optimize opt = result.getOptimize(); if (opt.compare(_fitness, result.getBestFitness()) >= 0) { _proceed = ++_stable <= _generations; } else { _fitness = result.getBestFitness(); _stable = 1; } } return _proceed; } }
@Override public boolean equals(final Object obj) { return obj == this || obj instanceof EvolutionResult && Objects.equals(_optimize, ((EvolutionResult)obj)._optimize) && Objects.equals(_population, ((EvolutionResult)obj)._population) && Objects.equals(_generation, ((EvolutionResult)obj)._generation) && Objects.equals(_totalGenerations, ((EvolutionResult)obj)._totalGenerations) && Objects.equals(_durations, ((EvolutionResult)obj)._durations) && Objects.equals(_killCount, ((EvolutionResult)obj)._killCount) && Objects.equals(_invalidCount, ((EvolutionResult)obj)._invalidCount) && Objects.equals(_alterCount, ((EvolutionResult)obj)._alterCount) && Objects.equals(getBestFitness(), ((EvolutionResult)obj).getBestFitness()); }
@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; } }
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<?, 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) ); }
IO.object.write(future.get(), resultPath); System.out.println("Best fitness: " + future.get().getBestFitness());
@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; } }
@Override public boolean test(final EvolutionResult<?, N> result) { final Number fitness = result.getBestFitness(); if (fitness != null) { _buffer.accept(fitness.doubleValue()); } return !_buffer.isFull() || _proceed.test( _buffer.doubleMoments(_shortFilterSize), _buffer.doubleMoments(_longFilterSize) ); }
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() )); }
protected double[] fitness(final P param) { final Predicate<? super EvolutionResult<G, N>> terminator1 = _terminator1.apply(param); final long start1 = System.currentTimeMillis(); final EvolutionResult<G, N> result1 = _engine1.stream() .limit(terminator1) .collect(EvolutionResult.toBestEvolutionResult()); final long end1 = System.currentTimeMillis(); final Predicate<? super EvolutionResult<G, N>> terminator2 = _terminator2.apply(param); final long start2 = System.currentTimeMillis(); final EvolutionResult<G, N> result2 = _engine2.stream() .limit(terminator2) .collect(EvolutionResult.toBestEvolutionResult()); final long end2 = System.currentTimeMillis(); return new double[] { result1.getTotalGenerations(), result1.getBestFitness() != null ? result1.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end1 - start1, result2.getTotalGenerations(), result2.getBestFitness() != null ? result2.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end2 - start2 }; }
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 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 // 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) ); }
@Test public void bestWorstPhenotype() { final int length = 100; final Function<Genotype<IntegerGene>, Integer> ff = gt -> gt.getGene().getAllele(); final MSeq<Phenotype<IntegerGene, Integer>> population = MSeq.ofLength(length); for (int i = 0; i < length; ++i) { final Genotype<IntegerGene> gt = Genotype.of(IntegerChromosome.of( IntegerGene.of(i, 0, length) )); population.set(i, Phenotype.of(gt, 1, ff)); } population.shuffle(RandomRegistry.getRandom()); final EvolutionResult<IntegerGene, Integer> maxResult = EvolutionResult.of( Optimize.MAXIMUM, population.toISeq(), 0, 0, EvolutionDurations.ZERO, 0, 0, 0 ); Assert.assertEquals(maxResult.getBestFitness().intValue(), length - 1); Assert.assertEquals(maxResult.getWorstFitness().intValue(), 0); final EvolutionResult<IntegerGene, Integer> minResult = EvolutionResult.of( Optimize.MINIMUM, population.toISeq(), 0, 0, EvolutionDurations.ZERO, 0, 0, 0 ); Assert.assertEquals(minResult.getBestFitness().intValue(), 0); Assert.assertEquals(minResult.getWorstFitness().intValue(), length - 1); }
@Override public int hashCode() { return hash(_optimize, hash(_population, hash(_generation, hash(_totalGenerations, hash(_durations, hash(_killCount, hash(_invalidCount, hash(_alterCount, hash(getBestFitness(), hash(EvolutionResult.class)))))))))); }