001/* 002 * Java Genetic Algorithm Library (jenetics-8.1.0). 003 * Copyright (c) 2007-2024 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 */ 020package io.jenetics.prog.regression; 021 022import java.util.List; 023import java.util.function.Function; 024 025import io.jenetics.ext.util.Tree; 026 027import io.jenetics.prog.op.Op; 028 029/** 030 * This interface represents a set of sample points, which can be evaluated with 031 * a given evolved <em>program</em>. 032 * 033 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 034 * @version 7.1 035 * @since 6.0 036 */ 037@FunctionalInterface 038public interface Sampling<T> { 039 040 /** 041 * This class represents the result of a sample calculation, which contains 042 * the array of calculated values and a corresponding array with expected 043 * sample values. This two arrays can then be used for calculating the 044 * error between modeled regression function and actual sample values. 045 * 046 * @param <T> the sample result type 047 */ 048 record Result<T>(T[] calculated, T[] expected) { 049 /** 050 * @param calculated the calculated result values 051 * @param expected the expected sample result values 052 * @throws NullPointerException if one of the arguments is {@code null} 053 */ 054 public Result { 055 calculated = calculated.clone(); 056 expected = expected.clone(); 057 } 058 } 059 060 /** 061 * Evaluates the given {@code program} tree with its sample points. The 062 * returned result object may be {@code null} if no sample point has been 063 * added to the <em>sampling</em> when calling the {@code eval} method. 064 * 065 * @param program the program to evaluate 066 * @return the evaluated sample result. May be {@code null} if the sampling 067 * is empty and contains no sample points. 068 */ 069 //@Deprecated(since = "7.1", forRemoval = true) 070 Result<T> eval(final Tree<? extends Op<T>, ?> program); 071 072 /** 073 * Evaluates the given {@code function} tree with its sample points. The 074 * returned result object may be {@code null} if no sample point has been 075 * added to the <em>sampling</em> when calling the {@code eval} method. 076 * 077 * @param function the function to evaluate 078 * @return the evaluated sample result. May be {@code null} if the sampling 079 * is empty and contains no sample points. 080 */ 081 default Result<T> eval(final Function<? super T[], ? extends T> function) { 082 throw new UnsupportedOperationException(); 083 } 084 085 /** 086 * Create a new sampling object from the given sample points. 087 * 088 * @since 7.1 089 * 090 * @param samples the sample points 091 * @param <T> the sample type 092 * @return a new sampling object 093 */ 094 static <T> Sampling<T> of(final List<? extends Sample<? extends T>> samples) { 095 return new SampleList<>(samples); 096 } 097 098 /** 099 * Create a new sampling object from the given sample points. 100 * 101 * @since 7.1 102 * 103 * @param samples the sample points 104 * @param <T> the sample type 105 * @return a new sampling object 106 */ 107 @SafeVarargs 108 static <T> Sampling<T> of(final Sample<? extends T>... samples) { 109 return Sampling.of(List.of(samples)); 110 } 111 112}