commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1569359 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/
Date Tue, 18 Feb 2014 14:33:28 GMT
Author: luc
Date: Tue Feb 18 14:33:28 2014
New Revision: 1569359

URL: http://svn.apache.org/r1569359
Log:
JUnit directly executes least squares tests
    
Previously JUnit would make the call to test a specific optimizer, and
then that method would call all of the individual test cases relevant to
that optimizer.,Now JUnit will directly call each individual test case.
    
The same test coverage is preserved. The GaussNewtonOptimizerTest is
split into two classes, one for each decomposition algorithm it can use.
There is a significant amount of duplicated code between
GaussNewtonOptimizerWithLUTest and GaussNewtonOptimizerWithQRTest.

Added:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithLUTest.java
      - copied, changed from r1569358, commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java
  (contents, props changed)
      - copied, changed from r1569358, commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
Removed:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java?rev=1569359&r1=1569358&r2=1569359&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
Tue Feb 18 14:33:28 2014
@@ -32,6 +32,7 @@ import org.apache.commons.math3.optim.Si
 import org.apache.commons.math3.util.FastMath;
 import org.apache.commons.math3.util.Pair;
 import org.junit.Assert;
+import org.junit.Test;
 
 import java.io.IOException;
 import java.util.Arrays;
@@ -103,33 +104,19 @@ public abstract class AbstractLeastSquar
     public abstract int getMaxIterations();
 
     /**
-     * Test the give optimizer on a suite of sample problems. If you need to disable a
-     * particular test case override it in your subclass. If you want to add more tests
-     * override this method and call super.
+     * Get an instance of the optimizer under test.
+     *
+     * @return the subject under test.
      */
-    public void check(LeastSquaresOptimizer optimizer) throws Exception {
-        testGetIterations(optimizer);
-        testTrivial(optimizer);
-        testQRColumnsPermutation(optimizer);
-        testNoDependency(optimizer);
-        testOneSet(optimizer);
-        testTwoSets(optimizer);
-        testNonInvertible(optimizer);
-        testIllConditioned(optimizer);
-        testMoreEstimatedParametersSimple(optimizer);
-        testMoreEstimatedParametersUnsorted(optimizer);
-        testRedundantEquations(optimizer);
-        testInconsistentEquations(optimizer);
-        testInconsistentSizes1(optimizer);
-        testInconsistentSizes2(optimizer);
-        testCircleFitting(optimizer);
-        testCircleFittingBadInit(optimizer);
-        testCircleFittingGoodInit(optimizer);
-        testKirby2(optimizer);
-        testHahn1(optimizer);
-    }
+    public abstract LeastSquaresOptimizer getOptimizer();
+
+    /**
+     * The subject under test.
+     */
+    public final LeastSquaresOptimizer optimizer = this.getOptimizer();
 
-    public void testGetIterations(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testGetIterations() {
         LeastSquaresProblem lsp = base()
                 .target(new double[]{1})
                 .weight(new DiagonalMatrix(new double[]{1}))
@@ -158,7 +145,8 @@ public abstract class AbstractLeastSquar
         Assert.assertTrue(optimum.getIterations() > 0);
     }
 
-    public void testTrivial(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testTrivial() {
         LinearProblem problem
                 = new LinearProblem(new double[][]{{2}},
                 new double[]{3});
@@ -171,7 +159,8 @@ public abstract class AbstractLeastSquar
         Assert.assertEquals(3.0, optimum.computeValue().getEntry(0), TOl);
     }
 
-    public void testQRColumnsPermutation(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testQRColumnsPermutation() {
         LinearProblem problem
                 = new LinearProblem(new double[][]{{1, -1}, {0, 2}, {1, -2}},
                 new double[]{4, 6, 1});
@@ -183,7 +172,8 @@ public abstract class AbstractLeastSquar
         assertEquals(TOl, optimum.computeValue(), 4, 6, 1);
     }
 
-    public void testNoDependency(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testNoDependency() {
         LinearProblem problem = new LinearProblem(new double[][]{
                 {2, 0, 0, 0, 0, 0},
                 {0, 2, 0, 0, 0, 0},
@@ -201,7 +191,8 @@ public abstract class AbstractLeastSquar
         }
     }
 
-    public void testOneSet(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testOneSet() {
         LinearProblem problem = new LinearProblem(new double[][]{
                 {1, 0, 0},
                 {-1, 1, 0},
@@ -214,7 +205,8 @@ public abstract class AbstractLeastSquar
         assertEquals(TOl, optimum.getPoint(), 1, 2, 3);
     }
 
-    public void testTwoSets(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testTwoSets() {
         double epsilon = 1e-7;
         LinearProblem problem = new LinearProblem(new double[][]{
                 {2, 1, 0, 4, 0, 0},
@@ -231,7 +223,8 @@ public abstract class AbstractLeastSquar
         assertEquals(TOl, optimum.getPoint(), 3, 4, -1, -2, 1 + epsilon, 1 - epsilon);
     }
 
-    public void testNonInvertible(LeastSquaresOptimizer optimizer) throws Exception {
+    @Test
+    public void testNonInvertible() throws Exception {
         try {
             LinearProblem problem = new LinearProblem(new double[][]{
                     {1, 2, -3},
@@ -247,7 +240,8 @@ public abstract class AbstractLeastSquar
         }
     }
 
-    public void testIllConditioned(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testIllConditioned() {
         LinearProblem problem1 = new LinearProblem(new double[][]{
                 {10, 7, 8, 7},
                 {7, 5, 6, 5},
@@ -275,7 +269,8 @@ public abstract class AbstractLeastSquar
         assertEquals(1e-8, optimum.getPoint(), -81, 137, -34, 22);
     }
 
-    public void testMoreEstimatedParametersSimple(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testMoreEstimatedParametersSimple() {
         LinearProblem problem = new LinearProblem(new double[][]{
                 {3, 2, 0, 0},
                 {0, 1, -1, 1},
@@ -288,7 +283,8 @@ public abstract class AbstractLeastSquar
         Assert.assertEquals(0, optimum.computeRMS(), TOl);
     }
 
-    public void testMoreEstimatedParametersUnsorted(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testMoreEstimatedParametersUnsorted() {
         LinearProblem problem = new LinearProblem(new double[][]{
                 {1, 1, 0, 0, 0, 0},
                 {0, 0, 1, 1, 1, 0},
@@ -305,7 +301,8 @@ public abstract class AbstractLeastSquar
         assertEquals(TOl, optimum.getPoint(), 2, 1, 3, 4, 5, 6);
     }
 
-    public void testRedundantEquations(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testRedundantEquations() {
         LinearProblem problem = new LinearProblem(new double[][]{
                 {1, 1},
                 {1, -1},
@@ -319,7 +316,8 @@ public abstract class AbstractLeastSquar
         assertEquals(TOl, optimum.getPoint(), 2, 1);
     }
 
-    public void testInconsistentEquations(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testInconsistentEquations() {
         LinearProblem problem = new LinearProblem(new double[][]{
                 {1, 1},
                 {1, -1},
@@ -333,7 +331,8 @@ public abstract class AbstractLeastSquar
         Assert.assertTrue(optimum.computeRMS() > 0.1);
     }
 
-    public void testInconsistentSizes1(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testInconsistentSizes1() {
         try {
             LinearProblem problem
                     = new LinearProblem(new double[][]{{1, 0},
@@ -356,7 +355,8 @@ public abstract class AbstractLeastSquar
         }
     }
 
-    public void testInconsistentSizes2(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testInconsistentSizes2() {
         try {
             LinearProblem problem
                     = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
@@ -381,7 +381,8 @@ public abstract class AbstractLeastSquar
         }
     }
 
-    public void testCircleFitting(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testCircleFitting() {
         CircleVectorial circle = new CircleVectorial();
         circle.addPoint(30, 68);
         circle.addPoint(50, -6);
@@ -427,7 +428,8 @@ public abstract class AbstractLeastSquar
         Assert.assertEquals(0.0016, cov[1][1], 0.001);
     }
 
-    public void testCircleFittingBadInit(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testCircleFittingBadInit() {
         CircleVectorial circle = new CircleVectorial();
         double[][] points = circlePoints;
         double[] weights = new double[points.length];
@@ -447,7 +449,8 @@ public abstract class AbstractLeastSquar
         Assert.assertEquals(0.2075001, center.getY(), 1e-6);
     }
 
-    public void testCircleFittingGoodInit(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testCircleFittingGoodInit() {
         CircleVectorial circle = new CircleVectorial();
         double[][] points = circlePoints;
         double[] weights = new double[points.length];
@@ -512,11 +515,13 @@ public abstract class AbstractLeastSquar
         }
     }
 
-    public void testKirby2(LeastSquaresOptimizer optimizer) throws IOException {
+    @Test
+    public void testKirby2() throws IOException {
         doTestStRD(StatisticalReferenceDatasetFactory.createKirby2(), optimizer, 1E-7, 1E-7);
     }
 
-    public void testHahn1(LeastSquaresOptimizer optimizer) throws IOException {
+    @Test
+    public void testHahn1() throws IOException {
         doTestStRD(StatisticalReferenceDatasetFactory.createHahn1(), optimizer, 1E-7, 1E-4);
     }
 

Copied: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithLUTest.java
(from r1569358, commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithLUTest.java?p2=commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithLUTest.java&p1=commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java&r1=1569358&r2=1569359&rev=1569359&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithLUTest.java
Tue Feb 18 14:33:28 2014
@@ -22,7 +22,6 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.fitting.leastsquares.GaussNewtonOptimizer.Decomposition;
 import org.apache.commons.math3.optim.SimpleVectorValueChecker;
 import org.junit.Test;
-import org.junit.Assert;
 
 import java.io.IOException;
 
@@ -35,7 +34,7 @@ import java.io.IOException;
  *
  * @version $Id$
  */
-public class GaussNewtonOptimizerTest
+public class GaussNewtonOptimizerWithLUTest
     extends AbstractLeastSquaresOptimizerAbstractTest {
 
     @Override
@@ -43,30 +42,19 @@ public class GaussNewtonOptimizerTest
         return 1000;
     }
 
-    @Test
-    public void testGaussNewtonLU() throws Exception {
-        check(new GaussNewtonOptimizer(Decomposition.LU));
-    }
-
-    @Test
-    public void testGaussNewtonQR() throws Exception {
-        check(new GaussNewtonOptimizer(Decomposition.QR));
-    }
-
     @Override
-    public void check(LeastSquaresOptimizer optimizer) throws Exception {
-        super.check(optimizer);
-        //add an additional test
-        testMaxEvaluations(optimizer);
+    public LeastSquaresOptimizer getOptimizer() {
+        return new GaussNewtonOptimizer(Decomposition.LU);
     }
 
     @Override
-    public void testMoreEstimatedParametersSimple(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testMoreEstimatedParametersSimple() {
         /*
          * Exception is expected with this optimizer
          */
         try {
-            super.testMoreEstimatedParametersSimple(optimizer);
+            super.testMoreEstimatedParametersSimple();
             fail(optimizer);
         } catch (ConvergenceException e) {
             //expected
@@ -74,19 +62,21 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
-    public void testMoreEstimatedParametersUnsorted(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testMoreEstimatedParametersUnsorted() {
         /*
          * Exception is expected with this optimizer
          */
         try{
-            super.testMoreEstimatedParametersUnsorted(optimizer);
+            super.testMoreEstimatedParametersUnsorted();
             fail(optimizer);
         }catch (ConvergenceException e){
             //expected
         }
     }
 
-    public void testMaxEvaluations(LeastSquaresOptimizer optimizer) throws Exception {
+    @Test
+    public void testMaxEvaluations() throws Exception {
         try{
         CircleVectorial circle = new CircleVectorial();
         circle.addPoint( 30.0,  68.0);
@@ -110,12 +100,13 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
-    public void testCircleFittingBadInit(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testCircleFittingBadInit() {
         /*
          * This test does not converge with this optimizer.
          */
         try{
-            super.testCircleFittingBadInit(optimizer);
+            super.testCircleFittingBadInit();
             fail(optimizer);
         }catch (ConvergenceException e){
             //expected
@@ -123,14 +114,15 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
-    public void testHahn1(LeastSquaresOptimizer optimizer)
+    @Test
+    public void testHahn1()
         throws IOException {
         /*
          * TODO This test leads to a singular problem with the Gauss-Newton
          * optimizer. This should be inquired.
          */
         try{
-            super.testHahn1(optimizer);
+            super.testHahn1();
             fail(optimizer);
         } catch (ConvergenceException e){
             //expected for LU

Copied: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java
(from r1569358, commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java?p2=commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java&p1=commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java&r1=1569358&r2=1569359&rev=1569359&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java
Tue Feb 18 14:33:28 2014
@@ -22,7 +22,6 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.fitting.leastsquares.GaussNewtonOptimizer.Decomposition;
 import org.apache.commons.math3.optim.SimpleVectorValueChecker;
 import org.junit.Test;
-import org.junit.Assert;
 
 import java.io.IOException;
 
@@ -35,7 +34,7 @@ import java.io.IOException;
  *
  * @version $Id$
  */
-public class GaussNewtonOptimizerTest
+public class GaussNewtonOptimizerWithQRTest
     extends AbstractLeastSquaresOptimizerAbstractTest {
 
     @Override
@@ -43,30 +42,19 @@ public class GaussNewtonOptimizerTest
         return 1000;
     }
 
-    @Test
-    public void testGaussNewtonLU() throws Exception {
-        check(new GaussNewtonOptimizer(Decomposition.LU));
-    }
-
-    @Test
-    public void testGaussNewtonQR() throws Exception {
-        check(new GaussNewtonOptimizer(Decomposition.QR));
-    }
-
     @Override
-    public void check(LeastSquaresOptimizer optimizer) throws Exception {
-        super.check(optimizer);
-        //add an additional test
-        testMaxEvaluations(optimizer);
+    public LeastSquaresOptimizer getOptimizer() {
+        return new GaussNewtonOptimizer(Decomposition.QR);
     }
 
     @Override
-    public void testMoreEstimatedParametersSimple(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testMoreEstimatedParametersSimple() {
         /*
          * Exception is expected with this optimizer
          */
         try {
-            super.testMoreEstimatedParametersSimple(optimizer);
+            super.testMoreEstimatedParametersSimple();
             fail(optimizer);
         } catch (ConvergenceException e) {
             //expected
@@ -74,19 +62,21 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
-    public void testMoreEstimatedParametersUnsorted(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testMoreEstimatedParametersUnsorted() {
         /*
          * Exception is expected with this optimizer
          */
         try{
-            super.testMoreEstimatedParametersUnsorted(optimizer);
+            super.testMoreEstimatedParametersUnsorted();
             fail(optimizer);
         }catch (ConvergenceException e){
             //expected
         }
     }
 
-    public void testMaxEvaluations(LeastSquaresOptimizer optimizer) throws Exception {
+    @Test
+    public void testMaxEvaluations() throws Exception {
         try{
         CircleVectorial circle = new CircleVectorial();
         circle.addPoint( 30.0,  68.0);
@@ -110,12 +100,13 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
-    public void testCircleFittingBadInit(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testCircleFittingBadInit() {
         /*
          * This test does not converge with this optimizer.
          */
         try{
-            super.testCircleFittingBadInit(optimizer);
+            super.testCircleFittingBadInit();
             fail(optimizer);
         }catch (ConvergenceException e){
             //expected
@@ -123,14 +114,15 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
-    public void testHahn1(LeastSquaresOptimizer optimizer)
+    @Test
+    public void testHahn1()
         throws IOException {
         /*
          * TODO This test leads to a singular problem with the Gauss-Newton
          * optimizer. This should be inquired.
          */
         try{
-            super.testHahn1(optimizer);
+            super.testHahn1();
             fail(optimizer);
         } catch (ConvergenceException e){
             //expected for LU

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerWithQRTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java?rev=1569359&r1=1569358&r2=1569359&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
Tue Feb 18 14:33:28 2014
@@ -64,21 +64,14 @@ public class LevenbergMarquardtOptimizer
         return 25;
     }
 
-    @Test
-    public void testLevenberMarquardtOptimizer() throws Exception {
-        check(new LevenbergMarquardtOptimizer());
-    }
-
     @Override
-    public void check(LeastSquaresOptimizer optimizer) throws Exception {
-        super.check(optimizer);
-        //add LM specific tests
-        testBevington(optimizer);
-        testCircleFitting2(optimizer);
+    public LeastSquaresOptimizer getOptimizer() {
+        return new LevenbergMarquardtOptimizer();
     }
 
     @Override
-    public void testNonInvertible(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testNonInvertible() {
         try{
             /*
              * Overrides the method from parent class, since the default singularity
@@ -162,7 +155,8 @@ public class LevenbergMarquardtOptimizer
      * relaxed for this test to be currently really useful (the issue is under
      * investigation).
      */
-    public void testBevington(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testBevington() {
         final double[][] dataPoints = {
             // column 1 = times
             { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
@@ -233,7 +227,8 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    public void testCircleFitting2(LeastSquaresOptimizer optimizer) {
+    @Test
+    public void testCircleFitting2() {
         final double xCenter = 123.456;
         final double yCenter = 654.321;
         final double xSigma = 10;



Mime
View raw message