# HG changeset patch # User Sascha L. Teichmann # Date 1340818173 0 # Node ID 307842cf8d9ee287a4bd54121f1b2ed65e9046e3 # Parent 1c00ed73ccf4b70a31196370b8737b3cc3bfa5fa FixA: Make fitting more tolerant. flys-artifacts/trunk@4819 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 1c00ed73ccf4 -r 307842cf8d9e flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Wed Jun 27 16:41:02 2012 +0000 +++ b/flys-artifacts/ChangeLog Wed Jun 27 17:29:33 2012 +0000 @@ -1,3 +1,9 @@ +2012-06-27 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/model/fixings/Fitting.java: + If fitting fails for tolerance of 1e-10 increase it incremental + by factor of 10 to 1e-3 and only bail out if it fails on this level. + 2012-06-27 Sascha L. Teichmann * src/main/java/de/intevation/flys/artifacts/math/fitting/Exp.java: diff -r 1c00ed73ccf4 -r 307842cf8d9e flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/Fitting.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/Fitting.java Wed Jun 27 16:41:02 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/Fitting.java Wed Jun 27 17:29:33 2012 +0000 @@ -22,6 +22,9 @@ import org.apache.log4j.Logger; +import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer; +import org.apache.commons.math.optimization.general.GaussNewtonOptimizer; + public class Fitting { private static Logger log = Logger.getLogger(Fitting.class); @@ -157,24 +160,38 @@ return false; } - LevenbergMarquardtOptimizer lmo = new LevenbergMarquardtOptimizer(); - List inputs = new ArrayList(xs.size()); de.intevation.flys.artifacts.math.Function instance = null; + LevenbergMarquardtOptimizer lmo = null; + for (;;) { - CurveFitter cf = new CurveFitter(lmo); + parameters = null; + for (double tolerance = 1e-10; tolerance < 1e-3; tolerance *= 10d) { - for (int i = 0, N = xs.size(); i < N; ++i) { - cf.addObservedPoint(xs.getQuick(i), ys.getQuick(i)); + lmo = new LevenbergMarquardtOptimizer(); + lmo.setCostRelativeTolerance(tolerance); + lmo.setOrthoTolerance(tolerance); + lmo.setParRelativeTolerance(tolerance); + + CurveFitter cf = new CurveFitter(lmo); + + for (int i = 0, N = xs.size(); i < N; ++i) { + cf.addObservedPoint(xs.getQuick(i), ys.getQuick(i)); + } + + try { + parameters = cf.fit(function, function.getInitialGuess()); + break; + } + catch (MathException me) { + if (log.isDebugEnabled()) { + log.debug("tolerance " + tolerance + " + failed."); + } + } } - - try { - parameters = cf.fit(function, function.getInitialGuess()); - } - catch (MathException me) { - log.warn(me); + if (parameters == null) { return false; }