001 /*
002 * Java Genetic Algorithm Library (jenetics-4.0.0).
003 * Copyright (c) 2007-2017 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.engine;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.util.function.Function;
025
026 import io.jenetics.Gene;
027
028 /**
029 * This interface describes a <i>problem</i> which can be solved by the GA
030 * evolution {@code Engine}. It connects the actual {@link #fitness()} function
031 * and the needed {@link #codec()}.
032 *
033 * <pre>{@code
034 * final Problem<ISeq<BitGene>, BitGene, Integer> counting = Problem.of(
035 * // Native fitness function
036 * genes -> (int)genes.stream()
037 * .filter(BitGene::getBit)
038 * .count(),
039 * // Problem encoding
040 * Codec.of(
041 * Genotype.of(BitChromosome.of(100)),
042 * gt -> gt.getChromosome().toSeq()
043 * )
044 * );
045 * }</pre>
046 *
047 * The example above shows the Ones-Counting problem definition.
048 *
049 * @see Codec
050 * @see Engine
051 *
052 * @param <T> the (<i>native</i>) argument type of the problem fitness function
053 * @param <G> the gene type the evolution engine is working with
054 * @param <C> the result type of the fitness function
055 *
056 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
057 * @version 3.4
058 * @since 3.4
059 */
060 public interface Problem<
061 T,
062 G extends Gene<?, G>,
063 C extends Comparable<? super C>
064 > {
065
066 /**
067 * Return the fitness function of the <i>problem</i> in the <i>native</i>
068 * problem domain.
069 *
070 * @return the fitness function
071 */
072 public Function<T, C> fitness();
073
074 /**
075 * Return the codec, which translates the types of the problem domain into
076 * types, which can be understand by the evolution {@code Engine}.
077 *
078 * @return the engine codec
079 */
080 public Codec<T, G> codec();
081
082 /**
083 * Return a new optimization <i>problem</i> with the given parameters.
084 *
085 * @param fitness the problem fitness function
086 * @param codec the evolution engine codec
087 * @param <T> the (<i>native</i>) argument type of the problem fitness function
088 * @param <G> the gene type the evolution engine is working with
089 * @param <C> the result type of the fitness function
090 * @return a new problem object from the given parameters
091 * @throws NullPointerException if one of the arguments is {@code null}
092 */
093 public static <T, G extends Gene<?, G>, C extends Comparable<? super C>>
094 Problem<T, G, C> of(
095 final Function<T, C> fitness,
096 final Codec<T, G> codec
097 ) {
098 requireNonNull(fitness);
099 requireNonNull(codec);
100
101 return new Problem<T, G, C>() {
102 @Override
103 public Codec<T, G> codec() {
104 return codec;
105 }
106
107 @Override
108 public Function<T, C> fitness() {
109 return fitness;
110 }
111 };
112 }
113
114 }
|