From 63a9488f2910434851e98a8fc0ec8c053b24083b Mon Sep 17 00:00:00 2001 From: Gilles Date: Wed, 11 May 2016 14:25:36 +0200 Subject: [PATCH] MATH-1335 Use new RNG API. Unrelated code comments removed in unit test. --- .../MultiStartUnivariateOptimizer.java | 6 ++--- .../MultiStartUnivariateOptimizerTest.java | 23 +++++++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizer.java b/src/main/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizer.java index 5383a5268..65bfa6776 100644 --- a/src/main/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizer.java +++ b/src/main/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizer.java @@ -26,7 +26,7 @@ import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.optim.MaxEval; import org.apache.commons.math4.optim.OptimizationData; import org.apache.commons.math4.optim.nonlinear.scalar.GoalType; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; /** * Special implementation of the {@link UnivariateOptimizer} interface @@ -47,7 +47,7 @@ public class MultiStartUnivariateOptimizer /** Number of starts to go. */ private final int starts; /** Random generator for multi-start. */ - private final RandomGenerator generator; + private final UniformRandomProvider generator; /** Found optima. */ private UnivariatePointValuePair[] optima; /** Optimization data. */ @@ -75,7 +75,7 @@ public class MultiStartUnivariateOptimizer */ public MultiStartUnivariateOptimizer(final UnivariateOptimizer optimizer, final int starts, - final RandomGenerator generator) { + final UniformRandomProvider generator) { super(optimizer.getConvergenceChecker()); if (starts < 1) { diff --git a/src/test/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizerTest.java b/src/test/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizerTest.java index ec8ec06be..a452ac808 100644 --- a/src/test/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizerTest.java +++ b/src/test/java/org/apache/commons/math4/optim/univariate/MultiStartUnivariateOptimizerTest.java @@ -28,7 +28,8 @@ import org.apache.commons.math4.optim.univariate.SearchInterval; import org.apache.commons.math4.optim.univariate.UnivariateObjectiveFunction; import org.apache.commons.math4.optim.univariate.UnivariateOptimizer; import org.apache.commons.math4.optim.univariate.UnivariatePointValuePair; -import org.apache.commons.math4.random.JDKRandomGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.junit.Assert; import org.junit.Test; @@ -37,8 +38,7 @@ public class MultiStartUnivariateOptimizerTest { @Test(expected=MathIllegalStateException.class) public void testMissingMaxEval() { UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); - JDKRandomGenerator g = new JDKRandomGenerator(); - g.setSeed(44428400075l); + UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 44428400075l); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g); optimizer.optimize(new UnivariateObjectiveFunction(new Sin()), GoalType.MINIMIZE, @@ -47,8 +47,7 @@ public class MultiStartUnivariateOptimizerTest { @Test(expected=MathIllegalStateException.class) public void testMissingSearchInterval() { UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); - JDKRandomGenerator g = new JDKRandomGenerator(); - g.setSeed(44428400075l); + UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 44428400075l); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g); optimizer.optimize(new MaxEval(300), new UnivariateObjectiveFunction(new Sin()), @@ -59,8 +58,7 @@ public class MultiStartUnivariateOptimizerTest { public void testSinMin() { UnivariateFunction f = new Sin(); UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); - JDKRandomGenerator g = new JDKRandomGenerator(); - g.setSeed(44428400075l); + UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 44428400075l); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g); optimizer.optimize(new MaxEval(300), new UnivariateObjectiveFunction(f), @@ -79,12 +77,9 @@ public class MultiStartUnivariateOptimizerTest { @Test public void testQuinticMin() { - // The quintic function has zeros at 0, +-0.5 and +-1. - // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643, UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14); - JDKRandomGenerator g = new JDKRandomGenerator(); - g.setSeed(4312000053L); + UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 4312000053L); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g); UnivariatePointValuePair optimum @@ -92,7 +87,7 @@ public class MultiStartUnivariateOptimizerTest { new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(-0.3, -0.2)); - Assert.assertEquals(-0.27195613, optimum.getPoint(), 1e-9); + Assert.assertEquals(-0.271956128, optimum.getPoint(), 1e-9); Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9); UnivariatePointValuePair[] optima = optimizer.getOptima(); @@ -114,8 +109,7 @@ public class MultiStartUnivariateOptimizerTest { } }; UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14); - JDKRandomGenerator g = new JDKRandomGenerator(); - g.setSeed(4312000053L); + UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 4312000053L); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g); try { @@ -135,5 +129,4 @@ public class MultiStartUnivariateOptimizerTest { private static class LocalException extends RuntimeException { private static final long serialVersionUID = 1194682757034350629L; } - }