Beispiel #1
0
  // Filters out invalid and to old individuals. Filtering is done in place.
  private FilterResult<G, C> filter(final Population<G, C> population, final long generation) {
    int killCount = 0;
    int invalidCount = 0;

    for (int i = 0, n = population.size(); i < n; ++i) {
      final Phenotype<G, C> individual = population.get(i);

      if (!_validator.test(individual)) {
        population.set(i, newPhenotype(generation));
        ++invalidCount;
      } else if (individual.getAge(generation) > _maximalPhenotypeAge) {
        population.set(i, newPhenotype(generation));
        ++killCount;
      }
    }

    return new FilterResult<>(population, killCount, invalidCount);
  }
Beispiel #2
0
  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);
  }
Beispiel #3
0
 // Selects the offspring population. A new population object is returned.
 private Population<G, C> selectOffspring(final Population<G, C> population) {
   return _offspringCount > 0
       ? _offspringSelector.select(population, _offspringCount, _optimize)
       : Population.empty();
 }
Beispiel #4
0
 // Selects the survivors population. A new population object is returned.
 private Population<G, C> selectSurvivors(final Population<G, C> population) {
   return _survivorsCount > 0
       ? _survivorsSelector.select(population, _survivorsCount, _optimize)
       : Population.empty();
 }
Beispiel #5
0
  /**
   * 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 Population<DoubleGene, Double> population(final int min, final int max) {
   return IntStream.rangeClosed(min, max)
       .mapToDouble(i -> (double) i)
       .mapToObj(FitnessThresholdLimitTest::phenotype)
       .collect(Population.toPopulation());
 }