import static java.lang.Math.PI; import static java.lang.Math.cos; import static java.lang.Math.sin; import static org.jenetics.engine.EvolutionResult.toBestPhenotype; import static org.jenetics.engine.limit.bySteadyFitness; import org.jenetics.DoubleGene; import org.jenetics.MeanAlterer; import org.jenetics.Mutator; import org.jenetics.Optimize; import org.jenetics.Phenotype; import org.jenetics.engine.Engine; import org.jenetics.engine.EvolutionStatistics; import org.jenetics.engine.codecs; import org.jenetics.util.DoubleRange; public class RealFunction { // The fitness function. private static double fitness(final double x) { return cos(0.5 + sin(x))*cos(x); } public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine // Create a new builder with the given fitness // function and chromosome. .builder( RealFunction::fitness, codecs.ofScalar(DoubleRange.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); } }