commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1413088 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.java test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.java
Date Fri, 23 Nov 2012 22:10:56 GMT
Author: erans
Date: Fri Nov 23 22:10:55 2012
New Revision: 1413088

URL: http://svn.apache.org/viewvc?rev=1413088&view=rev
Log:
MATH-908
Added constructor to enable independent settings of the line search
tolerances. Updated unit tests.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.java?rev=1413088&r1=1413087&r2=1413088&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.java
Fri Nov 23 22:10:55 2012
@@ -69,7 +69,10 @@ public class PowellOptimizer
     /**
      * This constructor allows to specify a user-defined convergence checker,
      * in addition to the parameters that control the default convergence
-     * checking procedure and the line search tolerances.
+     * checking procedure.
+     * <br/>
+     * The internal line search tolerances are set to the square-root of their
+     * corresponding value in the multivariate optimizer.
      *
      * @param rel Relative threshold.
      * @param abs Absolute threshold.
@@ -80,6 +83,27 @@ public class PowellOptimizer
     public PowellOptimizer(double rel,
                            double abs,
                            ConvergenceChecker<PointValuePair> checker) {
+        this(rel, abs, FastMath.sqrt(rel), FastMath.sqrt(abs), checker);
+    }
+
+    /**
+     * This constructor allows to specify a user-defined convergence checker,
+     * in addition to the parameters that control the default convergence
+     * checking procedure and the line search tolerances.
+     *
+     * @param rel Relative threshold for this optimizer.
+     * @param abs Absolute threshold for this optimizer.
+     * @param lineRel Relative threshold for the internal line search optimizer.
+     * @param lineAbs Absolute threshold for the internal line search optimizer.
+     * @param checker Convergence checker.
+     * @throws NotStrictlyPositiveException if {@code abs <= 0}.
+     * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
+     */
+    public PowellOptimizer(double rel,
+                           double abs,
+                           double lineRel,
+                           double lineAbs,
+                           ConvergenceChecker<PointValuePair> checker) {
         super(checker);
 
         if (rel < MIN_RELATIVE_TOLERANCE) {
@@ -91,15 +115,16 @@ public class PowellOptimizer
         relativeThreshold = rel;
         absoluteThreshold = abs;
 
-        // Line search tolerances can be much less stringent than the tolerances
-        // required for the optimizer itself.
-        line = new LineSearch(FastMath.sqrt(rel),
-                              FastMath.sqrt(abs));
+        // Create the line search optimizer.
+        line = new LineSearch(lineRel,
+                              lineAbs);
     }
 
     /**
-     * The parameters control the default convergence checking procedure, and
-     * the line search tolerances.
+     * The parameters control the default convergence checking procedure.
+     * <br/>
+     * The internal line search tolerances are set to the square-root of their
+     * corresponding value in the multivariate optimizer.
      *
      * @param rel Relative threshold.
      * @param abs Absolute threshold.

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.java?rev=1413088&r1=1413087&r2=1413088&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.java
Fri Nov 23 22:10:55 2012
@@ -46,13 +46,16 @@ public class PowellOptimizerTest {
         for (int i = 0; i < dim; i++) {
             init[i] = minPoint[i];
         }
-        doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-9, 1e-7);
+        doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9);
 
         // Initial is far from minimum.
         for (int i = 0; i < dim; i++) {
             init[i] = minPoint[i] + 3;
         }
-        doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-9, 1e-5);
+        doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-5);
+        // More stringent line search tolerance enhances the precision
+        // of the result.
+        doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9, 1e-7);
     }
 
     @Test
@@ -201,4 +204,34 @@ public class PowellOptimizerTest {
                                 optimum[i], point[i], pointTol);
         }
     }
+
+    /**
+     * @param func Function to optimize.
+     * @param optimum Expected optimum.
+     * @param init Starting point.
+     * @param goal Minimization or maximization.
+     * @param fTol Tolerance (relative error on the objective function) for
+     * "Powell" algorithm.
+     * @param fLineTol Tolerance (relative error on the objective function)
+     * for the internal line search algorithm.
+     * @param pointTol Tolerance for checking that the optimum is correct.
+     */
+    private void doTest(MultivariateFunction func,
+                        double[] optimum,
+                        double[] init,
+                        GoalType goal,
+                        double fTol,
+                        double fLineTol,
+                        double pointTol) {
+        final MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
+                                                                fLineTol, Math.ulp(1d), null);
+
+        final PointValuePair result = optim.optimize(1000, func, goal, init);
+        final double[] point = result.getPoint();
+
+        for (int i = 0, dim = optimum.length; i < dim; i++) {
+            Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
+                                optimum[i], point[i], pointTol);
+        }
+    }
 }



Mime
View raw message