Problem.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.1.0).
003  * Copyright (c) 2007-2018 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 import io.jenetics.Genotype;
028 
029 /**
030  * This interface describes a <i>problem</i> which can be solved by the GA
031  * evolution {@code Engine}. It connects the actual {@link #fitness()} function
032  * and the needed {@link #codec()}.
033  *
034  <pre>{@code
035  * final Problem<ISeq<BitGene>, BitGene, Integer> counting = Problem.of(
036  *     // Native fitness function
037  *     genes -> (int)genes.stream()
038  *         .filter(BitGene::getBit)
039  *         .count(),
040  *     // Problem encoding
041  *     Codec.of(
042  *         Genotype.of(BitChromosome.of(100)),
043  *         gt -> gt.getChromosome().toSeq()
044  *     )
045  * );
046  * }</pre>
047  *
048  * The example above shows the Ones-Counting problem definition.
049  *
050  @see Codec
051  @see Engine
052  *
053  @param <T> the (<i>native</i>) argument type of the problem fitness function
054  @param <G> the gene type the evolution engine is working with
055  @param <C> the result type of the fitness function
056  *
057  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
058  @version 4.1
059  @since 3.4
060  */
061 public interface Problem<
062     T,
063     extends Gene<?, G>,
064     extends Comparable<? super C>
065 {
066 
067     /**
068      * Return the fitness function of the <i>problem</i> in the <i>native</i>
069      * problem domain.
070      *
071      @return the fitness function
072      */
073     public Function<T, C> fitness();
074 
075     /**
076      * Return the codec, which translates the types of the problem domain into
077      * types, which can be understand by the evolution {@code Engine}.
078      *
079      @return the engine codec
080      */
081     public Codec<T, G> codec();
082 
083     /**
084      * Returns the fitness value for the given argument.
085      *
086      @since 4.1
087      *
088      @param arg the argument of the fitness function
089      @return the fitness value
090      */
091     public default C fitness(final T arg) {
092         return fitness().apply(arg);
093     }
094 
095     /**
096      * Returns the fitness value for the given argument.
097      *
098      @since 4.1
099      *
100      @param gt the argument of the fitness function
101      @return the fitness value
102      */
103     public default C fitness(final Genotype<G> gt) {
104         return fitness(codec().decode(gt));
105     }
106 
107     /**
108      * Return a new optimization <i>problem</i> with the given parameters.
109      *
110      @param fitness the problem fitness function
111      @param codec the evolution engine codec
112      @param <T> the (<i>native</i>) argument type of the problem fitness function
113      @param <G> the gene type the evolution engine is working with
114      @param <C> the result type of the fitness function
115      @return a new problem object from the given parameters
116      @throws NullPointerException if one of the arguments is {@code null}
117      */
118     public static <T, G extends Gene<?, G>, C extends Comparable<? super C>>
119     Problem<T, G, C> of(
120         final Function<T, C> fitness,
121         final Codec<T, G> codec
122     ) {
123         requireNonNull(fitness);
124         requireNonNull(codec);
125 
126         return new Problem<T, G, C>() {
127             @Override
128             public Codec<T, G> codec() {
129                 return codec;
130             }
131 
132             @Override
133             public Function<T, C> fitness() {
134                 return fitness;
135             }
136         };
137     }
138 
139 }