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/08/27 16:36:50 UTC

[commons-math] branch master updated: Fix incorrect usage of "ROSENBROCK" test function (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 6f33d5f  Fix incorrect usage of "ROSENBROCK" test function (unit tests).
6f33d5f is described below

commit 6f33d5f0a78f30e6fa609398c677e9c382203e49
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Fri Aug 27 18:35:47 2021 +0200

    Fix incorrect usage of "ROSENBROCK" test function (unit tests).
---
 .../scalar/noderiv/BOBYQAOptimizerTest.java        | 51 +++++++-----
 .../scalar/noderiv/CMAESOptimizerTest.java         | 95 ++++++++++++----------
 2 files changed, 81 insertions(+), 65 deletions(-)

diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java
index 98cc641..b1d45e5 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java
@@ -40,48 +40,53 @@ public class BOBYQAOptimizerTest {
 
     @Test(expected=NumberIsTooLargeException.class)
     public void testInitOutOfBounds() {
-        double[] startPoint = OptimTestUtils.point(DIM, 3);
-        double[][] boundaries = boundaries(DIM, -1, 2);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, boundaries,
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 3);
+        double[][] boundaries = boundaries(dim, -1, 2);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, boundaries,
                 GoalType.MINIMIZE,
                 1e-13, 1e-6, 2000, null);
     }
 
     @Test(expected=DimensionMismatchException.class)
     public void testBoundariesDimensionMismatch() {
-        double[] startPoint = OptimTestUtils.point(DIM, 0.5);
-        double[][] boundaries = boundaries(DIM + 1, -1, 2);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, boundaries,
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.5);
+        double[][] boundaries = boundaries(dim + 1, -1, 2);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, boundaries,
                GoalType.MINIMIZE,
                1e-13, 1e-6, 2000, null);
     }
 
     @Test(expected=NumberIsTooSmallException.class)
     public void testProblemDimensionTooSmall() {
+        final int dim = 12;
         double[] startPoint = OptimTestUtils.point(1, 0.5);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, null,
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, null,
                GoalType.MINIMIZE,
                1e-13, 1e-6, 2000, null);
     }
 
     @Test(expected=TooManyEvaluationsException.class)
     public void testMaxEvaluations() {
+        final int dim = 12;
         final int lowMaxEval = 2;
-        double[] startPoint = OptimTestUtils.point(DIM, 0.1);
+        double[] startPoint = OptimTestUtils.point(dim, 0.1);
         double[][] boundaries = null;
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, boundaries,
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, boundaries,
                GoalType.MINIMIZE,
                1e-13, 1e-6, lowMaxEval, null);
      }
 
     @Test
     public void testRosen() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.1);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.1);
         double[][] boundaries = null;
-        PointValuePair expected = new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, boundaries,
+        PointValuePair expected = new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, boundaries,
                 GoalType.MINIMIZE,
-                1e-13, 1e-6, 2000, expected);
+                1e-13, 1e-6, 3000, expected);
      }
 
     @Test
@@ -213,14 +218,15 @@ public class BOBYQAOptimizerTest {
 
     @Test
     public void testConstrainedRosen() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.1);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.1);
 
-        double[][] boundaries = boundaries(DIM,-1,2);
+        double[][] boundaries = boundaries(dim, -1, 2);
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, boundaries,
                 GoalType.MINIMIZE,
-                1e-13, 1e-6, 2000, expected);
+                1e-13, 1e-6, 3000, expected);
     }
 
     // See MATH-728
@@ -228,9 +234,10 @@ public class BOBYQAOptimizerTest {
     //       makes it run for several hours before completing
     @Ignore @Test
     public void testConstrainedRosenWithMoreInterpolationPoints() {
-        final double[] startPoint = OptimTestUtils.point(DIM, 0.1);
-        final double[][] boundaries = boundaries(DIM, -1, 2);
-        final PointValuePair expected = new PointValuePair(OptimTestUtils.point(DIM, 1.0), 0.0);
+        final int dim = 12;
+        final double[] startPoint = OptimTestUtils.point(dim, 0.1);
+        final double[][] boundaries = boundaries(dim, -1, 2);
+        final PointValuePair expected = new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
 
         // This should have been 78 because in the code the hard limit is
         // said to be
@@ -240,7 +247,7 @@ public class BOBYQAOptimizerTest {
         final int maxAdditionalPoints = 47;
 
         for (int num = 1; num <= maxAdditionalPoints; num++) {
-            doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, boundaries,
+            doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, boundaries,
                    GoalType.MINIMIZE,
                    1e-12, 1e-6, 2000,
                    num,
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/CMAESOptimizerTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/CMAESOptimizerTest.java
index 1eab96f..c0f356b 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/CMAESOptimizerTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/CMAESOptimizerTest.java
@@ -49,71 +49,77 @@ public class CMAESOptimizerTest {
 
     @Test(expected = NumberIsTooLargeException.class)
     public void testInitOutofbounds1() {
-        double[] startPoint = OptimTestUtils.point(DIM,3);
-        double[] insigma = OptimTestUtils.point(DIM, 0.3);
-        double[][] boundaries = boundaries(DIM,-1,2);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 3);
+        double[] insigma = OptimTestUtils.point(dim, 0.3);
+        double[][] boundaries = boundaries(dim, -1, 2);
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
     @Test(expected = NumberIsTooSmallException.class)
     public void testInitOutofbounds2() {
-        double[] startPoint = OptimTestUtils.point(DIM, -2);
-        double[] insigma = OptimTestUtils.point(DIM, 0.3);
-        double[][] boundaries = boundaries(DIM,-1,2);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, -2);
+        double[] insigma = OptimTestUtils.point(dim, 0.3);
+        double[][] boundaries = boundaries(dim, -1, 2);
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
 
     @Test(expected = DimensionMismatchException.class)
     public void testBoundariesDimensionMismatch() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.5);
-        double[] insigma = OptimTestUtils.point(DIM, 0.3);
-        double[][] boundaries = boundaries(DIM+1,-1,2);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.5);
+        double[] insigma = OptimTestUtils.point(dim, 0.3);
+        double[][] boundaries = boundaries(dim + 1,-1,2);
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
 
     @Test(expected = NotPositiveException.class)
     public void testInputSigmaNegative() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.5);
-        double[] insigma = OptimTestUtils.point(DIM,-0.5);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.5);
+        double[] insigma = OptimTestUtils.point(dim, -0.5);
         double[][] boundaries = null;
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
 
     @Test(expected = OutOfRangeException.class)
     public void testInputSigmaOutOfRange() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.5);
-        double[] insigma = OptimTestUtils.point(DIM, 1.1);
-        double[][] boundaries = boundaries(DIM,-0.5,0.5);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.5);
+        double[] insigma = OptimTestUtils.point(dim, 1.1);
+        double[][] boundaries = boundaries(dim, -0.5,0.5);
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
 
     @Test(expected = DimensionMismatchException.class)
     public void testInputSigmaDimensionMismatch() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.5);
-        double[] insigma = OptimTestUtils.point(DIM + 1, 0.5);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.5);
+        double[] insigma = OptimTestUtils.point(dim + 1, 0.5);
         double[][] boundaries = null;
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
@@ -121,15 +127,16 @@ public class CMAESOptimizerTest {
     @Test
     @Retry(3)
     public void testRosen() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.1);
-        double[] insigma = OptimTestUtils.point(DIM,0.1);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.1);
+        double[] insigma = OptimTestUtils.point(dim, 0.1);
         double[][] boundaries = null;
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
@@ -346,27 +353,29 @@ public class CMAESOptimizerTest {
 
     @Test
     public void testConstrainedRosen() {
-        double[] startPoint = OptimTestUtils.point(DIM, 0.1);
-        double[] insigma = OptimTestUtils.point(DIM, 0.1);
-        double[][] boundaries = boundaries(DIM, -1, 2);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.1);
+        double[] insigma = OptimTestUtils.point(dim, 0.1);
+        double[][] boundaries = boundaries(dim, -1, 2);
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13,
                 1e-13, 1e-6, 100000, expected);
     }
 
     @Test
     public void testDiagonalRosen() {
-        double[] startPoint = OptimTestUtils.point(DIM,0.1);
-        double[] insigma = OptimTestUtils.point(DIM,0.1);
+        final int dim = 12;
+        double[] startPoint = OptimTestUtils.point(dim, 0.1);
+        double[] insigma = OptimTestUtils.point(dim, 0.1);
         double[][] boundaries = null;
         PointValuePair expected =
-            new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
-        doTest(TestFunction.ROSENBROCK.withDimension(DIM), startPoint, insigma, boundaries,
+            new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0);
+        doTest(TestFunction.ROSENBROCK.withDimension(dim), startPoint, insigma, boundaries,
                 GoalType.MINIMIZE, LAMBDA, false, 1, 1e-13,
                 1e-10, 1e-4, 1000000, expected);
      }