You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2014/03/02 15:02:50 UTC

svn commit: r1573308 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java

Author: luc
Date: Sun Mar  2 14:02:50 2014
New Revision: 1573308

URL: http://svn.apache.org/r1573308
Log:
Fix switched iterations and evaluations

In LevenbergMarquardtOptimizer the number of iterations and evaluations
was switched in two of the return statements.

Patch provided by Evan Ward.

JIRA: MATH-1106

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java?rev=1573308&r1=1573307&r2=1573308&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java Sun Mar  2 14:02:50 2014
@@ -506,7 +506,7 @@ public class LevenbergMarquardtOptimizer
 
                     // tests for convergence.
                     if (checker != null && checker.converged(iterationCounter.getCount(), previous, current)) {
-                        return new OptimumImpl(current, iterationCounter.getCount(), evaluationCounter.getCount());
+                        return new OptimumImpl(current, evaluationCounter.getCount(), iterationCounter.getCount());
                     }
                 } else {
                     // failed iteration, reset the previous values
@@ -527,7 +527,7 @@ public class LevenbergMarquardtOptimizer
                      preRed <= costRelativeTolerance &&
                      ratio <= 2.0) ||
                     delta <= parRelativeTolerance * xNorm) {
-                    return new OptimumImpl(current, iterationCounter.getCount(), evaluationCounter.getCount());
+                    return new OptimumImpl(current, evaluationCounter.getCount(), iterationCounter.getCount());
                 }
 
                 // tests for termination and stringent tolerances

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=1573308&r1=1573307&r2=1573308&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 Sun Mar  2 14:02:50 2014
@@ -22,11 +22,13 @@ import org.apache.commons.math3.analysis
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.TooManyEvaluationsException;
 import org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum;
+import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
 import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
 import org.apache.commons.math3.linear.DiagonalMatrix;
 import org.apache.commons.math3.linear.RealMatrix;
 import org.apache.commons.math3.linear.RealVector;
 import org.apache.commons.math3.linear.SingularMatrixException;
+import org.apache.commons.math3.optim.ConvergenceChecker;
 import org.apache.commons.math3.util.FastMath;
 import org.apache.commons.math3.util.Precision;
 import org.junit.Assert;
@@ -35,6 +37,8 @@ import org.junit.Test;
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.hamcrest.CoreMatchers.is;
+
 /**
  * <p>Some of the unit tests are re-implementations of the MINPACK <a
  * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
@@ -264,6 +268,27 @@ public class LevenbergMarquardtOptimizer
         Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
     }
 
+    @Test
+    public void testEvaluationCount() {
+        //setup
+        LeastSquaresProblem lsp = new LinearProblem(new double[][] {{1}}, new double[] {1})
+                .getBuilder()
+                .checker(new ConvergenceChecker<Evaluation>() {
+                    public boolean converged(int iteration, Evaluation previous, Evaluation current) {
+                        return true;
+                    }
+                })
+                .build();
+
+        //action
+        Optimum optimum = optimizer.optimize(lsp);
+
+        //verify
+        //check iterations and evaluations are not switched.
+        Assert.assertThat(optimum.getIterations(), is(1));
+        Assert.assertThat(optimum.getEvaluations(), is(2));
+    }
+
     //TODO delete or use
     private static class QuadraticProblem {
         private List<Double> x;