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 static java.util.Objects.requireNonNull;
023
024import java.util.function.DoubleBinaryOperator;
025
026import io.jenetics.ext.util.Tree;
027
028import io.jenetics.prog.op.Op;
029
030/**
031 * This function calculates the <em>overall</em> error of a given program tree.
032 * The error is calculated from the {@link LossFunction} and, if desired, the
033 * program {@link Complexity}.
034 *
035 * {@snippet lang="java":
036 * final Error<Double> error = Error.of(LossFunction::mse, Complexity.ofNodeCount(50));
037 * }
038 *
039 * @see LossFunction
040 * @see Complexity
041 *
042 * @param <T> the sample type
043 *
044 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
045 * @version 5.0
046 * @since 5.0
047 */
048@FunctionalInterface
049public interface Error<T> {
050
051        /**
052         * Calculates the <em>overall</em> error of a given program tree. The error
053         * is calculated from the {@link LossFunction} and, if desired, the program
054         * {@link Complexity}.
055         *
056         * @param program the program tree which calculated the {@code calculated}
057         *        values
058         * @param calculated the calculated function values
059         * @param expected the expected function values
060         * @return the overall program error
061         * @throws NullPointerException if one of the arguments is {@code null}
062         */
063        double apply(
064                final Tree<? extends Op<T>, ?> program,
065                final T[] calculated,
066                final T[] expected
067        );
068
069        /**
070         * Creates an error function which only uses the given {@code loss} function
071         * for calculating the program error
072         *
073         * @param <T> the sample type
074         * @param loss the loss function to use for calculating the program error
075         * @return an error function which uses the loss function for error
076         *         calculation
077         * @throws NullPointerException if the given {@code loss} function is
078         *         {@code null}
079         */
080        static <T> Error<T> of(final LossFunction<T> loss) {
081                requireNonNull(loss);
082                return (p, c, e) -> loss.apply(c, e);
083        }
084
085        /**
086         * Creates an error function by combining the given {@code loss} function
087         * and program {@code complexity}. The loss function and program complexity
088         * is combined in the following way: {@code error = loss + loss*complexity}.
089         * The complexity function penalizes programs which grow to big.
090         *
091         * @param <T> the sample type
092         * @param loss the loss function
093         * @param complexity the program complexity measure
094         * @return a new error function by combining the given loss and complexity
095         *         function
096         * @throws NullPointerException if one of the functions is {@code null}
097         */
098        static <T> Error<T>
099        of(final LossFunction<T> loss, final Complexity<T> complexity) {
100                return of(loss, complexity, (lss, cpx) -> lss + lss*cpx);
101        }
102
103        /**
104         * Creates an error function by combining the given {@code loss} function
105         * and program {@code complexity}. The loss function and program complexity
106         * is combined in the following way: {@code error = loss + loss*complexity}.
107         * The complexity function penalizes programs which grow to big.
108         *
109         * @param <T> the sample type
110         * @param loss the loss function
111         * @param complexity the program complexity measure
112         * @param compose the function which composes the {@code loss} and
113         *        {@code complexity} function
114         * @return a new error function by combining the given loss and complexity
115         *         function
116         * @throws NullPointerException if one of the functions is {@code null}
117         */
118        static <T> Error<T> of(
119                final LossFunction<T> loss,
120                final Complexity<T> complexity,
121                final DoubleBinaryOperator compose
122        ) {
123                requireNonNull(loss);
124                requireNonNull(complexity);
125                requireNonNull(compose);
126
127                return (p, c, e) ->
128                        compose.applyAsDouble(loss.apply(c, e), complexity.apply(p));
129        }
130
131}