Sampling.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-6.1.0).
03  * Copyright (c) 2007-2020 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.prog.regression;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import io.jenetics.ext.util.Tree;
25 
26 import io.jenetics.prog.op.Op;
27 
28 /**
29  * This interface represents a set of sample points, which can be evaluated with
30  * a given evolved <em>program</em>.
31  *
32  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
33  @version 6.0
34  @since 6.0
35  */
36 @FunctionalInterface
37 public interface Sampling<T> {
38 
39     /**
40      * This class represents the result of a sample calculation, which contains
41      * the array of calculated values and a corresponding array with expected
42      * sample values. This two arrays can then be used for calculating the
43      * error between modeled regression function and actual sample values.
44      *
45      @param <T> the sample result  type
46      */
47     final class Result<T> {
48         private final T[] _calculated;
49         private final T[] _expected;
50 
51         private Result(final T[] calculated, final T[] expected) {
52             _calculated = requireNonNull(calculated);
53             _expected = requireNonNull(expected);
54         }
55 
56         /**
57          * Return the the calculated result values.
58          *
59          @return the the calculated result values
60          */
61         public T[] calculated() {
62             return _calculated;
63         }
64 
65         /**
66          * Return the expected sample result values.
67          *
68          @return the expected sample result values
69          */
70         public T[] expected() {
71             return _expected;
72         }
73 
74         /**
75          * Create a new sampling result object.
76          *
77          @param calculated the calculated values
78          @param expected the expected sample values
79          @param <T> the sample type
80          @return a new sampling result object
81          @throws NullPointerException if one of the arguments is {@code null}
82          */
83         public static <T> Result<T> of(final T[] calculated, final T[] expected) {
84             return new Result<>(calculated.clone(), expected.clone());
85         }
86     }
87 
88     /**
89      * Evaluates the given {@code program} tree with its sample points. The
90      * returned result object may be {@code null} if no sample point has been
91      * added to the <em>sampling</em> when calling the {@code eval} method.
92      *
93      @param program the program to evaluate
94      @return the evaluated sample result. May be {@code null} if the sampling
95      *         is empty and contains no sample points.
96      */
97     Result<T> eval(final Tree<? extends Op<T>, ?> program);
98 
99 }