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.math3.ml.neuralnet.sofm;
019
020import org.apache.commons.math3.ml.neuralnet.sofm.util.ExponentialDecayFunction;
021import org.apache.commons.math3.ml.neuralnet.sofm.util.QuasiSigmoidDecayFunction;
022import org.apache.commons.math3.exception.OutOfRangeException;
023
024/**
025 * Factory for creating instances of {@link LearningFactorFunction}.
026 *
027 * @since 3.3
028 */
029public class LearningFactorFunctionFactory {
030    /** Class contains only static methods. */
031    private LearningFactorFunctionFactory() {}
032
033    /**
034     * Creates an exponential decay {@link LearningFactorFunction function}.
035     * It will compute <code>a e<sup>-x / b</sup></code>,
036     * where {@code x} is the (integer) independent variable and
037     * <ul>
038     *  <li><code>a = initValue</code>
039     *  <li><code>b = -numCall / ln(valueAtNumCall / initValue)</code>
040     * </ul>
041     *
042     * @param initValue Initial value, i.e.
043     * {@link LearningFactorFunction#value(long) value(0)}.
044     * @param valueAtNumCall Value of the function at {@code numCall}.
045     * @param numCall Argument for which the function returns
046     * {@code valueAtNumCall}.
047     * @return the learning factor function.
048     * @throws org.apache.commons.math3.exception.OutOfRangeException
049     * if {@code initValue <= 0} or {@code initValue > 1}.
050     * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
051     * if {@code valueAtNumCall <= 0}.
052     * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
053     * if {@code valueAtNumCall >= initValue}.
054     * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
055     * if {@code numCall <= 0}.
056     */
057    public static LearningFactorFunction exponentialDecay(final double initValue,
058                                                          final double valueAtNumCall,
059                                                          final long numCall) {
060        if (initValue <= 0 ||
061            initValue > 1) {
062            throw new OutOfRangeException(initValue, 0, 1);
063        }
064
065        return new LearningFactorFunction() {
066            /** DecayFunction. */
067            private final ExponentialDecayFunction decay
068                = new ExponentialDecayFunction(initValue, valueAtNumCall, numCall);
069
070            /** {@inheritDoc} */
071            public double value(long n) {
072                return decay.value(n);
073            }
074        };
075    }
076
077    /**
078     * Creates an sigmoid-like {@code LearningFactorFunction function}.
079     * The function {@code f} will have the following properties:
080     * <ul>
081     *  <li>{@code f(0) = initValue}</li>
082     *  <li>{@code numCall} is the inflexion point</li>
083     *  <li>{@code slope = f'(numCall)}</li>
084     * </ul>
085     *
086     * @param initValue Initial value, i.e.
087     * {@link LearningFactorFunction#value(long) value(0)}.
088     * @param slope Value of the function derivative at {@code numCall}.
089     * @param numCall Inflexion point.
090     * @return the learning factor function.
091     * @throws org.apache.commons.math3.exception.OutOfRangeException
092     * if {@code initValue <= 0} or {@code initValue > 1}.
093     * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
094     * if {@code slope >= 0}.
095     * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
096     * if {@code numCall <= 0}.
097     */
098    public static LearningFactorFunction quasiSigmoidDecay(final double initValue,
099                                                           final double slope,
100                                                           final long numCall) {
101        if (initValue <= 0 ||
102            initValue > 1) {
103            throw new OutOfRangeException(initValue, 0, 1);
104        }
105
106        return new LearningFactorFunction() {
107            /** DecayFunction. */
108            private final QuasiSigmoidDecayFunction decay
109                = new QuasiSigmoidDecayFunction(initValue, slope, numCall);
110
111            /** {@inheritDoc} */
112            public double value(long n) {
113                return decay.value(n);
114            }
115        };
116    }
117}