All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jenetics.engine.Problem Maven / Gradle / Ivy

/*
 * Java Genetic Algorithm Library (jenetics-3.4.0).
 * Copyright (c) 2007-2016 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 ([email protected])
 */
package org.jenetics.engine;

import static java.util.Objects.requireNonNull;

import java.util.function.Function;

import org.jenetics.Gene;

/**
 * This interface describes a problem which can be solved by the GA
 * evolution {@code Engine}. It connects the actual {@link #fitness()} function
 * and the needed {@link #codec()}.
 *
 * @see Codec
 * @see Engine
 *
 * @param  the (native) argument type of the problem fitness function
 * @param  the gene type the evolution engine is working with
 * @param  the result type of the fitness function
 *
 * @author Franz Wilhelmstötter
 * @version 3.4
 * @since 3.4
 */
public interface Problem<
	T,
	G extends Gene,
	C extends Comparable
>
{

	/**
	 * Return the fitness function of the problem in the native
	 * problem domain.
	 *
	 * @return the fitness function
	 */
	public Function fitness();

	/**
	 * Return the codec, which translates the types of the problem domain into
	 * types, which can be understand by the evolution {@code Engine}.
	 *
	 * @return the engine codec
	 */
	public Codec codec();

	/**
	 * Return a new optimization problem with the given parameters.
	 *
	 * @param fitness the problem fitness function
	 * @param codec the evolution engine codec
	 * @param  the (native) argument type of the problem fitness function
	 * @param  the gene type the evolution engine is working with
	 * @param  the result type of the fitness function
	 * @return a new problem object from the given parameters
	 * @throws NullPointerException if one of the arguments is {@code null}
	 */
	public static , C extends Comparable>
	Problem of(
		final Function fitness,
		final Codec codec
	) {
		requireNonNull(fitness);
		requireNonNull(codec);

		return new Problem() {
			@Override
			public Codec codec() {
				return codec;
			}

			@Override
			public Function fitness() {
				return fitness;
			}
		};
	}

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy