ExponentialDecayFunction.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      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. package org.apache.commons.math4.neuralnet.sofm.util;

  18. import java.util.function.LongToDoubleFunction;

  19. import org.apache.commons.math4.neuralnet.internal.NeuralNetException;

  20. /**
  21.  * Exponential decay function: <code>a e<sup>-x / b</sup></code>,
  22.  * where {@code x} is the (integer) independent variable.
  23.  * <br>
  24.  * Class is immutable.
  25.  *
  26.  * @since 3.3
  27.  */
  28. public class ExponentialDecayFunction implements LongToDoubleFunction {
  29.     /** Factor {@code a}. */
  30.     private final double a;
  31.     /** Factor {@code 1 / b}. */
  32.     private final double oneOverB;

  33.     /**
  34.      * Creates an instance. It will be such that
  35.      * <ul>
  36.      *  <li>{@code a = initValue}</li>
  37.      *  <li>{@code b = -numCall / ln(valueAtNumCall / initValue)}</li>
  38.      * </ul>
  39.      *
  40.      * @param initValue Initial value, i.e. {@link #applyAsDouble(long) applyAsDouble(0)}.
  41.      * @param valueAtNumCall Value of the function at {@code numCall}.
  42.      * @param numCall Argument for which the function returns
  43.      * {@code valueAtNumCall}.
  44.      * @throws IllegalArgumentException if {@code initValue <= 0},
  45.      * {@code valueAtNumCall <= 0}, {@code valueAtNumCall >= initValue} or
  46.      * {@code numCall <= 0}.
  47.      */
  48.     public ExponentialDecayFunction(double initValue,
  49.                                     double valueAtNumCall,
  50.                                     long numCall) {
  51.         if (initValue <= 0) {
  52.             throw new NeuralNetException(NeuralNetException.NOT_STRICTLY_POSITIVE, initValue);
  53.         }
  54.         if (valueAtNumCall <= 0) {
  55.             throw new NeuralNetException(NeuralNetException.NOT_STRICTLY_POSITIVE, valueAtNumCall);
  56.         }
  57.         if (valueAtNumCall >= initValue) {
  58.             throw new NeuralNetException(NeuralNetException.TOO_LARGE, valueAtNumCall, initValue);
  59.         }
  60.         if (numCall <= 0) {
  61.             throw new NeuralNetException(NeuralNetException.NOT_STRICTLY_POSITIVE, numCall);
  62.         }

  63.         a = initValue;
  64.         oneOverB = -Math.log(valueAtNumCall / initValue) / numCall;
  65.     }

  66.     /**
  67.      * Computes <code>a e<sup>-numCall / b</sup></code>.
  68.      *
  69.      * @param numCall Current step of the training task.
  70.      * @return the value of the function at {@code numCall}.
  71.      */
  72.     @Override
  73.     public double applyAsDouble(long numCall) {
  74.         return a * Math.exp(-numCall * oneOverB);
  75.     }
  76. }