MATH-1158.

Use new sampler API.
This commit is contained in:
Gilles 2016-03-11 12:43:15 +01:00
parent a5035d0e1c
commit dff43a0530
2 changed files with 17 additions and 16 deletions

View File

@ -21,7 +21,8 @@ import org.apache.commons.math4.analysis.UnivariateFunction;
import org.apache.commons.math4.analysis.function.Constant;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.distribution.UniformRealDistribution;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
/**
* Creates functions that will select the initial values of a neuron's
@ -45,10 +46,10 @@ public class FeatureInitializerFactory {
* @throws org.apache.commons.math4.exception.NumberIsTooLargeException
* if {@code min >= max}.
*/
public static FeatureInitializer uniform(final RandomGenerator rng,
public static FeatureInitializer uniform(final UniformRandomProvider rng,
final double min,
final double max) {
return randomize(new UniformRealDistribution(rng, min, max),
return randomize(new UniformRealDistribution(min, max).createSampler(rng),
function(new Constant(0), 0, 0));
}
@ -64,8 +65,7 @@ public class FeatureInitializerFactory {
*/
public static FeatureInitializer uniform(final double min,
final double max) {
return randomize(new UniformRealDistribution(min, max),
function(new Constant(0), 0, 0));
return uniform(RandomSource.create(RandomSource.WELL_19937_C), min, max);
}
/**
@ -98,12 +98,12 @@ public class FeatureInitializerFactory {
/**
* Adds some amount of random data to the given initializer.
*
* @param random Random variable distribution.
* @param random Random variable distribution sampler.
* @param orig Original initializer.
* @return an initializer whose {@link FeatureInitializer#value() value}
* method will return {@code orig.value() + random.sample()}.
*/
public static FeatureInitializer randomize(final RealDistribution random,
public static FeatureInitializer randomize(final RealDistribution.Sampler random,
final FeatureInitializer orig) {
return new FeatureInitializer() {
/** {@inheritDoc} */

View File

@ -44,8 +44,8 @@ import org.apache.commons.math4.ml.neuralnet.sofm.LearningFactorFunction;
import org.apache.commons.math4.ml.neuralnet.sofm.LearningFactorFunctionFactory;
import org.apache.commons.math4.ml.neuralnet.sofm.NeighbourhoodSizeFunction;
import org.apache.commons.math4.ml.neuralnet.sofm.NeighbourhoodSizeFunctionFactory;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.random.Well44497b;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.util.FastMath;
/**
@ -56,7 +56,7 @@ import org.apache.commons.math4.util.FastMath;
public class TravellingSalesmanSolver {
private static final long FIRST_NEURON_ID = 0;
/** RNG. */
private final RandomGenerator random;
private final UniformRandomProvider random;
/** Set of cities. */
private final Set<City> cities = new HashSet<City>();
/** SOFM. */
@ -72,7 +72,7 @@ public class TravellingSalesmanSolver {
*/
public TravellingSalesmanSolver(City[] cityList,
double numNeuronsPerCity) {
this(cityList, numNeuronsPerCity, new Well44497b().nextLong());
this(cityList, numNeuronsPerCity, RandomSource.createLong());
}
/**
@ -84,7 +84,7 @@ public class TravellingSalesmanSolver {
public TravellingSalesmanSolver(City[] cityList,
double numNeuronsPerCity,
long seed) {
random = new Well44497b(seed);
random = RandomSource.create(RandomSource.WELL_1024_A, seed);
// Make sure that each city will appear only once in the list.
for (City city : cityList) {
@ -338,12 +338,13 @@ public class TravellingSalesmanSolver {
final UnivariateFunction f1 = FunctionUtils.add(h1, new Constant(centre[0]));
final UnivariateFunction f2 = FunctionUtils.add(h2, new Constant(centre[1]));
final RealDistribution u
= new UniformRealDistribution(random, -0.05 * radius, 0.05 * radius);
final RealDistribution u = new UniformRealDistribution(-0.05 * radius, 0.05 * radius);
return new FeatureInitializer[] {
FeatureInitializerFactory.randomize(u, FeatureInitializerFactory.function(f1, 0, 1)),
FeatureInitializerFactory.randomize(u, FeatureInitializerFactory.function(f2, 0, 1))
FeatureInitializerFactory.randomize(u.createSampler(random),
FeatureInitializerFactory.function(f1, 0, 1)),
FeatureInitializerFactory.randomize(u.createSampler(random),
FeatureInitializerFactory.function(f2, 0, 1))
};
}
}