/** * Create a new evolution {@code Engine.Builder} initialized with the values of the current * evolution {@code Engine}. With this method, the evolution engine can serve as a template for an * new one. * * @return a new engine builder */ public Builder<G, C> builder() { return new Builder<>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .executor(_executor.get()) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction((double) _offspringCount / (double) getPopulationSize()) .offspringSelector(_offspringSelector) .optimize(_optimize) .phenotypeValidator(_validator) .populationSize(getPopulationSize()) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries); }
// Evaluates the fitness function of the give population concurrently. private Population<G, C> evaluate(final Population<G, C> population) { try (Concurrency c = Concurrency.with(_executor.get())) { c.execute(population); } return population; }
/** * Return the {@link Executor} the engine is using for executing the evolution steps. * * @return the executor used for performing the evolution steps */ public Executor getExecutor() { return _executor.get(); }
/** * 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); }