Ejemplo n.º 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);
  }
Ejemplo n.º 2
0
  // Create a new and valid phenotype
  private Phenotype<G, C> newPhenotype(final long generation) {
    int count = 0;
    Phenotype<G, C> phenotype;
    do {
      phenotype =
          Phenotype.of(
              _genotypeFactory.newInstance(), generation, _fitnessFunction, _fitnessScaler);
    } while (++count < _individualCreationRetries && !_validator.test(phenotype));

    return phenotype;
  }
Ejemplo n.º 3
0
  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);
  }
Ejemplo n.º 4
0
 @Override
 void accept(final Phenotype<?, N> pt, final long generation) {
   super.accept(pt, generation);
   _fitness.accept(pt.getFitness().doubleValue());
 }
Ejemplo n.º 5
0
 void accept(final Phenotype<?, C> pt, final long generation) {
   _age.accept(pt.getAge(generation));
 }
 private static Phenotype<DoubleGene, Double> phenotype(final double value) {
   return Phenotype.of(
       Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0.0, 1000.0))),
       1,
       a -> a.getGene().getAllele());
 }