From e21865e8066874cf6bc43898e5ade28b893d5f4e Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Mon, 5 Nov 2007 17:11:29 +0000 Subject: [PATCH] improved test coverage git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@592093 13f79535-47bb-0310-9956-ffa450edef68 --- .../estimation/GaussNewtonEstimatorTest.java | 46 +++++++++---- .../LevenbergMarquardtEstimatorTest.java | 64 ++++++++++++++----- 2 files changed, 80 insertions(+), 30 deletions(-) diff --git a/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java b/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java index 70a8e294e..1fbe10bea 100644 --- a/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java +++ b/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java @@ -445,21 +445,39 @@ public class GaussNewtonEstimatorTest } + public void testMaxIterations() { + Circle circle = new Circle(98.680, 47.345); + circle.addPoint( 30.0, 68.0); + circle.addPoint( 50.0, -6.0); + circle.addPoint(110.0, -20.0); + circle.addPoint( 35.0, 15.0); + circle.addPoint( 45.0, 97.0); + try { + GaussNewtonEstimator estimator = new GaussNewtonEstimator(4, 1.0e-14, 1.0e-14); + estimator.estimate(circle); + fail("an exception should have been caught"); + } catch (EstimationException ee) { + // expected behavior + } catch (Exception e) { + fail("wrong exception type caught"); + } + } + public void testCircleFitting() throws EstimationException { - Circle circle = new Circle(98.680, 47.345); - circle.addPoint( 30.0, 68.0); - circle.addPoint( 50.0, -6.0); - circle.addPoint(110.0, -20.0); - circle.addPoint( 35.0, 15.0); - circle.addPoint( 45.0, 97.0); - GaussNewtonEstimator estimator = new GaussNewtonEstimator(100, 1.0e-10, 1.0e-10); - estimator.estimate(circle); - double rms = estimator.getRMS(circle); - assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10); - assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10); - assertEquals(96.07590211815305, circle.getX(), 1.0e-10); - assertEquals(48.13516790438953, circle.getY(), 1.0e-10); - } + Circle circle = new Circle(98.680, 47.345); + circle.addPoint( 30.0, 68.0); + circle.addPoint( 50.0, -6.0); + circle.addPoint(110.0, -20.0); + circle.addPoint( 35.0, 15.0); + circle.addPoint( 45.0, 97.0); + GaussNewtonEstimator estimator = new GaussNewtonEstimator(100, 1.0e-10, 1.0e-10); + estimator.estimate(circle); + double rms = estimator.getRMS(circle); + assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10); + assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10); + assertEquals(96.07590211815305, circle.getX(), 1.0e-10); + assertEquals(48.13516790438953, circle.getY(), 1.0e-10); + } public void testCircleFittingBadInit() throws EstimationException { Circle circle = new Circle(-12, -12); diff --git a/src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java b/src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java index d745a543b..c283dbeaa 100644 --- a/src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java +++ b/src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java @@ -446,24 +446,56 @@ public class LevenbergMarquardtEstimatorTest } - public void testCircleFitting() throws EstimationException { - Circle circle = new Circle(98.680, 47.345); - circle.addPoint( 30.0, 68.0); - circle.addPoint( 50.0, -6.0); - circle.addPoint(110.0, -20.0); - circle.addPoint( 35.0, 15.0); - circle.addPoint( 45.0, 97.0); - LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator(); - estimator.estimate(circle); - assertTrue(estimator.getCostEvaluations() < 10); - assertTrue(estimator.getJacobianEvaluations() < 10); - double rms = estimator.getRMS(circle); - assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10); - assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10); - assertEquals(96.07590211815305, circle.getX(), 1.0e-10); - assertEquals(48.13516790438953, circle.getY(), 1.0e-10); + public void testControlParameters() throws EstimationException { + Circle circle = new Circle(98.680, 47.345); + circle.addPoint( 30.0, 68.0); + circle.addPoint( 50.0, -6.0); + circle.addPoint(110.0, -20.0); + circle.addPoint( 35.0, 15.0); + circle.addPoint( 45.0, 97.0); + checkEstimate(circle, 100.0, 1000, 1.0e-10, 1.0e-10, 1.0e-10, false); + checkEstimate(circle, 1.0e-12, 10, 1.0e-20, 1.0e-20, 1.0e-20, true); } + private void checkEstimate(EstimationProblem problem, + double initialStepBoundFactor, int maxCostEval, + double costRelativeTolerance, double parRelativeTolerance, + double orthoTolerance, boolean shouldFail) { + try { + LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator(); + estimator.setInitialStepBoundFactor(initialStepBoundFactor); + estimator.setMaxCostEval(maxCostEval); + estimator.setCostRelativeTolerance(costRelativeTolerance); + estimator.setParRelativeTolerance(parRelativeTolerance); + estimator.setOrthoTolerance(orthoTolerance); + estimator.estimate(problem); + assertTrue(! shouldFail); + } catch (EstimationException ee) { + System.out.println(ee.getClass().getName() + " " + ee.getMessage()); + assertTrue(shouldFail); + } catch (Exception e) { + fail("wrong exception type caught"); + } + } + + public void testCircleFitting() throws EstimationException { + Circle circle = new Circle(98.680, 47.345); + circle.addPoint( 30.0, 68.0); + circle.addPoint( 50.0, -6.0); + circle.addPoint(110.0, -20.0); + circle.addPoint( 35.0, 15.0); + circle.addPoint( 45.0, 97.0); + LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator(); + estimator.estimate(circle); + assertTrue(estimator.getCostEvaluations() < 10); + assertTrue(estimator.getJacobianEvaluations() < 10); + double rms = estimator.getRMS(circle); + assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10); + assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10); + assertEquals(96.07590211815305, circle.getX(), 1.0e-10); + assertEquals(48.13516790438953, circle.getY(), 1.0e-10); + } + public void testCircleFittingBadInit() throws EstimationException { Circle circle = new Circle(-12, -12); double[][] points = new double[][] {