diff --git a/src/java/org/apache/commons/math/MessagesResources_fr.java b/src/java/org/apache/commons/math/MessagesResources_fr.java index 6492e4c98..4855b960c 100644 --- a/src/java/org/apache/commons/math/MessagesResources_fr.java +++ b/src/java/org/apache/commons/math/MessagesResources_fr.java @@ -118,8 +118,7 @@ public class MessagesResources_fr { "equals vertices {0} and {1} in simplex configuration", "sommets {0} et {1} \u00e9gaux dans la configuration du simplex" }, - // org.apache.commons.math.optimization.direct.DirectSearchOptimizer - // org.apache.commons.math.optimization.general.AbstractLeastSquaresOptimizer + // org.apache.commons.math.estimation.AbstractEstimation { "maximal number of evaluations exceeded ({0})", "nombre maximal d''\u00e9valuations d\u00e9pass\u00e9 ({0})" }, diff --git a/src/java/org/apache/commons/math/optimization/MultiStartScalarDifferentiableOptimizer.java b/src/java/org/apache/commons/math/optimization/MultiStartScalarDifferentiableOptimizer.java index 7ebd4db20..5aa7d2d9f 100644 --- a/src/java/org/apache/commons/math/optimization/MultiStartScalarDifferentiableOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/MultiStartScalarDifferentiableOptimizer.java @@ -38,16 +38,22 @@ import org.apache.commons.math.random.RandomVectorGenerator; public class MultiStartScalarDifferentiableOptimizer implements ScalarDifferentiableOptimizer { /** Serializable version identifier. */ - private static final long serialVersionUID = 9008747186334431824L; + private static final long serialVersionUID = 6185821146433609962L; /** Underlying classical optimizer. */ private final ScalarDifferentiableOptimizer optimizer; + /** Maximal number of iterations allowed. */ + private int maxIterations; + + /** Number of iterations already performed for all starts. */ + private int totalIterations; + /** Number of evaluations already performed for all starts. */ private int totalEvaluations; - /** Maximal number of evaluations allowed. */ - private int maxEvaluations; + /** Number of gradient evaluations already performed for all starts. */ + private int totalGradientEvaluations; /** Number of starts to go. */ private int starts; @@ -69,12 +75,14 @@ public class MultiStartScalarDifferentiableOptimizer implements ScalarDifferenti public MultiStartScalarDifferentiableOptimizer(final ScalarDifferentiableOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { - this.optimizer = optimizer; - this.totalEvaluations = 0; - this.maxEvaluations = Integer.MAX_VALUE; - this.starts = starts; - this.generator = generator; - this.optima = null; + this.optimizer = optimizer; + this.maxIterations = Integer.MAX_VALUE; + this.totalIterations = 0; + this.totalEvaluations = 0; + this.totalGradientEvaluations = 0; + this.starts = starts; + this.generator = generator; + this.optima = null; } /** Get all the optima found during the last call to {@link @@ -110,19 +118,29 @@ public class MultiStartScalarDifferentiableOptimizer implements ScalarDifferenti return (ScalarPointValuePair[]) optima.clone(); } + /** {@inheritDoc} */ + public void setMaxIterations(int maxIterations) { + this.maxIterations = maxIterations; + } + + /** {@inheritDoc} */ + public int getMaxIterations() { + return maxIterations; + } + + /** {@inheritDoc} */ + public int getIterations() { + return totalIterations; + } + /** {@inheritDoc} */ public int getEvaluations() { return totalEvaluations; } /** {@inheritDoc} */ - public void setMaxEvaluations(int maxEvaluations) { - this.maxEvaluations = maxEvaluations; - } - - /** {@inheritDoc} */ - public int getMaxEvaluations() { - return maxEvaluations; + public int getGradientEvaluations() { + return totalGradientEvaluations; } /** {@inheritDoc} */ @@ -141,14 +159,16 @@ public class MultiStartScalarDifferentiableOptimizer implements ScalarDifferenti double[] startPoint) throws ObjectiveException, OptimizationException { - optima = new ScalarPointValuePair[starts]; - totalEvaluations = 0; + optima = new ScalarPointValuePair[starts]; + totalIterations = 0; + totalEvaluations = 0; + totalGradientEvaluations = 0; // multi-start loop for (int i = 0; i < starts; ++i) { try { - optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations); + optimizer.setMaxIterations(maxIterations - totalIterations); optima[i] = optimizer.optimize(f, goalType, (i == 0) ? startPoint : generator.nextVector()); } catch (ObjectiveException obe) { @@ -157,7 +177,9 @@ public class MultiStartScalarDifferentiableOptimizer implements ScalarDifferenti optima[i] = null; } - totalEvaluations += optimizer.getEvaluations(); + totalIterations += optimizer.getIterations(); + totalEvaluations += optimizer.getEvaluations(); + totalGradientEvaluations += optimizer.getGradientEvaluations(); } diff --git a/src/java/org/apache/commons/math/optimization/MultiStartScalarOptimizer.java b/src/java/org/apache/commons/math/optimization/MultiStartScalarOptimizer.java index 234367c65..4c6043337 100644 --- a/src/java/org/apache/commons/math/optimization/MultiStartScalarOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/MultiStartScalarOptimizer.java @@ -38,17 +38,20 @@ import org.apache.commons.math.random.RandomVectorGenerator; public class MultiStartScalarOptimizer implements ScalarOptimizer { /** Serializable version identifier. */ - private static final long serialVersionUID = 6648351778723282863L; + private static final long serialVersionUID = -7333253288301713047L; /** Underlying classical optimizer. */ private final ScalarOptimizer optimizer; + /** Maximal number of iterations allowed. */ + private int maxIterations; + + /** Number of iterations already performed for all starts. */ + private int totalIterations; + /** Number of evaluations already performed for all starts. */ private int totalEvaluations; - /** Maximal number of evaluations allowed. */ - private int maxEvaluations; - /** Number of starts to go. */ private int starts; @@ -69,8 +72,9 @@ public class MultiStartScalarOptimizer implements ScalarOptimizer { public MultiStartScalarOptimizer(final ScalarOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { this.optimizer = optimizer; + this.maxIterations = Integer.MAX_VALUE; + this.totalIterations = 0; this.totalEvaluations = 0; - this.maxEvaluations = Integer.MAX_VALUE; this.starts = starts; this.generator = generator; this.optima = null; @@ -109,21 +113,26 @@ public class MultiStartScalarOptimizer implements ScalarOptimizer { return (ScalarPointValuePair[]) optima.clone(); } + /** {@inheritDoc} */ + public void setMaxIterations(int maxIterations) { + this.maxIterations = maxIterations; + } + + /** {@inheritDoc} */ + public int getMaxIterations() { + return maxIterations; + } + + /** {@inheritDoc} */ + public int getIterations() { + return totalIterations; + } + /** {@inheritDoc} */ public int getEvaluations() { return totalEvaluations; } - /** {@inheritDoc} */ - public void setMaxEvaluations(int maxEvaluations) { - this.maxEvaluations = maxEvaluations; - } - - /** {@inheritDoc} */ - public int getMaxEvaluations() { - return maxEvaluations; - } - /** {@inheritDoc} */ public void setConvergenceChecker(ScalarConvergenceChecker checker) { optimizer.setConvergenceChecker(checker); @@ -140,14 +149,15 @@ public class MultiStartScalarOptimizer implements ScalarOptimizer { double[] startPoint) throws ObjectiveException, OptimizationException { - optima = new ScalarPointValuePair[starts]; + optima = new ScalarPointValuePair[starts]; + totalIterations = 0; totalEvaluations = 0; // multi-start loop for (int i = 0; i < starts; ++i) { try { - optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations); + optimizer.setMaxIterations(maxIterations - totalIterations); optima[i] = optimizer.optimize(f, goalType, (i == 0) ? startPoint : generator.nextVector()); } catch (ObjectiveException obe) { @@ -156,6 +166,7 @@ public class MultiStartScalarOptimizer implements ScalarOptimizer { optima[i] = null; } + totalIterations += optimizer.getIterations(); totalEvaluations += optimizer.getEvaluations(); } diff --git a/src/java/org/apache/commons/math/optimization/MultiStartVectorialDifferentiableOptimizer.java b/src/java/org/apache/commons/math/optimization/MultiStartVectorialDifferentiableOptimizer.java index b92fb9c0e..31fe83cb7 100644 --- a/src/java/org/apache/commons/math/optimization/MultiStartVectorialDifferentiableOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/MultiStartVectorialDifferentiableOptimizer.java @@ -38,20 +38,23 @@ import org.apache.commons.math.random.RandomVectorGenerator; public class MultiStartVectorialDifferentiableOptimizer implements VectorialDifferentiableOptimizer { /** Serializable version identifier. */ - private static final long serialVersionUID = -6671992853686531955L; + private static final long serialVersionUID = -9109278856437190136L; /** Underlying classical optimizer. */ private final VectorialDifferentiableOptimizer optimizer; + /** Maximal number of iterations allowed. */ + private int maxIterations; + + /** Number of iterations already performed for all starts. */ + private int totalIterations; + /** Number of evaluations already performed for all starts. */ private int totalEvaluations; /** Number of jacobian evaluations already performed for all starts. */ private int totalJacobianEvaluations; - /** Maximal number of evaluations allowed. */ - private int maxEvaluations; - /** Number of starts to go. */ private int starts; @@ -73,9 +76,10 @@ public class MultiStartVectorialDifferentiableOptimizer implements VectorialDiff final int starts, final RandomVectorGenerator generator) { this.optimizer = optimizer; + this.maxIterations = Integer.MAX_VALUE; + this.totalIterations = 0; this.totalEvaluations = 0; this.totalJacobianEvaluations = 0; - this.maxEvaluations = Integer.MAX_VALUE; this.starts = starts; this.generator = generator; this.optima = null; @@ -114,6 +118,21 @@ public class MultiStartVectorialDifferentiableOptimizer implements VectorialDiff return (VectorialPointValuePair[]) optima.clone(); } + /** {@inheritDoc} */ + public void setMaxIterations(int maxIterations) { + this.maxIterations = maxIterations; + } + + /** {@inheritDoc} */ + public int getMaxIterations() { + return maxIterations; + } + + /** {@inheritDoc} */ + public int getIterations() { + return totalIterations; + } + /** {@inheritDoc} */ public int getEvaluations() { return totalEvaluations; @@ -124,16 +143,6 @@ public class MultiStartVectorialDifferentiableOptimizer implements VectorialDiff return totalJacobianEvaluations; } - /** {@inheritDoc} */ - public void setMaxEvaluations(int maxEvaluations) { - this.maxEvaluations = maxEvaluations; - } - - /** {@inheritDoc} */ - public int getMaxEvaluations() { - return maxEvaluations; - } - /** {@inheritDoc} */ public void setConvergenceChecker(VectorialConvergenceChecker checker) { optimizer.setConvergenceChecker(checker); @@ -150,15 +159,16 @@ public class MultiStartVectorialDifferentiableOptimizer implements VectorialDiff final double[] startPoint) throws ObjectiveException, OptimizationException, IllegalArgumentException { - optima = new VectorialPointValuePair[starts]; - totalEvaluations = 0; + optima = new VectorialPointValuePair[starts]; + totalIterations = 0; + totalEvaluations = 0; totalJacobianEvaluations = 0; // multi-start loop for (int i = 0; i < starts; ++i) { try { - optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations); + optimizer.setMaxIterations(maxIterations - totalIterations); optima[i] = optimizer.optimize(f, target, weights, (i == 0) ? startPoint : generator.nextVector()); } catch (ObjectiveException obe) { @@ -167,6 +177,7 @@ public class MultiStartVectorialDifferentiableOptimizer implements VectorialDiff optima[i] = null; } + totalIterations += optimizer.getIterations(); totalEvaluations += optimizer.getEvaluations(); totalJacobianEvaluations += optimizer.getJacobianEvaluations(); diff --git a/src/java/org/apache/commons/math/optimization/OptimizationException.java b/src/java/org/apache/commons/math/optimization/OptimizationException.java index bf93cfec7..e44aa003d 100644 --- a/src/java/org/apache/commons/math/optimization/OptimizationException.java +++ b/src/java/org/apache/commons/math/optimization/OptimizationException.java @@ -30,7 +30,7 @@ import org.apache.commons.math.ConvergenceException; public class OptimizationException extends ConvergenceException { /** Serializable version identifier. */ - private static final long serialVersionUID = -781139167958631145L; + private static final long serialVersionUID = -357696069587075016L; /** * Simple constructor. @@ -42,4 +42,12 @@ public class OptimizationException extends ConvergenceException { super(specifier, parts); } + /** + * Create an exception with a given root cause. + * @param cause the exception or error that caused this exception to be thrown + */ + public OptimizationException(Throwable cause) { + super(cause); + } + } diff --git a/src/java/org/apache/commons/math/optimization/ScalarDifferentiableOptimizer.java b/src/java/org/apache/commons/math/optimization/ScalarDifferentiableOptimizer.java index b7cb6a36f..9385c30a1 100644 --- a/src/java/org/apache/commons/math/optimization/ScalarDifferentiableOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/ScalarDifferentiableOptimizer.java @@ -29,38 +29,45 @@ import java.io.Serializable; */ public interface ScalarDifferentiableOptimizer extends Serializable { - /** Set the maximal number of objective function calls. - *
- * The number of objective function calls may be checked after a few - * related calls have been made. This implies that in some cases this number may - * be exceeded by a few units, depending on the dimension of the problem and kind - * of optimizer. - *
- * @param maxEvaluations maximal number of function calls - * . + /** Set the maximal number of iterations of the algorithm. + * @param maxIterations maximal number of function calls */ - void setMaxEvaluations(int maxEvaluations); + void setMaxIterations(int maxIterations); - /** Get the maximal number of objective function calls. - *- * The number of objective function calls may be checked after a few - * related calls have been made. This implies that in some cases this number may - * be exceeded by a few units, depending on the dimension of the problem and kind - * of optimizer. - *
- * @return maximal number of function calls + /** Get the maximal number of iterations of the algorithm. + * @return maximal number of iterations */ - int getMaxEvaluations(); + int getMaxIterations(); + + /** Get the number of iterations realized by the algorithm. + *+ * The number of evaluations corresponds to the last call to the + * {@link #optimize(ScalarDifferentiableObjectiveFunction, GoalType, double[]) optimize} + * method. It is 0 if the method has not been called yet. + *
+ * @return number of iterations + */ + int getIterations(); /** Get the number of evaluations of the objective function. *- * The number of evaluation correspond to the last call to the - * {@link #optimize(ScalarObjectiveFunction, GoalType, double[]) optimize} + * The number of evaluations corresponds to the last call to the + * {@link #optimize(ScalarDifferentiableObjectiveFunction, GoalType, double[]) optimize} * method. It is 0 if the method has not been called yet. *
* @return number of evaluations of the objective function */ - int getEvaluations(); + int getEvaluations(); + + /** Get the number of evaluations of the objective function gradient. + *+ * The number of evaluations corresponds to the last call to the + * {@link #optimize(ScalarDifferentiableObjectiveFunction, GoalType, double[]) optimize} + * method. It is 0 if the method has not been called yet. + *
+ * @return number of evaluations of the objective function gradient + */ + int getGradientEvaluations(); /** Set the convergence checker. * @param checker object to use to check for convergence diff --git a/src/java/org/apache/commons/math/optimization/ScalarOptimizer.java b/src/java/org/apache/commons/math/optimization/ScalarOptimizer.java index 95f821164..63f1292e3 100644 --- a/src/java/org/apache/commons/math/optimization/ScalarOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/ScalarOptimizer.java @@ -29,38 +29,35 @@ import java.io.Serializable; */ public interface ScalarOptimizer extends Serializable { - /** Set the maximal number of objective function calls. - *- * The number of objective function calls may be checked after a few - * related calls have been made. This implies that in some cases this number may - * be exceeded by a few units, depending on the dimension of the problem and kind - * of optimizer. - *
- * @param maxEvaluations maximal number of function calls - * . + /** Set the maximal number of iterations of the algorithm. + * @param maxIterations maximal number of function calls */ - void setMaxEvaluations(int maxEvaluations); + void setMaxIterations(int maxIterations); - /** Get the maximal number of objective function calls. - *- * The number of objective function calls may be checked after a few - * related calls have been made. This implies that in some cases this number may - * be exceeded by a few units, depending on the dimension of the problem and kind - * of optimizer. - *
- * @return maximal number of function calls + /** Get the maximal number of iterations of the algorithm. + * @return maximal number of iterations */ - int getMaxEvaluations(); + int getMaxIterations(); + + /** Get the number of iterations realized by the algorithm. + *+ * The number of evaluations corresponds to the last call to the + * {@link #optimize(ScalarObjectiveFunction, GoalType, double[]) optimize} + * method. It is 0 if the method has not been called yet. + *
+ * @return number of iterations + */ + int getIterations(); /** Get the number of evaluations of the objective function. *- * The number of evaluation correspond to the last call to the + * The number of evaluations corresponds to the last call to the * {@link #optimize(ScalarObjectiveFunction, GoalType, double[]) optimize} * method. It is 0 if the method has not been called yet. *
* @return number of evaluations of the objective function */ - int getEvaluations(); + int getEvaluations(); /** Set the convergence checker. * @param checker object to use to check for convergence diff --git a/src/java/org/apache/commons/math/optimization/VectorialDifferentiableOptimizer.java b/src/java/org/apache/commons/math/optimization/VectorialDifferentiableOptimizer.java index 73d27584e..788e079c7 100644 --- a/src/java/org/apache/commons/math/optimization/VectorialDifferentiableOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/VectorialDifferentiableOptimizer.java @@ -29,28 +29,21 @@ import java.io.Serializable; */ public interface VectorialDifferentiableOptimizer extends Serializable { - /** Set the maximal number of objective function calls. - *- * The number of objective function calls may be checked after a few - * related calls have been made. This implies that in some cases this number may - * be exceeded by a few units, depending on the dimension of the problem and kind - * of optimizer. - *
- * @param maxEvaluations maximal number of function calls + /** Set the maximal number of iterations of the algorithm. + * @param maxIterations maximal number of function calls * . */ - void setMaxEvaluations(int maxEvaluations); + void setMaxIterations(int maxIterations); - /** Get the maximal number of objective function calls. - *- * The number of objective function calls may be checked after a few - * related calls have been made. This implies that in some cases this number may - * be exceeded by a few units, depending on the dimension of the problem and kind - * of optimizer. - *
- * @return maximal number of function calls + /** Get the maximal number of iterations of the algorithm. + * @return maximal number of iterations */ - int getMaxEvaluations(); + int getMaxIterations(); + + /** Get the number of iterations realized by the algorithm. + * @return number of iterations + */ + int getIterations(); /** Get the number of evaluations of the objective function. *diff --git a/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java b/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java index d69c856b3..c29c09e03 100644 --- a/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.MaxIterationsExceededException; import org.apache.commons.math.optimization.ScalarConvergenceChecker; import org.apache.commons.math.optimization.GoalType; import org.apache.commons.math.optimization.ObjectiveException; @@ -28,7 +29,7 @@ import org.apache.commons.math.optimization.ScalarObjectiveFunction; import org.apache.commons.math.optimization.OptimizationException; import org.apache.commons.math.optimization.ScalarOptimizer; import org.apache.commons.math.optimization.ScalarPointValuePair; -import org.apache.commons.math.optimization.SimpleValueChecker; +import org.apache.commons.math.optimization.SimpleScalarValueChecker; /** * This class implements simplex-based direct search optimization @@ -65,7 +66,7 @@ import org.apache.commons.math.optimization.SimpleValueChecker; * will occur.
* *If {@link #setConvergenceChecker(ScalarConvergenceChecker)} is not called, - * a default {@link SimpleValueChecker} is used.
+ * a default {@link SimpleScalarValueChecker} is used. * *Convergence is checked by providing the worst points of * previous and current simplex to the convergence checker, not the best ones.
@@ -95,11 +96,14 @@ public abstract class DirectSearchOptimizer implements ScalarOptimizer { /** Convergence checker. */ private ScalarConvergenceChecker checker; - /** Number of evaluations already performed for the current start. */ - private int evaluations; + /** Maximal number of iterations allowed. */ + private int maxIterations; - /** Maximal number of evaluations allowed. */ - private int maxEvaluations; + /** Number of iterations already performed. */ + private int iterations; + + /** Number of evaluations already performed. */ + private int evaluations; /** Start simplex configuration. */ private double[][] startConfiguration; @@ -107,8 +111,8 @@ public abstract class DirectSearchOptimizer implements ScalarOptimizer { /** Simple constructor. */ protected DirectSearchOptimizer() { - setConvergenceChecker(new SimpleValueChecker()); - setMaxEvaluations(Integer.MAX_VALUE); + setConvergenceChecker(new SimpleScalarValueChecker()); + setMaxIterations(Integer.MAX_VALUE); } /** Set start configuration for simplex. @@ -208,13 +212,23 @@ public abstract class DirectSearchOptimizer implements ScalarOptimizer { } /** {@inheritDoc} */ - public void setMaxEvaluations(int maxEvaluations) { - this.maxEvaluations = maxEvaluations; + public void setMaxIterations(int maxIterations) { + this.maxIterations = maxIterations; } /** {@inheritDoc} */ - public int getMaxEvaluations() { - return maxEvaluations; + public int getMaxIterations() { + return maxIterations; + } + + /** {@inheritDoc} */ + public int getIterations() { + return iterations; + } + + /** {@inheritDoc} */ + public int getEvaluations() { + return evaluations; } /** {@inheritDoc} */ @@ -229,7 +243,7 @@ public abstract class DirectSearchOptimizer implements ScalarOptimizer { /** {@inheritDoc} */ public ScalarPointValuePair optimize(final ScalarObjectiveFunction f, final GoalType goalType, - final double[] startPoint) + final double[] startPoint) throws ObjectiveException, OptimizationException, IllegalArgumentException { if (startConfiguration == null) { @@ -251,15 +265,15 @@ public abstract class DirectSearchOptimizer implements ScalarOptimizer { }; // initialize search + iterations = 0; evaluations = 0; buildSimplex(startPoint); evaluateSimplex(comparator); ScalarPointValuePair[] previous = new ScalarPointValuePair[simplex.length]; - int iterations = 0; - while (evaluations <= maxEvaluations) { + while (true) { - if (++iterations > 1) { + if (iterations > 0) { boolean converged = true; for (int i = 0; i < simplex.length; ++i) { converged &= checker.converged(iterations, previous[i], simplex[i]); @@ -276,22 +290,24 @@ public abstract class DirectSearchOptimizer implements ScalarOptimizer { } - throw new OptimizationException( - "maximal number of evaluations exceeded ({0})", - evaluations); - } - /** {@inheritDoc} */ - public int getEvaluations() { - return evaluations; + /** Increment the iterations counter by 1. + * @exception OptimizationException if the maximal number + * of iterations is exceeded + */ + protected void incrementIterationsCounter() + throws OptimizationException { + if (++iterations > maxIterations) { + throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); + } } /** Compute the next simplex of the algorithm. * @param comparator comparator to use to sort simplex vertices from best to worst * @exception ObjectiveException if the function cannot be evaluated at * some point - * @exception OptimizationException if the algorithm failed to converge + * @exception OptimizationException if the algorithm fails to converge * @exception IllegalArgumentException if the start point dimension is wrong */ protected abstract void iterateSimplex(final ComparatorThe convergence check is set to a {@link SimpleVectorialValueChecker} * and the maximal number of evaluation is set to - * {@link AbstractLeastSquaresOptimizer#DEFAULT_MAX_EVALUATIONS}. + * {@link AbstractLeastSquaresOptimizer#DEFAULT_MAX_ITERATIONS}. * @param useLU if true, the normal equations will be solved using LU * decomposition, otherwise they will be solved using QR decomposition */ @@ -67,8 +67,9 @@ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer { // iterate until convergence is reached VectorialPointValuePair current = null; - boolean converged = false; - for (int iteration = 1; ! converged; ++iteration) { + for (boolean converged = false; !converged;) { + + incrementIterationsCounter(); // evaluate the objective function and its jacobian VectorialPointValuePair previous = current; @@ -122,7 +123,7 @@ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer { // check convergence if (previous != null) { - converged = checker.converged(++iteration, previous, current); + converged = checker.converged(getIterations(), previous, current); } } diff --git a/src/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizer.java b/src/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizer.java index 232901aaf..d36a5be0b 100644 --- a/src/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizer.java @@ -146,7 +146,7 @@ public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer { *
The default values for the algorithm settings are: *