private EvolutionStart<G, C> evolutionStart() { final int generation = 1; final int size = _offspringCount + _survivorsCount; final Population<G, C> population = new Population<G, C>(size).fill(() -> newPhenotype(generation), size); return EvolutionStart.of(population, generation); }
private EvolutionStart<G, C> evolutionStart( final Iterable<Genotype<G>> genotypes, final long generation) { final Stream<Phenotype<G, C>> stream = Stream.concat( StreamSupport.stream(genotypes.spliterator(), false) .map(gt -> Phenotype.of(gt, generation, _fitnessFunction, _fitnessScaler)), Stream.generate(() -> newPhenotype(generation))); final Population<G, C> population = stream.limit(getPopulationSize()).collect(toPopulation()); return EvolutionStart.of(population, generation); }
private EvolutionStart<G, C> evolutionStart( final Population<G, C> population, final long generation) { final Stream<Phenotype<G, C>> stream = Stream.concat( population .stream() .map(p -> p.newInstance(p.getGeneration(), _fitnessFunction, _fitnessScaler)), Stream.generate(() -> newPhenotype(generation))); final Population<G, C> pop = stream.limit(getPopulationSize()).collect(toPopulation()); return EvolutionStart.of(pop, generation); }
/** * 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); }
/** * Perform one evolution step with the given {@code population} and {@code generation}. 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> * * @see #evolve(EvolutionStart) * @param population the population to evolve * @param generation the current generation; used for calculating the phenotype age. * @return the evolution result * @throws java.lang.NullPointerException if the given {@code population} is {@code null} * @throws IllegalArgumentException if the given {@code generation} is smaller then one */ public EvolutionResult<G, C> evolve(final Population<G, C> population, final long generation) { return evolve(EvolutionStart.of(population, generation)); }