/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.example;

import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.sin;

import java.util.function.Function;

import org.jenetics.DoubleChromosome;
import org.jenetics.DoubleGene;
import org.jenetics.GeneticAlgorithm;
import org.jenetics.Genotype;
import org.jenetics.MeanAlterer;
import org.jenetics.Mutator;
import org.jenetics.NumberStatistics;
import org.jenetics.Optimize;
import org.jenetics.util.Factory;

final class Real
	implements Function<Genotype<DoubleGene>, Double>
{
	@Override
	public Double apply(Genotype<DoubleGene> genotype) {
		final double x = genotype.getGene().doubleValue();
		return cos(0.5 + sin(x)) * cos(x);
	}
}

public class RealFunction {
	public static void main(String[] args) {
		Factory<Genotype<DoubleGene>> gtf = Genotype.of(
			new DoubleChromosome(0.0, 2.0 * PI)
		);
		Function<Genotype<DoubleGene>, Double> ff = new Real();
		GeneticAlgorithm<DoubleGene, Double> ga =
			new GeneticAlgorithm<>(
				gtf, ff, Optimize.MINIMUM
			);

		ga.setStatisticsCalculator(
			new NumberStatistics.Calculator<DoubleGene, Double>()
		);
		ga.setPopulationSize(500);
		ga.setAlterers(
			new Mutator<DoubleGene, Double>(0.03),
			new MeanAlterer<DoubleGene, Double>(0.6)
		);

		ga.setup();
		ga.evolve(100);
		System.out.println(ga.getBestStatistics());
		System.out.println(ga.getBestPhenotype());
	}
}

@@ -900,24 +900,98 @@
 .Function;%0A%0A
+import org.jenetics.internal.engine.Engine;%0A%0Aimport org.jenetics.Alterer;%0A
 import org.j
@@ -2197,13 +2197,687 @@
 ype());%0A
-%09%7D
+%0A%09%09RealFunction2.main();%0A%09%7D%0A%7D%0A%0Aclass RealFunction2 %7B%0A%0A%09public static void main() %7B%0A%09%09final Function%3CGenotype%3CDoubleGene%3E, Double%3E ff = gt -%3E %7B%0A%09%09%09final double x = gt.getGene().doubleValue();%0A%09%09%09return cos(0.5 + sin(x)) * cos(x);%0A%09%09%7D;%0A%0A%09%09final Engine%3CDoubleGene, Double%3E engine = Engine%0A%09%09%09.newBuilder(Genotype.of(DoubleChromosome.of(0.0, 2.0 * PI)), ff)%0A%09%09%09.populationSize(500)%0A%09%09%09.alterer(Alterer.of(%0A%09%09%09%09new Mutator%3CDoubleGene, Double%3E(0.03),%0A%09%09%09%09new MeanAlterer%3C%3E(0.6)))%0A%09%09%09.optimize(Optimize.MINIMUM)%0A%09%09%09.build();%0A%0A%09%09final Object best = engine.stream().limit(100)%0A%09%09%09.collect(engine.BestEvolutionResult)%0A%09%09%09.getBestPhenotype();%0A%0A%09%09System.out.println(%22Best: %22 + best);%0A%0A%09%7D%0A
 %0A%7D%0A

Update 'RealFunction' example.