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.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.math3.exception.NotStrictlyPositiveException 049 * if {@code initValue <= 0}. 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 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 public int value(long n) { 067 return (int) FastMath.rint(decay.value(n)); 068 } 069 }; 070 } 071 072 /** 073 * Creates an sigmoid-like {@code NeighbourhoodSizeFunction function}. 074 * The function {@code f} will have the following properties: 075 * <ul> 076 * <li>{@code f(0) = initValue}</li> 077 * <li>{@code numCall} is the inflexion point</li> 078 * <li>{@code slope = f'(numCall)}</li> 079 * </ul> 080 * 081 * @param initValue Initial value, i.e. 082 * {@link NeighbourhoodSizeFunction#value(long) value(0)}. 083 * @param slope Value of the function derivative at {@code numCall}. 084 * @param numCall Inflexion point. 085 * @return the neighbourhood size function. 086 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException 087 * if {@code initValue <= 0}. 088 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException 089 * if {@code slope >= 0}. 090 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException 091 * if {@code numCall <= 0}. 092 */ 093 public static NeighbourhoodSizeFunction quasiSigmoidDecay(final double initValue, 094 final double slope, 095 final long numCall) { 096 return new NeighbourhoodSizeFunction() { 097 /** DecayFunction. */ 098 private final QuasiSigmoidDecayFunction decay 099 = new QuasiSigmoidDecayFunction(initValue, slope, numCall); 100 101 /** {@inheritDoc} */ 102 public int value(long n) { 103 return (int) FastMath.rint(decay.value(n)); 104 } 105 }; 106 } 107}