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.ml.neuralnet.sofm.util.ExponentialDecayFunction;
021import org.apache.commons.math4.ml.neuralnet.sofm.util.QuasiSigmoidDecayFunction;
022import org.apache.commons.math4.util.FastMath;
023
024/**
025 * Factory for creating instances of {@link NeighbourhoodSizeFunction}.
026 *
027 * @since 3.3
028 */
029public class NeighbourhoodSizeFunctionFactory {
030    /** Class contains only static methods. */
031    private NeighbourhoodSizeFunctionFactory() {}
032
033    /**
034     * Creates an exponential decay {@link NeighbourhoodSizeFunction 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 NeighbourhoodSizeFunction#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 neighbourhood size function.
048     * @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
049     * if {@code initValue <= 0}.
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 NeighbourhoodSizeFunction exponentialDecay(final double initValue,
058                                                             final double valueAtNumCall,
059                                                             final long numCall) {
060        return new NeighbourhoodSizeFunction() {
061            /** DecayFunction. */
062            private final ExponentialDecayFunction decay
063                = new ExponentialDecayFunction(initValue, valueAtNumCall, numCall);
064
065            /** {@inheritDoc} */
066            @Override
067            public int value(long n) {
068                return (int) FastMath.rint(decay.value(n));
069            }
070        };
071    }
072
073    /**
074     * Creates an sigmoid-like {@code NeighbourhoodSizeFunction function}.
075     * The function {@code f} will have the following properties:
076     * <ul>
077     *  <li>{@code f(0) = initValue}</li>
078     *  <li>{@code numCall} is the inflexion point</li>
079     *  <li>{@code slope = f'(numCall)}</li>
080     * </ul>
081     *
082     * @param initValue Initial value, i.e.
083     * {@link NeighbourhoodSizeFunction#value(long) value(0)}.
084     * @param slope Value of the function derivative at {@code numCall}.
085     * @param numCall Inflexion point.
086     * @return the neighbourhood size function.
087     * @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
088     * if {@code initValue <= 0}.
089     * @throws org.apache.commons.math4.exception.NumberIsTooLargeException
090     * if {@code slope >= 0}.
091     * @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
092     * if {@code numCall <= 0}.
093     */
094    public static NeighbourhoodSizeFunction quasiSigmoidDecay(final double initValue,
095                                                              final double slope,
096                                                              final long numCall) {
097        return new NeighbourhoodSizeFunction() {
098            /** DecayFunction. */
099            private final QuasiSigmoidDecayFunction decay
100                = new QuasiSigmoidDecayFunction(initValue, slope, numCall);
101
102            /** {@inheritDoc} */
103            @Override
104            public int value(long n) {
105                return (int) FastMath.rint(decay.value(n));
106            }
107        };
108    }
109}