commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r990792 [5/5] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/ main/java/org/apache/commons/math/optimization/ main/java/org/apache/commons/math/optimization/direct/ main/java/org/apache/commons/math/optimization/fitt...
Date Mon, 30 Aug 2010 13:06:24 GMT
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
Mon Aug 30 13:06:22 2010
@@ -26,11 +26,13 @@ import java.util.List;
 import junit.framework.TestCase;
 
 import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.ConvergenceException;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.exception.TooManyEvaluationsException;
 import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
 import org.apache.commons.math.analysis.MultivariateMatrixFunction;
 import org.apache.commons.math.linear.BlockRealMatrix;
 import org.apache.commons.math.linear.RealMatrix;
-import org.apache.commons.math.optimization.OptimizationException;
 import org.apache.commons.math.optimization.SimpleVectorialValueChecker;
 import org.apache.commons.math.optimization.VectorialPointValuePair;
 import org.apache.commons.math.util.FastMath;
@@ -104,7 +106,7 @@ public class LevenbergMarquardtOptimizer
         super(name);
     }
 
-    public void testTrivial() throws FunctionEvaluationException, OptimizationException {
+    public void testTrivial() throws FunctionEvaluationException {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
         LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
@@ -114,7 +116,7 @@ public class LevenbergMarquardtOptimizer
         try {
             optimizer.guessParametersErrors();
             fail("an exception should have been thrown");
-        } catch (OptimizationException ee) {
+        } catch (ConvergenceException ee) {
             // expected behavior
         } catch (Exception e) {
             fail("wrong exception caught");
@@ -123,7 +125,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(3.0, optimum.getValue()[0], 1.0e-10);
     }
 
-    public void testQRColumnsPermutation() throws FunctionEvaluationException, OptimizationException
{
+    public void testQRColumnsPermutation() throws FunctionEvaluationException {
 
         LinearProblem problem =
             new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 }
},
@@ -141,7 +143,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testNoDependency() throws FunctionEvaluationException, OptimizationException
{
+    public void testNoDependency() throws FunctionEvaluationException {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 2, 0, 0, 0, 0, 0 },
                 { 0, 2, 0, 0, 0, 0 },
@@ -160,7 +162,7 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    public void testOneSet() throws FunctionEvaluationException, OptimizationException {
+    public void testOneSet() throws FunctionEvaluationException {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1,  0, 0 },
@@ -177,7 +179,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testTwoSets() throws FunctionEvaluationException, OptimizationException {
+    public void testTwoSets() throws FunctionEvaluationException {
         double epsilon = 1.0e-7;
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  2,  1,   0,  4,       0, 0 },
@@ -202,7 +204,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testNonInversible() throws FunctionEvaluationException, OptimizationException
{
+    public void testNonInversible() throws FunctionEvaluationException {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1, 2, -3 },
@@ -216,7 +218,7 @@ public class LevenbergMarquardtOptimizer
         try {
             optimizer.getCovariances();
             fail("an exception should have been thrown");
-        } catch (OptimizationException ee) {
+        } catch (ConvergenceException ee) {
             // expected behavior
         } catch (Exception e) {
             fail("wrong exception caught");
@@ -224,7 +226,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testIllConditioned() throws FunctionEvaluationException, OptimizationException
{
+    public void testIllConditioned() throws FunctionEvaluationException {
         LinearProblem problem1 = new LinearProblem(new double[][] {
                 { 10.0, 7.0,  8.0,  7.0 },
                 {  7.0, 5.0,  6.0,  5.0 },
@@ -258,7 +260,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testMoreEstimatedParametersSimple() throws FunctionEvaluationException, OptimizationException
{
+    public void testMoreEstimatedParametersSimple() throws FunctionEvaluationException {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 3.0, 2.0,  0.0, 0.0 },
@@ -273,7 +275,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testMoreEstimatedParametersUnsorted() throws FunctionEvaluationException,
OptimizationException {
+    public void testMoreEstimatedParametersUnsorted() throws FunctionEvaluationException
{
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0, 1.0,  0.0,  0.0, 0.0,  0.0 },
                 { 0.0, 0.0,  1.0,  1.0, 1.0,  0.0 },
@@ -294,7 +296,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testRedundantEquations() throws FunctionEvaluationException, OptimizationException
{
+    public void testRedundantEquations() throws FunctionEvaluationException {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -311,7 +313,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testInconsistentEquations() throws FunctionEvaluationException, OptimizationException
{
+    public void testInconsistentEquations() throws FunctionEvaluationException {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -324,7 +326,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testInconsistentSizes() throws FunctionEvaluationException, OptimizationException
{
+    public void testInconsistentSizes() throws FunctionEvaluationException {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1
});
         LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
@@ -340,7 +342,7 @@ public class LevenbergMarquardtOptimizer
                                new double[] { 1 },
                                new double[] { 0, 0 });
             fail("an exception should have been thrown");
-        } catch (OptimizationException oe) {
+        } catch (DimensionMismatchException oe) {
             // expected behavior
         } catch (Exception e) {
             fail("wrong exception caught");
@@ -380,23 +382,23 @@ public class LevenbergMarquardtOptimizer
         try {
             LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
             optimizer.setInitialStepBoundFactor(initialStepBoundFactor);
-            optimizer.setMaxIterations(maxCostEval);
+            optimizer.setMaxEvaluations(maxCostEval);
             optimizer.setCostRelativeTolerance(costRelativeTolerance);
             optimizer.setParRelativeTolerance(parRelativeTolerance);
             optimizer.setOrthoTolerance(orthoTolerance);
             optimizer.optimize(problem, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1,
1, 1, 1, 1 },
                                new double[] { 98.680, 47.345 });
-            assertTrue(! shouldFail);
-        } catch (OptimizationException ee) {
-            assertTrue(shouldFail);
+            assertTrue(!shouldFail);
         } catch (FunctionEvaluationException ee) {
             assertTrue(shouldFail);
+        } catch (TooManyEvaluationsException ee) {
+            assertTrue(shouldFail);
         } catch (Exception e) {
             fail("wrong exception type caught");
         }
     }
 
-    public void testCircleFitting() throws FunctionEvaluationException, OptimizationException
{
+    public void testCircleFitting() throws FunctionEvaluationException {
         Circle circle = new Circle();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);
@@ -445,7 +447,7 @@ public class LevenbergMarquardtOptimizer
 
     }
 
-    public void testCircleFittingBadInit() throws FunctionEvaluationException, OptimizationException
{
+    public void testCircleFittingBadInit() throws FunctionEvaluationException {
         Circle circle = new Circle();
         double[][] points = new double[][] {
                 {-0.312967,  0.072366}, {-0.339248,  0.132965}, {-0.379780,  0.202724},
@@ -513,7 +515,7 @@ public class LevenbergMarquardtOptimizer
                                new double[] { 0.0, 4.4e-323, 1.0, 4.4e-323, 0.0 },
                                new double[] { 0, 0, 0 });
             fail("an exception should have been thrown");
-        } catch (OptimizationException ee) {
+        } catch (ConvergenceException ee) {
             // expected behavior
         }
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/MinpackTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/MinpackTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/MinpackTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/MinpackTest.java
Mon Aug 30 13:06:22 2010
@@ -23,9 +23,9 @@ import java.util.Arrays;
 import junit.framework.TestCase;
 
 import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.TooManyEvaluationsException;
 import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
 import org.apache.commons.math.analysis.MultivariateMatrixFunction;
-import org.apache.commons.math.optimization.OptimizationException;
 import org.apache.commons.math.optimization.VectorialPointValuePair;
 import org.apache.commons.math.util.FastMath;
 
@@ -490,7 +490,7 @@ public class MinpackTest extends TestCas
 
   private void minpackTest(MinpackFunction function, boolean exceptionExpected) {
       LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
-      optimizer.setMaxIterations(100 * (function.getN() + 1));
+      optimizer.setMaxEvaluations(400 * (function.getN() + 1));
       optimizer.setCostRelativeTolerance(FastMath.sqrt(2.22044604926e-16));
       optimizer.setParRelativeTolerance(FastMath.sqrt(2.22044604926e-16));
       optimizer.setOrthoTolerance(2.22044604926e-16);
@@ -503,7 +503,7 @@ public class MinpackTest extends TestCas
           assertFalse(exceptionExpected);
           function.checkTheoreticalMinCost(optimizer.getRMS());
           function.checkTheoreticalMinParams(optimum);
-      } catch (OptimizationException lsse) {
+      } catch (TooManyEvaluationsException e) {
           assertTrue(exceptionExpected);
       } catch (FunctionEvaluationException fe) {
           assertTrue(exceptionExpected);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
Mon Aug 30 13:06:22 2010
@@ -109,7 +109,7 @@ extends TestCase {
             new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0 });
@@ -125,7 +125,7 @@ extends TestCase {
 
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0 });
@@ -146,7 +146,7 @@ extends TestCase {
         }, new double[] { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5 });
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0,
0 });
@@ -164,7 +164,7 @@ extends TestCase {
         }, new double[] { 1, 1, 1});
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
@@ -187,7 +187,7 @@ extends TestCase {
 
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setPreconditioner(new Preconditioner() {
             public double[] precondition(double[] point, double[] r) {
                 double[] d = r.clone();
@@ -222,7 +222,7 @@ extends TestCase {
         }, new double[] { 1, 1, 1 });
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
                 optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
@@ -238,7 +238,7 @@ extends TestCase {
         }, new double[] { 32, 23, 33, 31 });
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-13, 1.0e-13));
         BrentSolver solver = new BrentSolver();
         solver.setAbsoluteAccuracy(1.0e-15);
@@ -277,7 +277,7 @@ extends TestCase {
 
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 7, 6, 5, 4 });
@@ -296,7 +296,7 @@ extends TestCase {
         }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 });
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 2, 2, 2, 2, 2,
2 });
@@ -312,7 +312,7 @@ extends TestCase {
 
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 });
@@ -330,7 +330,7 @@ extends TestCase {
 
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 });
@@ -347,7 +347,7 @@ extends TestCase {
         circle.addPoint( 45.0,  97.0);
         NonLinearConjugateGradientOptimizer optimizer =
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
-        optimizer.setMaxIterations(100);
+        optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-30, 1.0e-30));
         BrentSolver solver = new BrentSolver();
         solver.setAbsoluteAccuracy(1.0e-13);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/PowellOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/PowellOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/PowellOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/PowellOptimizerTest.java
Mon Aug 30 13:06:22 2010
@@ -48,13 +48,13 @@ public class PowellOptimizerTest {
         for (int i = 0; i < dim; i++) {
             init[i] = minPoint[i];
         }
-        // doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-5, 1e-9, 1e-7);
+        doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-9, 1e-7);
 
         // Initial is far from minimum.
         for (int i = 0; i < dim; i++) {
             init[i] = minPoint[i] + 3;
         }
-        doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-5, 1e-9, 1e-7);
+        doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-9, 1e-7);
     }
 
     @Test
@@ -80,13 +80,13 @@ public class PowellOptimizerTest {
         for (int i = 0; i < dim; i++) {
             init[i] = minPoint[i];
         }
-        doTest(func, minPoint, init,  GoalType.MINIMIZE, 1e-5, 1e-9, 1e-8);
+        doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8);
 
         // Initial is far from minimum.
         for (int i = 0; i < dim; i++) {
             init[i] = minPoint[i] - 20;
         }
-        doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-5, 1e-9, 1e-8);
+        doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8);
     }
 
     @Test
@@ -112,13 +112,13 @@ public class PowellOptimizerTest {
         for (int i = 0; i < dim; i++) {
             init[i] = maxPoint[i];
         }
-        doTest(func, maxPoint, init,  GoalType.MAXIMIZE, 1e-5, 1e-9, 1e-8);
+        doTest(func, maxPoint, init,  GoalType.MAXIMIZE, 1e-9, 1e-8);
 
         // Initial is far from minimum.
         for (int i = 0; i < dim; i++) {
             init[i] = maxPoint[i] - 20;
         }
-        doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-5, 1e-9, 1e-8);
+        doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8);
     }
 
     /**
@@ -126,8 +126,6 @@ public class PowellOptimizerTest {
      * @param optimum Expected optimum.
      * @param init Starting point.
      * @param goal Minimization or maximization.
-     * @param xTol Tolerance (relative error on the objective function) for
-     * "Brent" line search algorithm used by "Powell".
      * @param fTol Tolerance (relative error on the objective function) for
      * "Powell" algorithm.
      * @param pointTol Tolerance for checking that the optimum is correct.
@@ -136,12 +134,12 @@ public class PowellOptimizerTest {
                         double[] optimum,
                         double[] init,
                         GoalType goal,
-                        double xTol,
                         double fTol,
                         double pointTol)
         throws MathException {
-        final MultivariateRealOptimizer optim = new PowellOptimizer(xTol);
-        optim.setConvergenceChecker(new SimpleScalarValueChecker(fTol, -1));
+        final MultivariateRealOptimizer optim = new PowellOptimizer();
+        optim.setMaxEvaluations(1000);
+        optim.setConvergenceChecker(new SimpleScalarValueChecker(fTol, Math.ulp(1d)));
 
         final RealPointValuePair result = optim.optimize(func, goal, init);
         final double[] found = result.getPoint();

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BracketFinderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BracketFinderTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BracketFinderTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BracketFinderTest.java
Mon Aug 30 13:06:22 2010
@@ -24,6 +24,9 @@ import org.apache.commons.math.optimizat
 import org.junit.Assert;
 import org.junit.Test;
 
+/**
+ * Test for {@link BracketFinder}.
+ */
 public class BracketFinderTest {
 
     @Test
@@ -70,4 +73,52 @@ public class BracketFinderTest {
         Assert.assertEquals(-1, bFind.getMid(), tol);
         Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol);
     }
+
+    @Test
+    public void testMinimumIsOnIntervalBoundary() throws MathException {
+        final UnivariateRealFunction func = new UnivariateRealFunction() {
+                public double value(double x)
+                    throws FunctionEvaluationException {
+                    return x * x;
+                }
+            };
+
+        final BracketFinder bFind = new BracketFinder();
+
+        bFind.search(func, GoalType.MINIMIZE, 0, 1);
+        Assert.assertTrue(bFind.getLo() <= 0);
+        Assert.assertTrue(0 <= bFind.getHi());
+
+        bFind.search(func, GoalType.MINIMIZE, -1, 0);
+        Assert.assertTrue(bFind.getLo() <= 0);
+        Assert.assertTrue(0 <= bFind.getHi());
+    }
+
+    @Test
+    public void testIntervalBoundsOrdering() throws MathException {
+        final UnivariateRealFunction func = new UnivariateRealFunction() {
+                public double value(double x)
+                    throws FunctionEvaluationException {
+                    return x * x;
+                }
+            };
+
+        final BracketFinder bFind = new BracketFinder();
+
+        bFind.search(func, GoalType.MINIMIZE, -1, 1);
+        Assert.assertTrue(bFind.getLo() <= 0);
+        Assert.assertTrue(0 <= bFind.getHi());
+
+        bFind.search(func, GoalType.MINIMIZE, 1, -1);
+        Assert.assertTrue(bFind.getLo() <= 0);
+        Assert.assertTrue(0 <= bFind.getHi());
+
+        bFind.search(func, GoalType.MINIMIZE, 1, 2);
+        Assert.assertTrue(bFind.getLo() <= 0);
+        Assert.assertTrue(0 <= bFind.getHi());
+
+        bFind.search(func, GoalType.MINIMIZE, 2, 1);
+        Assert.assertTrue(bFind.getLo() <= 0);
+        Assert.assertTrue(0 <= bFind.getHi());
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BrentOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BrentOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BrentOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BrentOptimizerTest.java
Mon Aug 30 13:06:22 2010
@@ -20,17 +20,14 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.MathException;
-import org.apache.commons.math.MaxIterationsExceededException;
-import org.apache.commons.math.exception.NoDataException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.TooManyEvaluationsException;
 import org.apache.commons.math.analysis.QuinticFunction;
 import org.apache.commons.math.analysis.SinFunction;
 import org.apache.commons.math.analysis.UnivariateRealFunction;
 import org.apache.commons.math.optimization.GoalType;
-import org.apache.commons.math.optimization.UnivariateRealOptimizer;
 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
-import org.apache.commons.math.util.FastMath;
 import org.junit.Test;
 
 /**
@@ -41,28 +38,22 @@ public final class BrentOptimizerTest {
     @Test
     public void testSinMin() throws MathException {
         UnivariateRealFunction f = new SinFunction();
-        UnivariateRealOptimizer minimizer = new BrentOptimizer();
-        minimizer.setMaxEvaluations(200);
-        assertEquals(200, minimizer.getMaxEvaluations());
-        try {
-            minimizer.getResult();
-            fail("an exception should have been thrown");
-        } catch (NoDataException ise) {
-            // expected
-        } catch (Exception e) {
-            fail("wrong exception caught");
-        }
-        assertEquals(3 * FastMath.PI / 2, minimizer.optimize(f, GoalType.MINIMIZE, 4, 5),
10 * minimizer.getRelativeAccuracy());
-        assertTrue(minimizer.getIterationCount() <= 50);
-        assertEquals(3 * FastMath.PI / 2, minimizer.optimize(f, GoalType.MINIMIZE, 1, 5),
10 * minimizer.getRelativeAccuracy());
-        assertTrue(minimizer.getIterationCount() <= 50);
-        assertTrue(minimizer.getEvaluations()    <= 100);
-        assertTrue(minimizer.getEvaluations()    >=  15);
-        minimizer.setMaxEvaluations(10);
+        UnivariateRealOptimizer optimizer = new BrentOptimizer();
+        optimizer.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-10,
1e-14));
+        optimizer.setMaxEvaluations(200);
+        assertEquals(200, optimizer.getMaxEvaluations());
+        assertEquals(3 * Math.PI / 2, optimizer.optimize(f, GoalType.MINIMIZE, 4, 5).getPoint(),
+                     100 * optimizer.getConvergenceChecker().getRelativeThreshold());
+        assertTrue(optimizer.getEvaluations() <= 50);
+        assertEquals(3 * Math.PI / 2, optimizer.optimize(f, GoalType.MINIMIZE, 1, 5).getPoint(),
+                     100 * optimizer.getConvergenceChecker().getRelativeThreshold());
+        assertTrue(optimizer.getEvaluations() <= 100);
+        assertTrue(optimizer.getEvaluations() >= 15);
+        optimizer.setMaxEvaluations(10);
         try {
-            minimizer.optimize(f, GoalType.MINIMIZE, 4, 5);
+            optimizer.optimize(f, GoalType.MINIMIZE, 4, 5);
             fail("an exception should have been thrown");
-        } catch (FunctionEvaluationException fee) {
+        } catch (TooManyEvaluationsException fee) {
             // expected
         } catch (Exception e) {
             fail("wrong exception caught");
@@ -73,25 +64,27 @@ public final class BrentOptimizerTest {
     public void testQuinticMin() throws MathException {
         // The function has local minima at -0.27195613 and 0.82221643.
         UnivariateRealFunction f = new QuinticFunction();
-        UnivariateRealOptimizer minimizer = new BrentOptimizer();
-        assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2), 1.0e-8);
-        assertEquals( 0.82221643, minimizer.optimize(f, GoalType.MINIMIZE,  0.3,  0.9), 1.0e-8);
-        assertTrue(minimizer.getIterationCount() <= 50);
+        UnivariateRealOptimizer optimizer = new BrentOptimizer();
+        optimizer.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-10,
1e-14));
+        optimizer.setMaxEvaluations(200);
+        assertEquals(-0.27195613, optimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2).getPoint(),
1.0e-8);
+        assertEquals( 0.82221643, optimizer.optimize(f, GoalType.MINIMIZE,  0.3,  0.9).getPoint(),
1.0e-8);
+        assertTrue(optimizer.getEvaluations() <= 50);
 
         // search in a large interval
-        assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -1.0, 0.2), 1.0e-8);
-        assertTrue(minimizer.getIterationCount() <= 50);
+        assertEquals(-0.27195613, optimizer.optimize(f, GoalType.MINIMIZE, -1.0, 0.2).getPoint(),
1.0e-8);
+        assertTrue(optimizer.getEvaluations() <= 50);
     }
 
     @Test
     public void testQuinticMinStatistics() throws MathException {
         // The function has local minima at -0.27195613 and 0.82221643.
         UnivariateRealFunction f = new QuinticFunction();
-        UnivariateRealOptimizer minimizer = new BrentOptimizer();
-        minimizer.setRelativeAccuracy(1e-10);
-        minimizer.setAbsoluteAccuracy(1e-11);
+        UnivariateRealOptimizer optimizer = new BrentOptimizer();
+        optimizer.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-12,
1e-14));
+        optimizer.setMaxEvaluations(40);
 
-        final DescriptiveStatistics[] stat = new DescriptiveStatistics[3];
+        final DescriptiveStatistics[] stat = new DescriptiveStatistics[2];
         for (int i = 0; i < stat.length; i++) {
             stat[i] = new DescriptiveStatistics();
         }
@@ -102,31 +95,29 @@ public final class BrentOptimizerTest {
         final double delta = (max - min) / nSamples;
         for (int i = 0; i < nSamples; i++) {
             final double start = min + i * delta;
-            stat[0].addValue(minimizer.optimize(f, GoalType.MINIMIZE, min, max, start));
-            stat[1].addValue(minimizer.getIterationCount());
-            stat[2].addValue(minimizer.getEvaluations());
+            stat[0].addValue(optimizer.optimize(f, GoalType.MINIMIZE, min, max, start).getPoint());
+            stat[1].addValue(optimizer.getEvaluations());
         }
 
         final double meanOptValue = stat[0].getMean();
-        final double medianIter = stat[1].getPercentile(50);
-        final double medianEval = stat[2].getPercentile(50);
-        assertTrue(meanOptValue > -0.27195612812 && meanOptValue < -0.27195612811);
-        assertEquals(medianIter, 17, FastMath.ulp(1d));
-        assertEquals(medianEval, 18, FastMath.ulp(1d));
+        final double medianEval = stat[1].getPercentile(50);
+        assertTrue(meanOptValue > -0.2719561281 && meanOptValue < -0.2719561280);
+        assertEquals((int) medianEval, 27);
     }
 
-    @Test
+    @Test(expected = TooManyEvaluationsException.class)
     public void testQuinticMax() throws MathException {
         // The quintic function has zeros at 0, +-0.5 and +-1.
         // The function has a local maximum at 0.27195613.
         UnivariateRealFunction f = new QuinticFunction();
-        UnivariateRealOptimizer minimizer = new BrentOptimizer();
-        assertEquals(0.27195613, minimizer.optimize(f, GoalType.MAXIMIZE, 0.2, 0.3), 1.0e-8);
-        minimizer.setMaximalIterationCount(5);
+        UnivariateRealOptimizer optimizer = new BrentOptimizer();
+        optimizer.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-12,
1e-14));
+        assertEquals(0.27195613, optimizer.optimize(f, GoalType.MAXIMIZE, 0.2, 0.3).getPoint(),
1e-8);
+        optimizer.setMaxEvaluations(5);
         try {
-            minimizer.optimize(f, GoalType.MAXIMIZE, 0.2, 0.3);
+            optimizer.optimize(f, GoalType.MAXIMIZE, 0.2, 0.3);
             fail("an exception should have been thrown");
-        } catch (MaxIterationsExceededException miee) {
+        } catch (TooManyEvaluationsException miee) {
             // expected
         } catch (Exception e) {
             fail("wrong exception caught");
@@ -136,15 +127,15 @@ public final class BrentOptimizerTest {
     @Test
     public void testMinEndpoints() throws Exception {
         UnivariateRealFunction f = new SinFunction();
-        UnivariateRealOptimizer solver = new BrentOptimizer();
-
-        solver.setRelativeAccuracy(1e-8);
+        UnivariateRealOptimizer optimizer = new BrentOptimizer();
+        optimizer.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-8,
1e-14));
+        optimizer.setMaxEvaluations(50);
 
         // endpoint is minimum
-        double result = solver.optimize(f, GoalType.MINIMIZE, 3 * FastMath.PI / 2, 5);
-        assertEquals(3 * FastMath.PI / 2, result, 10 * solver.getRelativeAccuracy());
+        double result = optimizer.optimize(f, GoalType.MINIMIZE, 3 * Math.PI / 2, 5).getPoint();
+        assertEquals(3 * Math.PI / 2, result, 100 * optimizer.getConvergenceChecker().getRelativeThreshold());
 
-        result = solver.optimize(f, GoalType.MINIMIZE, 4, 3 * FastMath.PI / 2);
-        assertEquals(3 * FastMath.PI / 2, result, 10 * solver.getRelativeAccuracy());
+        result = optimizer.optimize(f, GoalType.MINIMIZE, 4, 3 * Math.PI / 2).getPoint();
+        assertEquals(3 * Math.PI / 2, result, 100 * optimizer.getConvergenceChecker().getRelativeThreshold());
     }
 }

Copied: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizerTest.java
(from r985626, commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizerTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizerTest.java?p2=commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizerTest.java&p1=commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizerTest.java&r1=985626&r2=990792&rev=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizerTest.java
Mon Aug 30 13:06:22 2010
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.commons.math.optimization;
+package org.apache.commons.math.optimization.univariate;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -26,7 +26,9 @@ import org.apache.commons.math.analysis.
 import org.apache.commons.math.analysis.SinFunction;
 import org.apache.commons.math.analysis.UnivariateRealFunction;
 import org.apache.commons.math.optimization.univariate.BrentOptimizer;
+import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.random.JDKRandomGenerator;
+import org.apache.commons.math.util.FastMath;
 import org.junit.Test;
 
 public class MultiStartUnivariateRealOptimizerTest {
@@ -35,21 +37,22 @@ public class MultiStartUnivariateRealOpt
     public void testSinMin() throws MathException {
         UnivariateRealFunction f = new SinFunction();
         UnivariateRealOptimizer underlying = new BrentOptimizer();
+        underlying.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-10,
1e-14));
+        underlying.setMaxEvaluations(300);
         JDKRandomGenerator g = new JDKRandomGenerator();
         g.setSeed(44428400075l);
-        MultiStartUnivariateRealOptimizer minimizer =
+        MultiStartUnivariateRealOptimizer optimizer =
             new MultiStartUnivariateRealOptimizer(underlying, 10, g);
-        minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
-        double[] optima = minimizer.getOptima();
-        double[] optimaValues = minimizer.getOptimaValues();
+        optimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
+        UnivariateRealPointValuePair[] optima = optimizer.getOptima();
         for (int i = 1; i < optima.length; ++i) {
-            double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
-            assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
-            assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
-            assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
+            double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI);
+            assertTrue (FastMath.abs(d - FastMath.rint(d)) < 1.0e-8);
+            assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
+            assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
         }
-        assertTrue(minimizer.getEvaluations() > 150);
-        assertTrue(minimizer.getEvaluations() < 250);
+        assertTrue(optimizer.getEvaluations() > 150);
+        assertTrue(optimizer.getEvaluations() < 250);
     }
 
     @Test
@@ -58,44 +61,23 @@ public class MultiStartUnivariateRealOpt
         // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
         UnivariateRealFunction f = new QuinticFunction();
         UnivariateRealOptimizer underlying = new BrentOptimizer();
-        underlying.setRelativeAccuracy(1e-15);
+        underlying.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(1e-9,
1e-14));
+        underlying.setMaxEvaluations(300);
         JDKRandomGenerator g = new JDKRandomGenerator();
         g.setSeed(4312000053L);
-        MultiStartUnivariateRealOptimizer minimizer =
+        MultiStartUnivariateRealOptimizer optimizer =
             new MultiStartUnivariateRealOptimizer(underlying, 5, g);
-        minimizer.setAbsoluteAccuracy(10 * minimizer.getAbsoluteAccuracy());
-        minimizer.setRelativeAccuracy(10 * minimizer.getRelativeAccuracy());
 
-        try {
-            minimizer.getOptima();
-            fail("an exception should have been thrown");
-        } catch (IllegalStateException ise) {
-            // expected
-        } catch (Exception e) {
-            fail("wrong exception caught");
-        }
-        try {
-            minimizer.getOptimaValues();
-            fail("an exception should have been thrown");
-        } catch (IllegalStateException ise) {
-            // expected
-        } catch (Exception e) {
-            fail("wrong exception caught");
-        }
-
-        double result = minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2);
-        assertEquals(-0.2719561270319131, result, 1.0e-13);
-        assertEquals(-0.2719561270319131, minimizer.getResult(), 1.0e-13);
-        assertEquals(-0.04433426954946637, minimizer.getFunctionValue(), 1.0e-13);
+        UnivariateRealPointValuePair optimum
+            = optimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2);
+        assertEquals(-0.2719561271, optimum.getPoint(), 1e-9);
+        assertEquals(-0.0443342695, optimum.getValue(), 1e-9);
 
-        double[] optima = minimizer.getOptima();
-        double[] optimaValues = minimizer.getOptimaValues();
+        UnivariateRealPointValuePair[] optima = optimizer.getOptima();
         for (int i = 0; i < optima.length; ++i) {
-            assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
+            assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9);
         }
-        assertTrue(minimizer.getEvaluations()    >= 120);
-        assertTrue(minimizer.getEvaluations()    <= 170);
-        assertTrue(minimizer.getIterationCount() >= 120);
-        assertTrue(minimizer.getIterationCount() <= 170);
+        assertTrue(optimizer.getEvaluations() >= 110);
+        assertTrue(optimizer.getEvaluations() <= 150);
     }
 }



Mime
View raw message