MATH-1335

Use new RNG API.
This commit is contained in:
Gilles 2016-05-11 13:25:42 +02:00
parent 57e01f404e
commit f81e046546
4 changed files with 20 additions and 23 deletions

View File

@ -28,8 +28,8 @@ import org.apache.commons.math4.linear.MatrixUtils;
import org.apache.commons.math4.linear.RealMatrix;
import org.apache.commons.math4.ml.distance.DistanceMeasure;
import org.apache.commons.math4.ml.distance.EuclideanDistance;
import org.apache.commons.math4.random.JDKRandomGenerator;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathArrays;
import org.apache.commons.math4.util.MathUtils;
@ -83,7 +83,7 @@ public class FuzzyKMeansClusterer<T extends Clusterable> extends Clusterer<T> {
private final double epsilon;
/** Random generator for choosing initial centers. */
private final RandomGenerator random;
private final UniformRandomProvider random;
/** The membership matrix. */
private double[][] membershipMatrix;
@ -120,7 +120,7 @@ public class FuzzyKMeansClusterer<T extends Clusterable> extends Clusterer<T> {
public FuzzyKMeansClusterer(final int k, final double fuzziness,
final int maxIterations, final DistanceMeasure measure)
throws NumberIsTooSmallException {
this(k, fuzziness, maxIterations, measure, DEFAULT_EPSILON, new JDKRandomGenerator());
this(k, fuzziness, maxIterations, measure, DEFAULT_EPSILON, RandomSource.create(RandomSource.MT_64));
}
/**
@ -137,7 +137,7 @@ public class FuzzyKMeansClusterer<T extends Clusterable> extends Clusterer<T> {
*/
public FuzzyKMeansClusterer(final int k, final double fuzziness,
final int maxIterations, final DistanceMeasure measure,
final double epsilon, final RandomGenerator random)
final double epsilon, final UniformRandomProvider random)
throws NumberIsTooSmallException {
super(measure);
@ -192,7 +192,7 @@ public class FuzzyKMeansClusterer<T extends Clusterable> extends Clusterer<T> {
* Returns the random generator this instance will use.
* @return the random generator
*/
public RandomGenerator getRandomGenerator() {
public UniformRandomProvider getRandomGenerator() {
return random;
}

View File

@ -28,8 +28,8 @@ import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
import org.apache.commons.math4.ml.distance.DistanceMeasure;
import org.apache.commons.math4.ml.distance.EuclideanDistance;
import org.apache.commons.math4.random.JDKRandomGenerator;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.stat.descriptive.moment.Variance;
import org.apache.commons.math4.util.MathUtils;
@ -65,7 +65,7 @@ public class KMeansPlusPlusClusterer<T extends Clusterable> extends Clusterer<T>
private final int maxIterations;
/** Random generator for choosing initial centers. */
private final RandomGenerator random;
private final UniformRandomProvider random;
/** Selected strategy for empty clusters. */
private final EmptyClusterStrategy emptyStrategy;
@ -109,7 +109,7 @@ public class KMeansPlusPlusClusterer<T extends Clusterable> extends Clusterer<T>
* @param measure the distance measure to use
*/
public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure) {
this(k, maxIterations, measure, new JDKRandomGenerator());
this(k, maxIterations, measure, RandomSource.create(RandomSource.MT_64));
}
/** Build a clusterer.
@ -125,7 +125,7 @@ public class KMeansPlusPlusClusterer<T extends Clusterable> extends Clusterer<T>
*/
public KMeansPlusPlusClusterer(final int k, final int maxIterations,
final DistanceMeasure measure,
final RandomGenerator random) {
final UniformRandomProvider random) {
this(k, maxIterations, measure, random, EmptyClusterStrategy.LARGEST_VARIANCE);
}
@ -141,7 +141,7 @@ public class KMeansPlusPlusClusterer<T extends Clusterable> extends Clusterer<T>
*/
public KMeansPlusPlusClusterer(final int k, final int maxIterations,
final DistanceMeasure measure,
final RandomGenerator random,
final UniformRandomProvider random,
final EmptyClusterStrategy emptyStrategy) {
super(measure);
this.k = k;
@ -170,7 +170,7 @@ public class KMeansPlusPlusClusterer<T extends Clusterable> extends Clusterer<T>
* Returns the random generator this instance will use.
* @return the random generator
*/
public RandomGenerator getRandomGenerator() {
public UniformRandomProvider getRandomGenerator() {
return random;
}

View File

@ -28,8 +28,8 @@ import org.apache.commons.math4.ml.clustering.DoublePoint;
import org.apache.commons.math4.ml.clustering.FuzzyKMeansClusterer;
import org.apache.commons.math4.ml.distance.CanberraDistance;
import org.apache.commons.math4.ml.distance.DistanceMeasure;
import org.apache.commons.math4.random.JDKRandomGenerator;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
@ -97,7 +97,7 @@ public class FuzzyKMeansClustererTest {
@Test
public void testGetters() {
final DistanceMeasure measure = new CanberraDistance();
final RandomGenerator random = new JDKRandomGenerator();
final UniformRandomProvider random = RandomSource.create(RandomSource.MT_64);
final FuzzyKMeansClusterer<DoublePoint> clusterer =
new FuzzyKMeansClusterer<DoublePoint>(3, 2.0, 100, measure, 1e-6, random);

View File

@ -28,20 +28,19 @@ import org.apache.commons.math4.ml.clustering.Cluster;
import org.apache.commons.math4.ml.clustering.DoublePoint;
import org.apache.commons.math4.ml.clustering.KMeansPlusPlusClusterer;
import org.apache.commons.math4.ml.distance.EuclideanDistance;
import org.apache.commons.math4.random.JDKRandomGenerator;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KMeansPlusPlusClustererTest {
private RandomGenerator random;
private UniformRandomProvider random;
@Before
public void setUp() {
random = new JDKRandomGenerator();
random.setSeed(1746432956321l);
random = RandomSource.create(RandomSource.MT_64, 1746432956321l);
}
/**
@ -152,10 +151,8 @@ public class KMeansPlusPlusClustererTest {
// Ask a KMeansPlusPlusClusterer to run zero iterations (i.e., to simply choose initial
// cluster centers).
final long RANDOM_SEED = 0;
final int NUM_CLUSTERS = 2;
final int NUM_ITERATIONS = 0;
random.setSeed(RANDOM_SEED);
KMeansPlusPlusClusterer<DoublePoint> clusterer =
new KMeansPlusPlusClusterer<DoublePoint>(NUM_CLUSTERS, NUM_ITERATIONS,