commons-commits mailing list archives

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

URL: http://svn.apache.org/r1569350
Log:
Make infinite loop in GaussNewton explicit.

The main loop in GN could only be ended by convergence or exception. I made that
explicit with while(true) and removed an unreachable MathInternalError.

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

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java?rev=1569350&r1=1569349&r2=1569350&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java
Tue Feb 18 14:32:28 2014
@@ -17,7 +17,6 @@
 package org.apache.commons.math3.fitting.leastsquares;
 
 import org.apache.commons.math3.exception.ConvergenceException;
-import org.apache.commons.math3.exception.MathInternalError;
 import org.apache.commons.math3.exception.NullArgumentException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
 import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
@@ -111,7 +110,7 @@ public class GaussNewtonOptimizer implem
 
         // iterate until convergence is reached
         PointVectorValuePair current = null;
-        for (boolean converged = false; !converged; ) {
+        while (true) {
             iterationCounter.incrementCount();
 
             // evaluate the objective function and its jacobian
@@ -124,6 +123,16 @@ public class GaussNewtonOptimizer implem
             final RealMatrix weightedJacobian = value.computeJacobian();
             current = new PointVectorValuePair(currentPoint, currentObjective);
 
+            // Check convergence.
+            if (previous != null) {
+                if (checker.converged(iterationCounter.getCount(), previous, current)) {
+                    return new OptimumImpl(
+                            value,
+                            evaluationCounter.getCount(),
+                            iterationCounter.getCount());
+                }
+            }
+
             // build the linear problem
             final double[] b = new double[nC];
             final double[][] a = new double[nC][nC];
@@ -148,17 +157,6 @@ public class GaussNewtonOptimizer implem
                 }
             }
 
-            // Check convergence.
-            if (previous != null) {
-                converged = checker.converged(iterationCounter.getCount(), previous, current);
-                if (converged) {
-                    return new OptimumImpl(
-                            value,
-                            evaluationCounter.getCount(),
-                            iterationCounter.getCount());
-                }
-            }
-
             try {
                 // solve the linearized least squares problem
                 RealMatrix mA = new BlockRealMatrix(a);
@@ -174,8 +172,6 @@ public class GaussNewtonOptimizer implem
                 throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
             }
         }
-        // Must never happen.
-        throw new MathInternalError();
     }
 
     @Override



Mime
View raw message