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 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1573308 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a6f9630666
commit
e2dc384d7b
|
@ -506,7 +506,7 @@ public class LevenbergMarquardtOptimizer implements LeastSquaresOptimizer {
|
||||||
|
|
||||||
// tests for convergence.
|
// tests for convergence.
|
||||||
if (checker != null && checker.converged(iterationCounter.getCount(), previous, current)) {
|
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 {
|
} else {
|
||||||
// failed iteration, reset the previous values
|
// failed iteration, reset the previous values
|
||||||
|
@ -527,7 +527,7 @@ public class LevenbergMarquardtOptimizer implements LeastSquaresOptimizer {
|
||||||
preRed <= costRelativeTolerance &&
|
preRed <= costRelativeTolerance &&
|
||||||
ratio <= 2.0) ||
|
ratio <= 2.0) ||
|
||||||
delta <= parRelativeTolerance * xNorm) {
|
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
|
// tests for termination and stringent tolerances
|
||||||
|
|
|
@ -22,11 +22,13 @@ import org.apache.commons.math3.analysis.MultivariateVectorFunction;
|
||||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||||
import org.apache.commons.math3.exception.TooManyEvaluationsException;
|
import org.apache.commons.math3.exception.TooManyEvaluationsException;
|
||||||
import org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum;
|
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.geometry.euclidean.twod.Vector2D;
|
||||||
import org.apache.commons.math3.linear.DiagonalMatrix;
|
import org.apache.commons.math3.linear.DiagonalMatrix;
|
||||||
import org.apache.commons.math3.linear.RealMatrix;
|
import org.apache.commons.math3.linear.RealMatrix;
|
||||||
import org.apache.commons.math3.linear.RealVector;
|
import org.apache.commons.math3.linear.RealVector;
|
||||||
import org.apache.commons.math3.linear.SingularMatrixException;
|
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.FastMath;
|
||||||
import org.apache.commons.math3.util.Precision;
|
import org.apache.commons.math3.util.Precision;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
@ -35,6 +37,8 @@ import org.junit.Test;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Some of the unit tests are re-implementations of the MINPACK <a
|
* <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
|
* href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
|
||||||
|
@ -264,6 +268,27 @@ public class LevenbergMarquardtOptimizerTest
|
||||||
Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
|
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
|
//TODO delete or use
|
||||||
private static class QuadraticProblem {
|
private static class QuadraticProblem {
|
||||||
private List<Double> x;
|
private List<Double> x;
|
||||||
|
|
Loading…
Reference in New Issue