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/12 00:08:25 UTC

[commons-math] branch master updated (9b7a2c8 -> 9371883)

This is an automated email from the ASF dual-hosted git repository.

erans pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git.


    from 9b7a2c8  Delete spurious files.
     new 6a7b4cc  Drop repeated tests inside "SimplexOptimizerTest.Task" class.
     new f29ebd2  More test functions.
     new 59e9604  Consistency check to ensure that "TestFunction" implementations are correct.
     new 9371883  Increase number of allowed failures (unit tests).

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 commons-math-legacy/pom.xml                        |   2 +-
 .../optim/nonlinear/scalar/TestFunction.java       |  96 ++++++++
 .../scalar/noderiv/SimplexOptimizerTest.java       | 243 ++++++++++++++-------
 .../std_test_func.simplex.hedar_fukushima.csv      |  16 +-
 .../std_test_func.simplex.multidirectional.csv     |  40 ++--
 .../noderiv/std_test_func.simplex.nelder_mead.csv  |  40 ++--
 6 files changed, 328 insertions(+), 109 deletions(-)

[commons-math] 01/04: Drop repeated tests inside "SimplexOptimizerTest.Task" class.

Posted by er...@apache.org.
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

commit 6a7b4ccbe3a3ed39454cfa39df084e54705bcd1a
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Wed Aug 11 19:58:28 2021 +0200

    Drop repeated tests inside "SimplexOptimizerTest.Task" class.
    
    Explicitly specify the initial simplex side (as test parameter).
---
 .../scalar/noderiv/SimplexOptimizerTest.java       | 126 +++++++++------------
 1 file changed, 55 insertions(+), 71 deletions(-)

diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java
index d748508..25dd7c8 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java
@@ -17,7 +17,6 @@
 package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
 
 import java.util.Arrays;
-import org.opentest4j.AssertionFailedError;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ParameterContext;
@@ -83,6 +82,7 @@ public class SimplexOptimizerTest {
     @ParameterizedTest
     @CsvFileSource(resources = NELDER_MEAD_INPUT_FILE)
     void testFunctionWithNelderMead(@AggregateWith(TaskAggregator.class) Task task) {
+        // task.checkAlongLine(1000, true);
         task.run(new NelderMeadTransform());
     }
 
@@ -106,8 +106,6 @@ public class SimplexOptimizerTest {
         private static final int FUNC_EVAL_DEBUG = 80000;
         /** Default convergence criterion. */
         private static final double CONVERGENCE_CHECK = 1e-9;
-        /** Default simplex size. */
-        private static final double SIDE_LENGTH = 1;
         /** Default cooling factor. */
         private static final double SA_COOL_FACTOR = 0.5;
         /** Default acceptance probability at beginning of SA. */
@@ -115,7 +113,7 @@ public class SimplexOptimizerTest {
         /** Default acceptance probability at end of SA. */
         private static final double SA_END_PROB = 1e-20;
         /** Function. */
-        private final MultivariateFunction f;
+        private final MultivariateFunction function;
         /** Initial value. */
         private final double[] start;
         /** Optimum. */
@@ -124,46 +122,45 @@ public class SimplexOptimizerTest {
         private final double pointTolerance;
         /** Allowed function evaluations. */
         private final int functionEvaluations;
-        /** Repeats on failure. */
-        private final int repeatsOnFailure;
+        /** Side length of initial simplex. */
+        private final double simplexSideLength;
         /** Range of random noise. */
         private final double jitter;
         /** Whether to perform simulated annealing. */
         private final boolean withSA;
 
         /**
-         * @param f Test function.
+         * @param function Test function.
          * @param start Start point.
          * @param optimum Optimum.
          * @param pointTolerance Allowed distance between result and
          * {@code optimum}.
          * @param functionEvaluations Allowed number of function evaluations.
-         * @param repeatsOnFailure Maximum number of times to rerun when an
-         * {@link AssertionFailedError} is thrown.
+         * @param simplexSideLength Side length of initial simplex.
          * @param jitter Size of random jitter.
          * @param withSA Whether to perform simulated annealing.
          */
-        Task(MultivariateFunction f,
+        Task(MultivariateFunction function,
              double[] start,
              double[] optimum,
              double pointTolerance,
              int functionEvaluations,
-             int repeatsOnFailure,
+             double simplexSideLength,
              double jitter,
              boolean withSA) {
-            this.f = f;
+            this.function = function;
             this.start = start;
             this.optimum = optimum;
             this.pointTolerance = pointTolerance;
             this.functionEvaluations = functionEvaluations;
-            this.repeatsOnFailure = repeatsOnFailure;
+            this.simplexSideLength = simplexSideLength;
             this.jitter = jitter;
             this.withSA = withSA;
         }
 
         @Override
         public String toString() {
-            return f.toString();
+            return function.toString();
         }
 
         /**
@@ -176,67 +173,54 @@ public class SimplexOptimizerTest {
             // required by the current code.
             final int maxEval = Math.max(functionEvaluations, FUNC_EVAL_DEBUG);
 
-            int currentRetry = -1;
-            AssertionFailedError lastFailure = null;
-            while (currentRetry++ <= repeatsOnFailure) {
-                try {
-                    final String name = f.toString();
-                    final int dim = start.length;
+            final String name = function.toString();
+            final int dim = start.length;
 
-                    final SimulatedAnnealing sa;
-                    final PopulationSize popSize;
-                    if (withSA) {
-                        final SimulatedAnnealing.CoolingSchedule coolSched =
-                            SimulatedAnnealing.CoolingSchedule.decreasingExponential(SA_COOL_FACTOR);
+            final SimulatedAnnealing sa;
+            final PopulationSize popSize;
+            if (withSA) {
+                final SimulatedAnnealing.CoolingSchedule coolSched =
+                    SimulatedAnnealing.CoolingSchedule.decreasingExponential(SA_COOL_FACTOR);
 
-                        sa = new SimulatedAnnealing(dim,
-                                                    SA_START_PROB,
-                                                    SA_END_PROB,
-                                                    coolSched,
-                                                    RandomSource.KISS.create());
+                sa = new SimulatedAnnealing(dim,
+                                            SA_START_PROB,
+                                            SA_END_PROB,
+                                            coolSched,
+                                            RandomSource.KISS.create());
 
-                        popSize = new PopulationSize(dim);
-                    } else {
-                        sa = null;
-                        popSize = null;
-                    }
-
-                    final SimplexOptimizer optim = new SimplexOptimizer(-1, CONVERGENCE_CHECK);
-                    final Simplex initialSimplex =
-                        Simplex.alongAxes(OptimTestUtils.point(dim,
-                                                               SIDE_LENGTH,
-                                                               jitter));
-                    final double[] startPoint = OptimTestUtils.point(start, jitter);
-                    final PointValuePair result =
-                        optim.optimize(new MaxEval(maxEval),
-                                       new ObjectiveFunction(f),
-                                       GoalType.MINIMIZE,
-                                       new InitialGuess(startPoint),
-                                       initialSimplex,
-                                       factory,
-                                       sa,
-                                       popSize);
+                popSize = new PopulationSize(dim);
+            } else {
+                sa = null;
+                popSize = null;
+            }
 
-                    final double[] endPoint = result.getPoint();
-                    final double funcValue = result.getValue();
-                    final double dist = MathArrays.distance(optimum, endPoint);
-                    Assertions.assertEquals(0d, dist, pointTolerance,
-                                            name + ": distance to optimum" +
-                                            " f(" + Arrays.toString(endPoint) + ")=" +
-                                            funcValue);
+            final SimplexOptimizer optim = new SimplexOptimizer(-1, CONVERGENCE_CHECK);
+            final Simplex initialSimplex =
+                Simplex.alongAxes(OptimTestUtils.point(dim,
+                                                       simplexSideLength,
+                                                       jitter));
+            final double[] startPoint = OptimTestUtils.point(start, jitter);
+            final PointValuePair result =
+                optim.optimize(new MaxEval(maxEval),
+                               new ObjectiveFunction(function),
+                               GoalType.MINIMIZE,
+                               new InitialGuess(startPoint),
+                               initialSimplex,
+                               factory,
+                               sa,
+                               popSize);
 
-                    final int nEval = optim.getEvaluations();
-                    Assertions.assertTrue(nEval < functionEvaluations,
-                                          name + ": nEval=" + nEval);
+            final double[] endPoint = result.getPoint();
+            final double funcValue = result.getValue();
+            final double dist = MathArrays.distance(optimum, endPoint);
+            Assertions.assertEquals(0d, dist, pointTolerance,
+                                    name + ": distance to optimum" +
+                                    " f(" + Arrays.toString(endPoint) + ")=" +
+                                    funcValue);
 
-                    break; // Assertions passed: Retry not neccessary.
-                } catch (AssertionFailedError e) {
-                    if (currentRetry >= repeatsOnFailure) {
-                        // Allowed repeats have been exhausted: Bail out.
-                        throw e;
-                    }
-                }
-            }
+            final int nEval = optim.getEvaluations();
+            Assertions.assertTrue(nEval < functionEvaluations,
+                                  name + ": nEval=" + nEval);
         }
     }
 
@@ -257,7 +241,7 @@ public class SimplexOptimizerTest {
             final double[] optimum = toArrayOfDoubles(a.getString(index++), dim);
             final double pointTol = a.getDouble(index++);
             final int funcEval = a.getInteger(index++);
-            final int repeat = a.getInteger(index++);
+            final double sideLength = a.getDouble(index++);
             final double jitter = a.getDouble(index++);
             final boolean withSA = a.getBoolean(index++);
 
@@ -266,7 +250,7 @@ public class SimplexOptimizerTest {
                             optimum,
                             pointTol,
                             funcEval,
-                            repeat,
+                            sideLength,
                             jitter,
                             withSA);
         }

[commons-math] 02/04: More test functions.

Posted by er...@apache.org.
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

commit f29ebd2e130405fe611c653243ac1124a0fed5f8
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Thu Aug 12 01:56:44 2021 +0200

    More test functions.
---
 .../optim/nonlinear/scalar/TestFunction.java       | 96 ++++++++++++++++++++++
 .../std_test_func.simplex.hedar_fukushima.csv      | 16 +++-
 .../std_test_func.simplex.multidirectional.csv     | 40 +++++----
 .../noderiv/std_test_func.simplex.nelder_mead.csv  | 40 +++++----
 4 files changed, 159 insertions(+), 33 deletions(-)

diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/TestFunction.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/TestFunction.java
index 798ada1..216be58 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/TestFunction.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/TestFunction.java
@@ -17,6 +17,7 @@
 package org.apache.commons.math4.legacy.optim.nonlinear.scalar;
 
 import java.util.function.Function;
+import java.util.function.DoubleUnaryOperator;
 import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
 import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath;
 
@@ -179,6 +180,101 @@ public enum TestFunction {
                 }
                 return f;
             };
+        }),
+    // https://www.sfu.ca/~ssurjano/griewank.html
+    GRIEWANK(dim -> {
+            final double A = 4000;
+            return x -> {
+                double sum = 0;
+                double prod = 1;
+                for (int i = 0; i < dim; i++) {
+                    final double xi = x[i];
+                    sum += xi * xi;
+                    prod *= Math.cos(xi / Math.sqrt(i + 1));
+                }
+                return sum / A - prod + 1;
+            };
+        }),
+    // https://www.sfu.ca/~ssurjano/levy.html
+    LEVY(dim -> {
+            final int last = dim - 1;
+            final DoubleUnaryOperator w = x -> 1 + 0.25 * (x - 1);
+            return x -> {
+                final double a0 = Math.sin(Math.PI * w.applyAsDouble(x[0]));
+                double sum = a0 * a0;
+                for (int i = 0; i < last; i++) {
+                    final double wi = w.applyAsDouble(x[i]);
+                    final double wiM1 = wi - 1;
+                    final double ai = Math.sin(Math.PI * wi + 1);
+                    sum += wiM1 * wiM1 * (1 + 10 * ai * ai);
+                }
+                final double wl = w.applyAsDouble(x[last]);
+                final double wlM1 = wl - 1;
+                final double al = Math.sin(2 * Math.PI * wl);
+                return sum + wlM1 * wlM1 * (1 + al * al);
+            };
+        }),
+    // https://www.sfu.ca/~ssurjano/schwef.html
+    SCHWEFEL(dim -> {
+            final double A = 418.9829;
+            return x -> {
+                double sum = 0;
+                for (int i = 0; i < dim; i++) {
+                    final double xi = x[i];
+                    sum += xi * Math.sin(Math.sqrt(Math.abs(xi)));
+                }
+                return A * dim - sum;
+            };
+        }),
+    // https://www.sfu.ca/~ssurjano/zakharov.html
+    ZAKHAROV(dim -> {
+            final double A = 0.5;
+            return x -> {
+                double sum1 = 0;
+                double sum2 = 0;
+                for (int i = 0; i < dim; i++) {
+                    final double xi = x[i];
+                    sum1 += xi * xi;
+                    sum2 += A * (i + 1) * xi;
+                }
+                final double sum22 = sum2 * sum2;
+                return sum1 + sum22 + sum22 * sum22;
+            };
+        }),
+    // https://www.sfu.ca/~ssurjano/permdb.html
+    PERM(dim -> {
+            final double BETA = 10;
+            return x -> {
+                double sum1 = 0;
+                for (int i = 0; i < dim; i++) {
+                    final double iP1 = i + 1;
+                    double sum2 = 0;
+                    for (int j = 0; j < dim; j++) {
+                        final double jP1 = j + 1;
+                        final double a = Math.pow(jP1, iP1) + BETA;
+                        final double b = Math.pow(x[j] / jP1, iP1) - 1;
+                        sum2 += a * b;
+                    }
+                    sum1 += sum2 * sum2;
+                }
+                return sum1;
+            };
+        }),
+    // https://www.sfu.ca/~ssurjano/stybtang.html
+    STYBLINSKI_TANG(dim -> {
+            final double A = 0.5;
+            final double B = 16;
+            final double C = 5;
+            return x -> {
+                double sum = 0;
+                for (int i = 0; i < dim; i++) {
+                    final double xi = x[i];
+                    final double xi2 = xi * xi;
+                    final double xi4 = xi2 * xi2;
+                    sum += xi4 - B * xi2 + C * xi;
+                }
+                return A * sum;
+            };
         });
 
     /** Template for variable dimension. */
diff --git a/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.hedar_fukushima.csv b/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.hedar_fukushima.csv
index e867df5..d3ba7dc 100644
--- a/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.hedar_fukushima.csv
+++ b/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.hedar_fukushima.csv
@@ -23,15 +23,16 @@
 #  3: optimum ("n" space-separated values)
 #  4: maximum expected distance from the result to the optimum
 #  5: expected number of function evaluations
-#  6: number of retries in case of assertion failure
+#  6: length of the sides of the initial simplex
 #  7: size of the random noise (to generate slightly different initial conditions)
 #  8: whether to perform simulated annealing
 #
 # Caveat: Some tests are commented out (cf. JIRA: MATH-1552).
 #
 PARABOLA, 4, 2.5 3.1 4.6 5.8, 0 0 0 0, 1e-4, 820, 3, 2e-1, true
-ROSENBROCK, 2, -1.2 1, 1 1, 1e-4, 360, 3, 1e-1, true
-ROSENBROCK, 10, -0.1 0.1 0.2 -0.1 -0.2 0.3 0.2 -0.1 0.2 -0.3, 1 1 1 1 1 1 1 1 1 1, 5e-5, 30000, 3, 5e-2, true
+ROSENBROCK, 2, -1.2 1, 1 1, 1e-4, 380, 3, 1e-1, true
+ROSENBROCK, 5, -4.4 -3.5 -2.6 -1.7 -0.8, 1 1 1 1 1, 1e-4, 3800, 5e-1, 1e-1, true
+ROSENBROCK, 10, -0.1 0.1 0.2 -0.1 -0.2 0.3 0.2 -0.1 0.2 -0.3, 1 1 1 1 1 1 1 1 1 1, 5e-5, 38000, 3, 5e-2, true
 POWELL, 4, 3 -1 -2 1, 0 0 0 0, 5e-3, 1100, 3, 1e-1, true
 CIGAR, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-5, 75000, 3, 1e-1, true
 SPHERE, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-4, 47000, 3, 1e-1, true
@@ -43,3 +44,12 @@ TABLET, 11, 2 3 4 -3 -2 -1 2 3 4 3 -1, 0 0 0 0 0 0 0 0 0 0 0, 2e-4, 41000, 3, 1e
 SS_DIFF_POW, 6, -3.2 2.1 1.2 -2.3 3.2 -2.1, 0 0 0 0 0 0, 1e-3, 10500, 3, 1e-1, true
 #ACKLEY, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 1100, 3, 5e-1, true
 #RASTRIGIN, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 10000, 3, 5e-1, true
+#GRIEWANK, 3, -210 123 -456, 0 0 0, 1e-1, 1000, 50, 100, true
+#LEVY, 4, 4 -6 -2 8, 1 1 1 1, 1e-3, 3000, 1, 1, true
+#SCHWEFEL, 2, 100 -200, 420.9687 420.9687, 1, 300, 100, 100, true
+ZAKHAROV, 5, -4 -2 3 5 7, 0 0 0 0 0, 1e-4, 2200, 1.5, 1, true
+PERM, 2, -2 -1, 1 2, 2e-3, 210, 5e-1, 1e-1, true
+#PERM, 3, -2 -3 -1, 1 2 3, 5e-5, 200, 5e-1, 1e-1, true
+#PERM, 4, -2 -3 -4 -1, 1 2 3 4, 5e-4, 2200, 5e-1, 1e-1, true
+#PERM, 5, -2 -3 -4 -5 -1, 1 2 3 4 5, 5e-4, 200, 5e-1, 1e-1, true
+#STYBLINSKI_TANG, 4, 1 2 3 4, -2.903534 -2.903534 -2.903534 -2.903534, 1e-4, 500, 1, 5e-1, true
diff --git a/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.multidirectional.csv b/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.multidirectional.csv
index df135fc..4be1bee 100644
--- a/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.multidirectional.csv
+++ b/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.multidirectional.csv
@@ -23,23 +23,33 @@
 #  3: optimum ("n" space-separated values)
 #  4: maximum expected distance from the result to the optimum
 #  5: expected number of function evaluations
-#  6: number of retries in case of assertion failure
+#  6: length of the sides of the initial simplex
 #  7: size of the random noise (to generate slightly different initial conditions)
 #  8: whether to perform simulated annealing
 #
 # Caveat: Some tests are commented out (cf. JIRA: MATH-1552).
 #
-PARABOLA, 4, 2.5 3.1 4.6 5.8, 0 0 0 0, 1e-4, 340, 3, 2e-1, false
-ROSENBROCK, 2, -1.2 1, 1 1, 2e-3, 9200, 3, 1e-1, false
-ROSENBROCK, 10, -0.1 0.1 0.2 -0.1 -0.2 0.3 0.2 -0.1 0.2 -0.3, 1 1 1 1 1 1 1 1 1 1, 3e-3, 100000, 3, 5e-2, false
-#POWELL, 4, 3 -1 -2 1, 0 0 0 0, 5e-3, 420, 3, 1e-1, false
-CIGAR, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 1e-6, 7000, 3, 1e-1, false
-SPHERE, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-5, 3600, 3, 1e-1, false
-ELLI, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 50000, 3, 1e-1, false
-TWO_AXES, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-6, 3200, 3, 1e-1, false
-CIG_TAB, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 5e-6, 2700, 3, 1e-1, false
-TABLET, 11, 2 3 4 -3 -2 -1 2 3 4 3 -1, 0 0 0 0 0 0 0 0 0 0 0, 5e-6, 3000, 3, 1e-1, false
-DIFF_POW, 7, 1 -1 1 -1 1 -1 1, 0 0 0 0 0 0 0, 5e-4, 2500, 3, 1e-1, false
-SS_DIFF_POW, 6, -3.2 2.1 1.2 -2.3 3.2 -2.1, 0 0 0 0 0 0, 1e-3, 4000, 3, 1e-1, false
-ACKLEY, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 700, 3, 5e-1, false
-#RASTRIGIN, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 10000, 3, 5e-1, false
+PARABOLA, 4, 2.5 3.1 4.6 5.8, 0 0 0 0, 1e-4, 380, 1, 2e-1, false
+ROSENBROCK, 2, -1.2 1, 1 1, 2e-3, 11100, 1, 1e-1, false
+#ROSENBROCK, 5, -4.4 -3.5 -2.6 -1.7 -0.8, 1 1 1 1 1, 1e-4, 180, 5e-1, 1e-1, false
+ROSENBROCK, 10, -0.1 0.1 0.2 -0.1 -0.2 0.3 0.2 -0.1 0.2 -0.3, 1 1 1 1 1 1 1 1 1 1, 3e-3, 100000, 1, 5e-2, false
+#POWELL, 4, 3 -1 -2 1, 0 0 0 0, 5e-3, 420, 1, 1e-1, false
+CIGAR, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-6, 7000, 1, 1e-1, false
+SPHERE, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-5, 3600, 1, 1e-1, false
+ELLI, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 50000, 1, 1e-1, false
+TWO_AXES, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-6, 3200, 1, 1e-1, false
+CIG_TAB, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 5e-6, 2900, 1, 1e-1, false
+TABLET, 11, 2 3 4 -3 -2 -1 2 3 4 3 -1, 0 0 0 0 0 0 0 0 0 0 0, 5e-6, 3200, 1, 1e-1, false
+DIFF_POW, 7, 1 -1 1 -1 1 -1 1, 0 0 0 0 0 0 0, 5e-4, 2500, 1, 1e-1, false
+SS_DIFF_POW, 6, -3.2 2.1 1.2 -2.3 3.2 -2.1, 0 0 0 0 0 0, 1e-3, 4000, 1, 1e-1, false
+ACKLEY, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 770, 1, 5e-1, false
+#RASTRIGIN, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 10000, 1, 5e-1, false
+#GRIEWANK, 3, -210 123 -456, 0 0 0, 1e-1, 1000, 50, 100, false
+#LEVY, 4, 4 -6 -2 8, 1 1 1 1, 1e-3, 3000, 1, 1, false
+#SCHWEFEL, 2, 100 -200, 420.9687 420.9687, 1, 300, 100, 100, false
+ZAKHAROV, 5, -4 -2 3 5 7, 0 0 0 0 0, 1e-4, 3100, 1.5, 1, false
+PERM, 2, -2 -1, 1 2, 3e-2, 25000, 5e-1, 1e-1, false
+#PERM, 3, -2 -3 -1, 1 2 3, 5e-5, 200, 5e-1, 1e-1, false
+#PERM, 4, -2 -3 -4 -1, 1 2 3 4, 5e-4, 2200, 5e-1, 1e-1, false
+#PERM, 5, -2 -3 -4 -5 -1, 1 2 3 4 5, 5e-4, 200, 5e-1, 1e-1, false
+#STYBLINSKI_TANG, 4, 1 2 3 4, -2.903534 -2.903534 -2.903534 -2.903534, 1e-4, 500, 1, 5e-1, false
diff --git a/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.nelder_mead.csv b/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.nelder_mead.csv
index ca7d585..49db4d8 100644
--- a/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.nelder_mead.csv
+++ b/commons-math-legacy/src/test/resources/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/std_test_func.simplex.nelder_mead.csv
@@ -23,23 +23,33 @@
 #  3: optimum ("n" space-separated values)
 #  4: maximum expected distance from the result to the optimum
 #  5: expected number of function evaluations
-#  6: number of retries in case of assertion failure
+#  6: length of the sides of the initial simplex
 #  7: size of the random noise (to generate slightly different initial conditions)
 #  8: whether to perform simulated annealing
 #
 # Caveat: Some tests are commented out (cf. JIRA: MATH-1552).
 #
-PARABOLA, 4, 2.5 3.1 4.6 5.8, 0 0 0 0, 1e-4, 200, 3, 2e-1, false
-ROSENBROCK, 2, -1.2 1, 1 1, 1e-4, 180, 3, 1e-1, false
-ROSENBROCK, 10, -0.1 0.1 0.2 -0.1 -0.2 0.3 0.2 -0.1 0.2 -0.3, 1 1 1 1 1 1 1 1 1 1, 5e-5, 9000, 3, 5e-2, false
-POWELL, 4, 3 -1 -2 1, 0 0 0 0, 5e-3, 420, 3, 1e-1, false
-CIGAR, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-5, 7000, 3, 1e-1, false
-SPHERE, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-4, 3000, 3, 1e-1, false
-ELLI, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 50000, 3, 1e-1, false
-#TWO_AXES, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 5000, 3, 1e-1, false
-#CIG_TAB, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 7000, 3, 1e-1, false
-TABLET, 11, 2 3 4 -3 -2 -1 2 3 4 3 -1, 0 0 0 0 0 0 0 0 0 0 0, 2e-4, 3100, 3, 1e-1, false
-#DIFF_POW, 7, 1 -1 1 -1 1 -1 1, 0 0 0 0 0 0 0, 5e-4, 2500, 3, 1e-1, false
-SS_DIFF_POW, 6, -3.2 2.1 1.2 -2.3 3.2 -2.1, 0 0 0 0 0 0, 1e-3, 4000, 3, 1e-1, false
-ACKLEY, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 355, 3, 5e-1, false
-#RASTRIGIN, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 10000, 3, 5e-1, false
+PARABOLA, 4, 2.5 3.1 4.6 5.8, 0 0 0 0, 1e-4, 200, 1, 2e-1, false
+ROSENBROCK, 2, -1.2 1, 1 1, 1e-4, 180, 1, 1e-1, false
+ROSENBROCK, 5, -4.4 -3.5 -2.6 -1.7 -0.8, 1 1 1 1 1, 1e-4, 800, 5e-1, 1e-1, false
+ROSENBROCK, 10, -0.1 0.1 0.2 -0.1 -0.2 0.3 0.2 -0.1 0.2 -0.3, 1 1 1 1 1 1 1 1 1 1, 5e-5, 9000, 1, 5e-2, false
+POWELL, 4, 3 -1 -2 1, 0 0 0 0, 5e-3, 420, 1, 1e-1, false
+CIGAR, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-5, 7000, 1, 1e-1, false
+SPHERE, 13, -1.2 2.3 -3.2 2.1 1.2 -2.3 3.2 -2.1 -1.2 2.3 -3.2 2.1 -1.2, 0 0 0 0 0 0 0 0 0 0 0 0 0, 5e-4, 3000, 1, 1e-1, false
+ELLI, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 50000, 1, 1e-1, false
+#TWO_AXES, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 5000, 1, 1e-1, false
+#CIG_TAB, 10, 2 3 4 -3 -2 -1 2 3 4 3, 0 0 0 0 0 0 0 0 0 0, 1e-4, 7000, 1, 1e-1, false
+TABLET, 11, 2 3 4 -3 -2 -1 2 3 4 3 -1, 0 0 0 0 0 0 0 0 0 0 0, 2e-4, 3600, 1, 1e-1, false
+#DIFF_POW, 7, 1 -1 1 -1 1 -1 1, 0 0 0 0 0 0 0, 5e-4, 2500, 1, 1e-1, false
+SS_DIFF_POW, 6, -3.2 2.1 1.2 -2.3 3.2 -2.1, 0 0 0 0 0 0, 1e-3, 4000, 1, 1e-1, false
+ACKLEY, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 430, 1, 5e-1, false
+#RASTRIGIN, 4, 3 4 -3 -2, 0 0 0 0, 1e-6, 10000, 1, 5e-1, false
+#GRIEWANK, 3, -210 123 -456, 0 0 0, 1e-1, 1000, 50, 100, false
+#LEVY, 4, 4 -6 -2 8, 1 1 1 1, 1e-3, 3000, 1, 1, false
+#SCHWEFEL, 2, 100 -200, 420.9687 420.9687, 1, 300, 100, 100, false
+ZAKHAROV, 5, -4 -2 3 5 7, 0 0 0 0 0, 5e-5, 500, 1.5, 1, false
+PERM, 2, -2 -1, 1 2, 5e-5, 200, 5e-1, 1e-1, false
+#PERM, 3, -2 -3 -1, 1 2 3, 5e-5, 200, 5e-1, 1e-1, false
+PERM, 4, -2 -3 -4 -1, 1 2 3 4, 5e-3, 2800, 5e-1, 1e-1, false
+#PERM, 5, -2 -3 -4 -5 -1, 1 2 3 4 5, 5e-4, 200, 5e-1, 1e-1, false
+#STYBLINSKI_TANG, 4, 1 2 3 4, -2.903534 -2.903534 -2.903534 -2.903534, 1e-4, 500, 1, 5e-1, false

[commons-math] 03/04: Consistency check to ensure that "TestFunction" implementations are correct.

Posted by er...@apache.org.
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

commit 59e9604dd810c3323980a6f11fee5a3b4a5366d8
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Thu Aug 12 01:59:37 2021 +0200

    Consistency check to ensure that "TestFunction" implementations are correct.
    
    Call is commented out (it is mostly intended for a one-time visual check).
---
 .../scalar/noderiv/SimplexOptimizerTest.java       | 109 +++++++++++++++++++++
 1 file changed, 109 insertions(+)

diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java
index 25dd7c8..732ba42 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java
@@ -17,6 +17,10 @@
 package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
 
 import java.util.Arrays;
+import java.io.PrintWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ParameterContext;
@@ -222,6 +226,111 @@ public class SimplexOptimizerTest {
             Assertions.assertTrue(nEval < functionEvaluations,
                                   name + ": nEval=" + nEval);
         }
+
+        /**
+         * Asserts that the lowest function value (along a line starting at
+         * {@link #start} is reached at the {@link #optimum}.
+         *
+         * @param numPoints Number of points at which to evaluate the function.
+         * @param plot Whether to generate a file (for visual debugging).
+         */
+        public void checkAlongLine(int numPoints,
+                                   boolean plot) {
+            if (plot) {
+                final String name = createPlotBasename(function, start, optimum);
+                try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Paths.get(name)))) {
+                    checkAlongLine(numPoints, out);
+                } catch (IOException e) {
+                    Assertions.fail(e.getMessage());
+                }
+            } else {
+                checkAlongLine(numPoints, null);
+            }
+        }
+
+        /**
+         * Computes the values of the function along the straight line between
+         * {@link #startPoint} and {@link #optimum} and asserts that the value
+         * at the latter is smaller than at any other points along the line.
+         * <p>
+         * If the {@code output} stream is not {@code null}, two columns are
+         * printed:
+         * <ol>
+         *  <li>parameter in the {@code [0, 1]} interval (0 at {@link #startPoint}
+         *   and 1 at {@link #optimum}),</li>
+         *  <li>function value at {@code t * (optimum - startPoint)}.</li>
+         * </ol>
+         *
+         * @param numPoints Number of points to evaluate between {@link #start}
+         * and {@link #optimum}.
+         * @param output Output stream.
+         */
+        private void checkAlongLine(int numPoints,
+                                    PrintWriter output) {
+            final double delta = 1d / numPoints;
+
+            final int dim = start.length;
+            final double[] dir = new double[dim];
+            for (int i = 0; i < dim; i++) {
+                dir[i] = optimum[i] - start[i];
+            }
+
+            double[] minPoint = null;
+            double minValue = Double.POSITIVE_INFINITY;
+            int count = 0;
+            while (count <= numPoints) {
+                final double[] p = new double[dim];
+                final double t = count * delta;
+                for (int i = 0; i < dim; i++) {
+                    p[i] = start[i] + t * dir[i];
+                }
+
+                final double value = function.value(p);
+                if (value <= minValue) {
+                    minValue = value;
+                    minPoint = p;
+                }
+
+                if (output != null) {
+                    output.println(t + " " + value);
+                }
+
+                ++count;
+            }
+
+            final double tol = 1e-15;
+            Assertions.assertArrayEquals(optimum, minPoint, tol,
+                                         "Minimum: f(" + Arrays.toString(minPoint) + ")=" + minValue);
+        }
+
+        /**
+         * Generates a string suitable as a file name.
+         * <p>
+         * Brackets are removed; space, slash, "=" sign and comma
+         * characters are converted to underscores.
+         *
+         * @param f Function.
+         * @param start Start point.
+         * @param end End point.
+         * @return a string.
+         */
+        private static String createPlotBasename(MultivariateFunction f,
+                                                 double[] start,
+                                                 double[] end) {
+            final String s = f.toString() + "__" +
+                Arrays.toString(start) + "__" +
+                Arrays.toString(end) + ".dat";
+
+            final String repl = "_";
+            return s
+                .replaceAll("\\[", "")
+                .replaceAll("\\]", "")
+                .replaceAll("=", repl)
+                .replaceAll(",\\s+", repl)
+                .replaceAll(",", repl)
+                .replaceAll("\\s", repl)
+                .replaceAll("/", repl);
+        }
     }
 
     /**

[commons-math] 04/04: Increase number of allowed failures (unit tests).

Posted by er...@apache.org.
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

commit 9371883b2bc72c328b66f6ed4982b88682e8ee5c
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Thu Aug 12 02:04:15 2021 +0200

    Increase number of allowed failures (unit tests).
---
 commons-math-legacy/pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commons-math-legacy/pom.xml b/commons-math-legacy/pom.xml
index 345d18f..8f86c23 100644
--- a/commons-math-legacy/pom.xml
+++ b/commons-math-legacy/pom.xml
@@ -132,7 +132,7 @@
       <plugin>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
-          <rerunFailingTestsCount>5</rerunFailingTestsCount>
+          <rerunFailingTestsCount>9</rerunFailingTestsCount>
         </configuration>
       </plugin>
       <!-- Ignore spotbugs in the legacy module.