/*
 * 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.engine;

import static java.util.Objects.requireNonNull;

import java.util.function.Function;

import org.jenetics.Gene;
import org.jenetics.Genotype;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
public final class Optimizer<ARG_TYPE> {

	private final class Inner<G extends Gene<?, G>> {
		private final Codec<G, ARG_TYPE> _codec;

		Inner(final Codec<G, ARG_TYPE> codec) {
			_codec = requireNonNull(codec);
		}

		<R extends Comparable<? super R>> ARG_TYPE
		argmin(final Function<ARG_TYPE, R> function) {
			final Engine<G, R> engine = Engine
				.builder(function.compose(_codec.decoder()), _codec.genotype())
				.build();

			final Genotype<G> bgt = engine.stream()
				.limit(100)
				.collect(EvolutionResult.toBestGenotype());

			return _codec.decoder().apply(bgt);
		}
	}

	private Inner<?> _inner;

	private Optimizer() {
	}

	public <R extends Comparable<? super R>> ARG_TYPE
	argmin(final Function<ARG_TYPE, R> function) {
		return _inner.argmin(function);
	}


	public static <G extends Gene<?, G>, S> Optimizer<S> 
	of(final Codec<G, S> codec) {
		final Optimizer<S> optimizer = new Optimizer<>();
		optimizer._inner = optimizer.new Inner<>(codec);

		return optimizer;
	}

	public static Optimizer<Double> ofDouble(
		final double min,
		final double max
	) {
		return of(Codec.ofDouble(min, max));
	}
}

@@ -905,16 +905,46 @@
 enotype;
+%0Aimport org.jenetics.Optimize;
 %0A%0A/**%0A *
@@ -1496,16 +1496,48 @@
 type())%0A
+%09%09%09%09.optimize(Optimize.MINIMUM)%0A
 %09%09%09%09.bui
@@ -1601,11 +1601,33 @@
 mit(
-100
+limit.bySteadyFitness(30)
 )%0A%09%09
@@ -1965,17 +1965,16 @@
 mizer%3CS%3E
- 
 %0A%09of(fin
@@ -2208,24 +2208,24 @@
 le max%0A%09) %7B%0A
-
 %09%09return of(
@@ -2253,11 +2253,161 @@
 ax));%0A%09%7D
+%0A%0A%09public static void main(final String%5B%5D args) %7B%0A%09%09final double result = Optimizer.ofDouble(0, 100).argmin(i -%3E i);%0A%0A%09%09System.out.println(result);%0A%09%7D
 %0A%7D%0A

Add test method.