You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/05/17 15:49:38 UTC

[05/25] [math] MATH-1335

MATH-1335

Use new RNG API.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/f81e0465
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/f81e0465
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/f81e0465

Branch: refs/heads/develop
Commit: f81e0465460ff9abd450eda836bbb4f9b13837c7
Parents: 57e01f4
Author: Gilles <er...@apache.org>
Authored: Wed May 11 13:25:42 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue May 17 15:30:23 2016 +0200

----------------------------------------------------------------------
 .../math4/ml/clustering/FuzzyKMeansClusterer.java     | 12 ++++++------
 .../math4/ml/clustering/KMeansPlusPlusClusterer.java  | 14 +++++++-------
 .../math4/ml/clustering/FuzzyKMeansClustererTest.java |  6 +++---
 .../ml/clustering/KMeansPlusPlusClustererTest.java    | 11 ++++-------
 4 files changed, 20 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/f81e0465/src/main/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClusterer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClusterer.java b/src/main/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClusterer.java
index e57697f..f650d24 100644
--- a/src/main/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClusterer.java
+++ b/src/main/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClusterer.java
@@ -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;
     }
 

http://git-wip-us.apache.org/repos/asf/commons-math/blob/f81e0465/src/main/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClusterer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClusterer.java b/src/main/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClusterer.java
index 1901013..0b8ad96 100644
--- a/src/main/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClusterer.java
+++ b/src/main/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClusterer.java
@@ -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;
     }
 

http://git-wip-us.apache.org/repos/asf/commons-math/blob/f81e0465/src/test/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClustererTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClustererTest.java b/src/test/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClustererTest.java
index a46c01e..4950c80 100644
--- a/src/test/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClustererTest.java
+++ b/src/test/java/org/apache/commons/math4/ml/clustering/FuzzyKMeansClustererTest.java
@@ -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);
 

http://git-wip-us.apache.org/repos/asf/commons-math/blob/f81e0465/src/test/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClustererTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClustererTest.java b/src/test/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClustererTest.java
index 39ef2c5..0e07a1f 100644
--- a/src/test/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClustererTest.java
+++ b/src/test/java/org/apache/commons/math4/ml/clustering/KMeansPlusPlusClustererTest.java
@@ -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,