@Override public void accept(final EvolutionResult<?, C> result) { if (_fitness.getMax() == null) { _fitness = MinMax.of(result.getOptimize().ascending()); } super.accept(result); }
@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())); }
/** * Perform one evolution step with the given evolution {@code start} object New phenotypes are * created with the fitness function and fitness scaler defined by this <em>engine</em> * * <p><em>This method is thread-safe.</em> * * @since 3.1 * @see #evolve(org.jenetics.Population, long) * @param start the evolution start object * @return the evolution result * @throws java.lang.NullPointerException if the given evolution {@code start} is {@code null} */ public EvolutionResult<G, C> evolve(final EvolutionStart<G, C> start) { final Timer timer = Timer.of().start(); final Population<G, C> startPopulation = start.getPopulation(); // Initial evaluation of the population. final Timer evaluateTimer = Timer.of(_clock).start(); evaluate(startPopulation); evaluateTimer.stop(); // Select the offspring population. final CompletableFuture<TimedResult<Population<G, C>>> offspring = _executor.async(() -> selectOffspring(startPopulation), _clock); // Select the survivor population. final CompletableFuture<TimedResult<Population<G, C>>> survivors = _executor.async(() -> selectSurvivors(startPopulation), _clock); // Altering the offspring population. final CompletableFuture<TimedResult<AlterResult<G, C>>> alteredOffspring = _executor.thenApply(offspring, p -> alter(p.result, start.getGeneration()), _clock); // Filter and replace invalid and to old survivor individuals. final CompletableFuture<TimedResult<FilterResult<G, C>>> filteredSurvivors = _executor.thenApply(survivors, pop -> filter(pop.result, start.getGeneration()), _clock); // Filter and replace invalid and to old offspring individuals. final CompletableFuture<TimedResult<FilterResult<G, C>>> filteredOffspring = _executor.thenApply( alteredOffspring, pop -> filter(pop.result.population, start.getGeneration()), _clock); // Combining survivors and offspring to the new population. final CompletableFuture<Population<G, C>> population = filteredSurvivors.thenCombineAsync( filteredOffspring, (s, o) -> { final Population<G, C> pop = s.result.population; pop.addAll(o.result.population); return pop; }, _executor.get()); // Evaluate the fitness-function and wait for result. final Population<G, C> pop = population.join(); final TimedResult<Population<G, C>> result = TimedResult.of(() -> evaluate(pop), _clock).get(); final EvolutionDurations durations = EvolutionDurations.of( offspring.join().duration, survivors.join().duration, alteredOffspring.join().duration, filteredOffspring.join().duration, filteredSurvivors.join().duration, result.duration.plus(evaluateTimer.getTime()), timer.stop().getTime()); final int killCount = filteredOffspring.join().result.killCount + filteredSurvivors.join().result.killCount; final int invalidCount = filteredOffspring.join().result.invalidCount + filteredSurvivors.join().result.invalidCount; return EvolutionResult.of( _optimize, result.result, start.getGeneration(), durations, killCount, invalidCount, alteredOffspring.join().result.alterCount); }
private static EvolutionResult<DoubleGene, Double> result( final int min, final int max, final Optimize opt) { return EvolutionResult.of(opt, population(min, max), 1L, EvolutionDurations.ZERO, 1, 1, 1); }