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.ml.neuralnet.sofm;
019
020import org.apache.commons.math4.exception.OutOfRangeException;
021import org.apache.commons.math4.ml.neuralnet.sofm.util.ExponentialDecayFunction;
022import org.apache.commons.math4.ml.neuralnet.sofm.util.QuasiSigmoidDecayFunction;
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.math4.exception.OutOfRangeException
049     * if {@code initValue <= 0} or {@code initValue > 1}.
050     * @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
051     * if {@code valueAtNumCall <= 0}.
052     * @throws org.apache.commons.math4.exception.NumberIsTooLargeException
053     * if {@code valueAtNumCall >= initValue}.
054     * @throws org.apache.commons.math4.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            @Override
072            public double value(long n) {
073                return decay.value(n);
074            }
075        };
076    }
077
078    /**
079     * Creates an sigmoid-like {@code LearningFactorFunction function}.
080     * The function {@code f} will have the following properties:
081     * <ul>
082     *  <li>{@code f(0) = initValue}</li>
083     *  <li>{@code numCall} is the inflexion point</li>
084     *  <li>{@code slope = f'(numCall)}</li>
085     * </ul>
086     *
087     * @param initValue Initial value, i.e.
088     * {@link LearningFactorFunction#value(long) value(0)}.
089     * @param slope Value of the function derivative at {@code numCall}.
090     * @param numCall Inflexion point.
091     * @return the learning factor function.
092     * @throws org.apache.commons.math4.exception.OutOfRangeException
093     * if {@code initValue <= 0} or {@code initValue > 1}.
094     * @throws org.apache.commons.math4.exception.NumberIsTooLargeException
095     * if {@code slope >= 0}.
096     * @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
097     * if {@code numCall <= 0}.
098     */
099    public static LearningFactorFunction quasiSigmoidDecay(final double initValue,
100                                                           final double slope,
101                                                           final long numCall) {
102        if (initValue <= 0 ||
103            initValue > 1) {
104            throw new OutOfRangeException(initValue, 0, 1);
105        }
106
107        return new LearningFactorFunction() {
108            /** DecayFunction. */
109            private final QuasiSigmoidDecayFunction decay
110                = new QuasiSigmoidDecayFunction(initValue, slope, numCall);
111
112            /** {@inheritDoc} */
113            @Override
114            public double value(long n) {
115                return decay.value(n);
116            }
117        };
118    }
119}