Author: erans Date: Thu Aug 18 13:38:29 2011 New Revision: 1159214 URL: http://svn.apache.org/viewvc?rev=1159214&view=rev Log: Removed "try" blocks.
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java?rev=1159214&r1=1159213&r2=1159214&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java Thu Aug 18 13:38:29 2011 @@ -201,7 +201,7 @@ public class GaussNewtonOptimizerTest { } - @Test + @Test(expected=ConvergenceException.class) public void testNonInversible() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { @@ -211,12 +211,7 @@ public class GaussNewtonOptimizerTest { }, new double[] { 1, 1, 1 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); - try { - optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); - Assert.fail("an exception should have been caught"); - } catch (ConvergenceException ee) { - // expected behavior - } + optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); } @Test @@ -255,7 +250,7 @@ public class GaussNewtonOptimizerTest { } - @Test + @Test(expected=ConvergenceException.class) public void testMoreEstimatedParametersSimple() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { @@ -266,16 +261,11 @@ public class GaussNewtonOptimizerTest { GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); - try { - optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, - new double[] { 7, 6, 5, 4 }); - Assert.fail("an exception should have been caught"); - } catch (ConvergenceException ee) { - // expected behavior - } + optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, + new double[] { 7, 6, 5, 4 }); } - @Test + @Test(expected=ConvergenceException.class) public void testMoreEstimatedParametersUnsorted() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, @@ -286,13 +276,8 @@ public class GaussNewtonOptimizerTest { }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); - try { - optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, - new double[] { 2, 2, 2, 2, 2, 2 }); - Assert.fail("an exception should have been caught"); - } catch (ConvergenceException ee) { - // expected behavior - } + optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, + new double[] { 2, 2, 2, 2, 2, 2 }); } @Test @@ -311,7 +296,6 @@ public class GaussNewtonOptimizerTest { Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(2.0, optimum.getPoint()[0], 1.0e-8); Assert.assertEquals(1.0, optimum.getPoint()[1], 1.0e-8); - } @Test @@ -329,8 +313,8 @@ public class GaussNewtonOptimizerTest { } - @Test - public void testInconsistentSizes() throws MathUserException { + @Test(expected=DimensionMismatchException.class) + public void testInconsistentSizes1() throws MathUserException { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); @@ -342,27 +326,30 @@ public class GaussNewtonOptimizerTest { Assert.assertEquals(-1, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(+1, optimum.getPoint()[1], 1.0e-10); - try { - optimizer.optimize(100, problem, problem.target, - new double[] { 1 }, - new double[] { 0, 0 }); - Assert.fail("an exception should have been thrown"); - } catch (DimensionMismatchException oe) { - // expected behavior - } + optimizer.optimize(100, problem, problem.target, + new double[] { 1 }, + new double[] { 0, 0 }); + } - try { - optimizer.optimize(100, problem, new double[] { 1 }, - new double[] { 1 }, - new double[] { 0, 0 }); - Assert.fail("an exception should have been thrown"); - } catch (DimensionMismatchException oe) { - // expected behavior - } + @Test(expected=DimensionMismatchException.class) + public void testInconsistentSizes2() throws MathUserException { + LinearProblem problem = + new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 }); + GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); + optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); + + VectorialPointValuePair optimum = + optimizer.optimize(100, problem, problem.target, new double[] { 1, 1 }, new double[] { 0, 0 }); + Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); + Assert.assertEquals(-1, optimum.getPoint()[0], 1.0e-10); + Assert.assertEquals(+1, optimum.getPoint()[1], 1.0e-10); + optimizer.optimize(100, problem, new double[] { 1 }, + new double[] { 1 }, + new double[] { 0, 0 }); } - @Test + @Test(expected=TooManyEvaluationsException.class) public void testMaxEvaluations() throws Exception { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30.0, 68.0); @@ -372,14 +359,10 @@ public class GaussNewtonOptimizerTest { circle.addPoint( 45.0, 97.0); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialPointChecker(1.0e-30, 1.0e-30)); - try { - optimizer.optimize(100, circle, new double[] { 0, 0, 0, 0, 0 }, - new double[] { 1, 1, 1, 1, 1 }, - new double[] { 98.680, 47.345 }); - Assert.fail("an exception should have been caught"); - } catch (TooManyEvaluationsException ee) { - // expected behavior - } + + optimizer.optimize(100, circle, new double[] { 0, 0, 0, 0, 0 }, + new double[] { 1, 1, 1, 1, 1 }, + new double[] { 98.680, 47.345 }); } @Test @@ -403,40 +386,10 @@ public class GaussNewtonOptimizerTest { Assert.assertEquals(48.135167894714, center.y, 1.0e-10); } - @Test + @Test(expected=ConvergenceException.class) public void testCircleFittingBadInit() throws MathUserException { CircleVectorial circle = new CircleVectorial(); - double[][] points = new double[][] { - {-0.312967, 0.072366}, {-0.339248, 0.132965}, {-0.379780, 0.202724}, - {-0.390426, 0.260487}, {-0.361212, 0.328325}, {-0.346039, 0.392619}, - {-0.280579, 0.444306}, {-0.216035, 0.470009}, {-0.149127, 0.493832}, - {-0.075133, 0.483271}, {-0.007759, 0.452680}, { 0.060071, 0.410235}, - { 0.103037, 0.341076}, { 0.118438, 0.273884}, { 0.131293, 0.192201}, - { 0.115869, 0.129797}, { 0.072223, 0.058396}, { 0.022884, 0.000718}, - {-0.053355, -0.020405}, {-0.123584, -0.032451}, {-0.216248, -0.032862}, - {-0.278592, -0.005008}, {-0.337655, 0.056658}, {-0.385899, 0.112526}, - {-0.405517, 0.186957}, {-0.415374, 0.262071}, {-0.387482, 0.343398}, - {-0.347322, 0.397943}, {-0.287623, 0.458425}, {-0.223502, 0.475513}, - {-0.135352, 0.478186}, {-0.061221, 0.483371}, { 0.003711, 0.422737}, - { 0.065054, 0.375830}, { 0.108108, 0.297099}, { 0.123882, 0.222850}, - { 0.117729, 0.134382}, { 0.085195, 0.056820}, { 0.029800, -0.019138}, - {-0.027520, -0.072374}, {-0.102268, -0.091555}, {-0.200299, -0.106578}, - {-0.292731, -0.091473}, {-0.356288, -0.051108}, {-0.420561, 0.014926}, - {-0.471036, 0.074716}, {-0.488638, 0.182508}, {-0.485990, 0.254068}, - {-0.463943, 0.338438}, {-0.406453, 0.404704}, {-0.334287, 0.466119}, - {-0.254244, 0.503188}, {-0.161548, 0.495769}, {-0.075733, 0.495560}, - { 0.001375, 0.434937}, { 0.082787, 0.385806}, { 0.115490, 0.323807}, - { 0.141089, 0.223450}, { 0.138693, 0.131703}, { 0.126415, 0.049174}, - { 0.066518, -0.010217}, {-0.005184, -0.070647}, {-0.080985, -0.103635}, - {-0.177377, -0.116887}, {-0.260628, -0.100258}, {-0.335756, -0.056251}, - {-0.405195, -0.000895}, {-0.444937, 0.085456}, {-0.484357, 0.175597}, - {-0.472453, 0.248681}, {-0.438580, 0.347463}, {-0.402304, 0.422428}, - {-0.326777, 0.479438}, {-0.247797, 0.505581}, {-0.152676, 0.519380}, - {-0.071754, 0.516264}, { 0.015942, 0.472802}, { 0.076608, 0.419077}, - { 0.127673, 0.330264}, { 0.159951, 0.262150}, { 0.153530, 0.172681}, - { 0.140653, 0.089229}, { 0.078666, 0.024981}, { 0.023807, -0.037022}, - {-0.048837, -0.077056}, {-0.127729, -0.075338}, {-0.221271, -0.067526} - }; + double[][] points = circlePoints; double[] target = new double[points.length]; Arrays.fill(target, 0.0); double[] weights = new double[points.length]; @@ -446,19 +399,29 @@ public class GaussNewtonOptimizerTest { } GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); - try { - optimizer.optimize(100, circle, target, weights, new double[] { -12, -12 }); - Assert.fail("an exception should have been caught"); - } catch (ConvergenceException ee) { - // expected behavior + + optimizer.optimize(100, circle, target, weights, new double[] { -12, -12 }); + } + + @Test + public void testCircleFittingGoodInit() throws MathUserException { + CircleVectorial circle = new CircleVectorial(); + double[][] points = circlePoints; + double[] target = new double[points.length]; + Arrays.fill(target, 0.0); + double[] weights = new double[points.length]; + Arrays.fill(weights, 2.0); + for (int i = 0; i < points.length; ++i) { + circle.addPoint(points[i][0], points[i][1]); } + GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); + optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); VectorialPointValuePair optimum = optimizer.optimize(100, circle, target, weights, new double[] { 0, 0 }); Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1.0e-6); Assert.assertEquals(0.2074999736353867, optimum.getPointRef()[1], 1.0e-6); Assert.assertEquals(0.04268731682389561, optimizer.getRMS(), 1.0e-8); - } private static class LinearProblem implements DifferentiableMultivariateVectorialFunction, Serializable { @@ -483,4 +446,36 @@ public class GaussNewtonOptimizerTest { }; } } + + private final double[][] circlePoints = new double[][] { + {-0.312967, 0.072366}, {-0.339248, 0.132965}, {-0.379780, 0.202724}, + {-0.390426, 0.260487}, {-0.361212, 0.328325}, {-0.346039, 0.392619}, + {-0.280579, 0.444306}, {-0.216035, 0.470009}, {-0.149127, 0.493832}, + {-0.075133, 0.483271}, {-0.007759, 0.452680}, { 0.060071, 0.410235}, + { 0.103037, 0.341076}, { 0.118438, 0.273884}, { 0.131293, 0.192201}, + { 0.115869, 0.129797}, { 0.072223, 0.058396}, { 0.022884, 0.000718}, + {-0.053355, -0.020405}, {-0.123584, -0.032451}, {-0.216248, -0.032862}, + {-0.278592, -0.005008}, {-0.337655, 0.056658}, {-0.385899, 0.112526}, + {-0.405517, 0.186957}, {-0.415374, 0.262071}, {-0.387482, 0.343398}, + {-0.347322, 0.397943}, {-0.287623, 0.458425}, {-0.223502, 0.475513}, + {-0.135352, 0.478186}, {-0.061221, 0.483371}, { 0.003711, 0.422737}, + { 0.065054, 0.375830}, { 0.108108, 0.297099}, { 0.123882, 0.222850}, + { 0.117729, 0.134382}, { 0.085195, 0.056820}, { 0.029800, -0.019138}, + {-0.027520, -0.072374}, {-0.102268, -0.091555}, {-0.200299, -0.106578}, + {-0.292731, -0.091473}, {-0.356288, -0.051108}, {-0.420561, 0.014926}, + {-0.471036, 0.074716}, {-0.488638, 0.182508}, {-0.485990, 0.254068}, + {-0.463943, 0.338438}, {-0.406453, 0.404704}, {-0.334287, 0.466119}, + {-0.254244, 0.503188}, {-0.161548, 0.495769}, {-0.075733, 0.495560}, + { 0.001375, 0.434937}, { 0.082787, 0.385806}, { 0.115490, 0.323807}, + { 0.141089, 0.223450}, { 0.138693, 0.131703}, { 0.126415, 0.049174}, + { 0.066518, -0.010217}, {-0.005184, -0.070647}, {-0.080985, -0.103635}, + {-0.177377, -0.116887}, {-0.260628, -0.100258}, {-0.335756, -0.056251}, + {-0.405195, -0.000895}, {-0.444937, 0.085456}, {-0.484357, 0.175597}, + {-0.472453, 0.248681}, {-0.438580, 0.347463}, {-0.402304, 0.422428}, + {-0.326777, 0.479438}, {-0.247797, 0.505581}, {-0.152676, 0.519380}, + {-0.071754, 0.516264}, { 0.015942, 0.472802}, { 0.076608, 0.419077}, + { 0.127673, 0.330264}, { 0.159951, 0.262150}, { 0.153530, 0.172681}, + { 0.140653, 0.089229}, { 0.078666, 0.024981}, { 0.023807, -0.037022}, + {-0.048837, -0.077056}, {-0.127729, -0.075338}, {-0.221271, -0.067526} + }; }