Пример #1
0
  public static void main(String[] args) {
    final Engine<DoubleGene, Double> engine =
        Engine
            // Create a new builder with the given fitness
            // function and chromosome.
            .builder(RealFunction::eval, DoubleChromosome.of(0.0, 2.0 * PI))
            .populationSize(500)
            .optimize(Optimize.MINIMUM)
            .alterers(new Mutator<>(0.03), new MeanAlterer<>(0.6))
            // Build an evolution engine with the
            // defined parameters.
            .build();

    // Create evolution statistics consumer.
    final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber();

    final Phenotype<DoubleGene, Double> best =
        engine
            .stream()
            // Truncate the evolution stream after 7 "steady"
            // generations.
            .limit(bySteadyFitness(7))
            // The evolution will stop after maximal 100
            // generations.
            .limit(100)
            // Update the evaluation statistics after
            // each generation
            .peek(statistics)
            // Collect (reduce) the evolution stream to
            // its best phenotype.
            .collect(toBestPhenotype());

    System.out.println(statistics);
    System.out.println(best);
  }
 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());
 }