Problem.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.7.0).
003  * Copyright (c) 2007-2016 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@gmx.at)
019  */
020 package org.jenetics.engine;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.util.function.Function;
025 
026 import org.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  @see Codec
034  @see Engine
035  *
036  @param <T> the (<i>native</i>) argument type of the problem fitness function
037  @param <G> the gene type the evolution engine is working with
038  @param <C> the result type of the fitness function
039  *
040  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
041  @version 3.4
042  @since 3.4
043  */
044 public interface Problem<
045     T,
046     extends Gene<?, G>,
047     extends Comparable<? super C>
048 >
049 {
050 
051     /**
052      * Return the fitness function of the <i>problem</i> in the <i>native</i>
053      * problem domain.
054      *
055      @return the fitness function
056      */
057     public Function<T, C> fitness();
058 
059     /**
060      * Return the codec, which translates the types of the problem domain into
061      * types, which can be understand by the evolution {@code Engine}.
062      *
063      @return the engine codec
064      */
065     public Codec<T, G> codec();
066 
067     /**
068      * Return a new optimization <i>problem</i> with the given parameters.
069      *
070      @param fitness the problem fitness function
071      @param codec the evolution engine codec
072      @param <T> the (<i>native</i>) argument type of the problem fitness function
073      @param <G> the gene type the evolution engine is working with
074      @param <C> the result type of the fitness function
075      @return a new problem object from the given parameters
076      @throws NullPointerException if one of the arguments is {@code null}
077      */
078     public static <T, G extends Gene<?, G>, C extends Comparable<? super C>>
079     Problem<T, G, C> of(
080         final Function<T, C> fitness,
081         final Codec<T, G> codec
082     ) {
083         requireNonNull(fitness);
084         requireNonNull(codec);
085 
086         return new Problem<T, G, C>() {
087             @Override
088             public Codec<T, G> codec() {
089                 return codec;
090             }
091 
092             @Override
093             public Function<T, C> fitness() {
094                 return fitness;
095             }
096         };
097     }
098 
099 }