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 2021/06/05 21:52:10 UTC
[commons-math] branch master updated: MATH-1552: Fiddling with
setup and expectations (unit tests).
This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new df927f0 MATH-1552: Fiddling with setup and expectations (unit tests).
df927f0 is described below
commit df927f0b674d8c6cd5f535c2995777a4dd7be101
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Sat Jun 5 23:48:27 2021 +0200
MATH-1552: Fiddling with setup and expectations (unit tests).
---
.../nonlinear/scalar/noderiv/OptimTestUtils.java | 56 ++++++++++++++--------
.../SimplexOptimizerMultiDirectionalTest.java | 28 +++++------
.../noderiv/SimplexOptimizerNelderMeadTest.java | 56 +++++++++++-----------
3 files changed, 75 insertions(+), 65 deletions(-)
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/OptimTestUtils.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/OptimTestUtils.java
index 5c3cb7b..ae81da5 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/OptimTestUtils.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/OptimTestUtils.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath;
@@ -213,29 +214,24 @@ class OptimTestUtils {
}
static class Ackley implements MultivariateFunction {
- private double axisratio;
-
- Ackley(double axra) {
- axisratio = axra;
- }
-
- public Ackley() {
- this(1);
- }
-
+ private static final double A = 20;
+ private static final double B = 0.2;
+ private static final double C = 2 * Math.PI;
+
@Override
public double value(double[] x) {
- double f = 0;
- double res2 = 0;
- double fac = 0;
- for (int i = 0; i < x.length; ++i) {
- fac = AccurateMath.pow(axisratio, (i - 1.) / (x.length - 1.));
- f += fac * fac * x[i] * x[i];
- res2 += AccurateMath.cos(2. * AccurateMath.PI * fac * x[i]);
+ final int dim = x.length;
+ double acc1 = 0;
+ double acc2 = 0;
+ for (int i = 0; i < dim; i++) {
+ final double v = x[i];
+ acc1 += v * v;
+ acc2 += Math.cos(C * v);
}
- f = (20. - 20. * AccurateMath.exp(-0.2 * AccurateMath.sqrt(f / x.length))
- + AccurateMath.exp(1.) - AccurateMath.exp(res2 / x.length));
- return f;
+ acc1 = -B * Math.sqrt(acc1 / dim);
+ acc2 /= dim;
+
+ return -A * Math.exp(acc1) - Math.exp(acc2) + A + Math.E;
}
}
@@ -334,11 +330,29 @@ class OptimTestUtils {
}
static double[] point(int n, double value) {
- double[] ds = new double[n];
+ final double[] ds = new double[n];
Arrays.fill(ds, value);
return ds;
}
+ /**
+ * @param n Dimension.
+ * @param value Value.
+ * @param jitter Random noise to add to {@code value}.
+ */
+ static double[] point(int n,
+ double value,
+ double jitter) {
+ final ContinuousUniformSampler s = new ContinuousUniformSampler(rng(),
+ -jitter,
+ jitter);
+ final double[] ds = new double[n];
+ for (int i = 0; i < n; i++) {
+ ds[i] = value + s.sample();
+ }
+ return ds;
+ }
+
/** Creates a RNG instance. */
static UniformRandomProvider rng() {
return RandomSource.create(RandomSource.MWC_256);
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java
index 3b03942..87505d6 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java
@@ -272,7 +272,7 @@ public class SimplexOptimizerMultiDirectionalTest {
doTest(new OptimTestUtils.DiffPow(),
OptimTestUtils.point(DIM, 1.0),
GoalType.MINIMIZE,
- 614,
+ 1000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
1e-14);
}
@@ -280,33 +280,31 @@ public class SimplexOptimizerMultiDirectionalTest {
@Test
public void testSsDiffPow() {
doTest(new OptimTestUtils.SsDiffPow(),
- OptimTestUtils.point(DIM / 2, 1.0),
+ OptimTestUtils.point(DIM / 2, 1.0, 1e-1),
GoalType.MINIMIZE,
- 656,
+ 5000,
new PointValuePair(OptimTestUtils.point(DIM / 2, 0.0), 0.0),
- 1e-15);
+ 1e-3);
}
- @Ignore
@Test
public void testAckley() {
doTest(new OptimTestUtils.Ackley(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
- 587,
- new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
- 0);
+ 7500,
+ new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0d),
+ 1e-11);
}
- @Ignore
@Test
public void testRastrigin() {
doTest(new OptimTestUtils.Rastrigin(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
- 535,
- new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
- 0);
+ 5000,
+ new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0d),
+ 1e-6);
}
/**
@@ -329,7 +327,7 @@ public class SimplexOptimizerMultiDirectionalTest {
new ObjectiveFunction(func),
goal,
new InitialGuess(startPoint),
- new MultiDirectionalSimplex(dim, 0.1));
+ new MultiDirectionalSimplex(dim, 1));
final double dist = MathArrays.distance(expected.getPoint(),
result.getPoint());
Assert.assertEquals(0d, dist, tol);
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java
index 0842296..42d3d99 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java
@@ -270,7 +270,7 @@ public class SimplexOptimizerNelderMeadTest {
doTest(new OptimTestUtils.Rosen(),
OptimTestUtils.point(DIM, 0.1),
GoalType.MINIMIZE,
- 11975,
+ 9078,
new PointValuePair(OptimTestUtils.point(DIM, 1.0), 0.0),
1e-6);
}
@@ -279,18 +279,18 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testEllipse() {
doTest(new OptimTestUtils.Elli(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
- 7184,
+ 12000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
- 1e-14);
+ 1e-6);
}
@Ignore
@Test
public void testElliRotated() {
doTest(new OptimTestUtils.ElliRotated(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
7467,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
@@ -300,9 +300,9 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testCigar() {
doTest(new OptimTestUtils.Cigar(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
- 9160,
+ 7000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
1e-6);
}
@@ -311,7 +311,7 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testTwoAxes() {
doTest(new OptimTestUtils.TwoAxes(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
3451,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
@@ -322,20 +322,19 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testCigTab() {
doTest(new OptimTestUtils.CigTab(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
- 7454,
+ 7000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
- 1e-14);
+ 1e-4);
}
- @Ignore
@Test
public void testSphere() {
doTest(new OptimTestUtils.Sphere(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
- 3881,
+ 3000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
1e-6);
}
@@ -344,9 +343,9 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testTablet() {
doTest(new OptimTestUtils.Tablet(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
- 6639,
+ 10000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
1e-14);
}
@@ -355,42 +354,41 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testDiffPow() {
doTest(new OptimTestUtils.DiffPow(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
- 4105,
+ 7000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
- 1e-14);
+ 1e-6);
}
- @Ignore
@Test
public void testSsDiffPow() {
doTest(new OptimTestUtils.SsDiffPow(),
- OptimTestUtils.point(DIM / 2, 1.0),
+ OptimTestUtils.point(DIM / 2, 1.0, 2e-1),
GoalType.MINIMIZE,
- 3990,
+ 4000,
new PointValuePair(OptimTestUtils.point(DIM / 2, 0.0), 0.0),
- 1e-15);
+ 1e-3);
}
@Ignore
@Test
public void testAckley() {
doTest(new OptimTestUtils.Ackley(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
- 2849,
+ 10000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
- 0);
+ 1e-11);
}
@Ignore
@Test
public void testRastrigin() {
doTest(new OptimTestUtils.Rastrigin(),
- OptimTestUtils.point(DIM, 1.0),
+ OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
- 2166,
+ 10000,
new PointValuePair(OptimTestUtils.point(DIM, 0.0), 0.0),
0);
}
@@ -415,7 +413,7 @@ public class SimplexOptimizerNelderMeadTest {
new ObjectiveFunction(func),
goal,
new InitialGuess(startPoint),
- new NelderMeadSimplex(dim, 0.1));
+ new NelderMeadSimplex(dim, 1));
final double dist = MathArrays.distance(expected.getPoint(),
result.getPoint());
Assert.assertEquals(0d, dist, tol);