Problem.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.4.0).
003  * Copyright (c) 2007-2019 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.2
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      * Converts the given {@link Genotype} to the target type {@link T}. This is
085      * a shortcut for
086      <pre>{@code
087      * final Problem<SomeObject, DoubleGene, Double> problem = ...
088      * final Genotype<DoubleGene> gt = problem.codec().encoding().newInstance();
089      *
090      * final SomeObject arg = problem.decode(gt);
091      * }</pre>
092      *
093      @since 4.2
094      *
095      @see Codec#decode(Genotype)
096      *
097      @param genotype the genotype to be converted
098      @return the converted genotype
099      @throws NullPointerException if the given {@code genotype} is {@code null}
100      */
101     public default T decode(final Genotype<G> genotype) {
102         return codec().decode(genotype);
103     }
104 
105     /**
106      * Returns the fitness value for the given argument.
107      *
108      @since 4.1
109      *
110      @param arg the argument of the fitness function
111      @return the fitness value
112      */
113     public default C fitness(final T arg) {
114         return fitness().apply(arg);
115     }
116 
117     /**
118      * Returns the fitness value for the given argument.
119      *
120      @since 4.1
121      *
122      @param genotype the argument of the fitness function
123      @return the fitness value
124      */
125     public default C fitness(final Genotype<G> genotype) {
126         return fitness(codec().decode(genotype));
127     }
128 
129     /**
130      * Return a new optimization <i>problem</i> with the given parameters.
131      *
132      @param fitness the problem fitness function
133      @param codec the evolution engine codec
134      @param <T> the (<i>native</i>) argument type of the problem fitness function
135      @param <G> the gene type the evolution engine is working with
136      @param <C> the result type of the fitness function
137      @return a new problem object from the given parameters
138      @throws NullPointerException if one of the arguments is {@code null}
139      */
140     public static <T, G extends Gene<?, G>, C extends Comparable<? super C>>
141     Problem<T, G, C> of(
142         final Function<T, C> fitness,
143         final Codec<T, G> codec
144     ) {
145         requireNonNull(fitness);
146         requireNonNull(codec);
147 
148         return new Problem<T, G, C>() {
149             @Override
150             public Codec<T, G> codec() {
151                 return codec;
152             }
153 
154             @Override
155             public Function<T, C> fitness() {
156                 return fitness;
157             }
158         };
159     }
160 
161 }