001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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
018package org.apache.commons.math4.neuralnet.sofm.util;
019
020import java.util.function.DoubleUnaryOperator;
021import java.util.function.LongToDoubleFunction;
022
023import org.apache.commons.math4.neuralnet.internal.NeuralNetException;
024
025/**
026 * Decay function whose shape is similar to a sigmoid.
027 * <br>
028 * Class is immutable.
029 *
030 * @since 3.3
031 */
032public class QuasiSigmoidDecayFunction implements LongToDoubleFunction {
033    /** Sigmoid. */
034    private final DoubleUnaryOperator sigmoid;
035    /** See {@link #value(long)}. */
036    private final double scale;
037
038    /**
039     * Creates an instance.
040     * The function {@code f} will have the following properties:
041     * <ul>
042     *  <li>{@code f(0) = initValue}</li>
043     *  <li>{@code numCall} is the inflexion point</li>
044     *  <li>{@code slope = f'(numCall)}</li>
045     * </ul>
046     *
047     * @param initValue Initial value, i.e. {@link #applyAsDouble(long) applyAsDouble(0)}.
048     * @param slope Value of the function derivative at {@code numCall}.
049     * @param numCall Inflexion point.
050     * @throws IllegalArgumentException if {@code initValue <= 0},
051     * {@code slope >= 0} or {@code numCall <= 0}.
052     */
053    public QuasiSigmoidDecayFunction(double initValue,
054                                     double slope,
055                                     long numCall) {
056        if (initValue <= 0) {
057            throw new NeuralNetException(NeuralNetException.NOT_STRICTLY_POSITIVE, initValue);
058        }
059        if (slope >= 0) {
060            throw new NeuralNetException(NeuralNetException.TOO_LARGE, slope, 0);
061        }
062        if (numCall <= 1) {
063            throw new NeuralNetException(NeuralNetException.TOO_SMALL, numCall, 1);
064        }
065
066        final double k = initValue;
067        final double m = numCall;
068        final double b = 4 * slope / initValue;
069        sigmoid = x -> k / (1 + Math.exp(b * (m - x)));
070
071        final double y0 = sigmoid.applyAsDouble(0d);
072        scale = k / y0;
073    }
074
075    /**
076     * Computes the value of the learning factor.
077     *
078     * @param numCall Current step of the training task.
079     * @return the value of the function at {@code numCall}.
080     */
081    @Override
082    public double applyAsDouble(long numCall) {
083        return scale * sigmoid.applyAsDouble((double) numCall);
084    }
085}