if (!getNextPosition(function, estimate, data)) {
if (isConverged(data)) {
return data.getX(); // this can happen if the starting position is the root
}
throw new MathException("Cannot work with this starting position. Please choose another point");
}
int count = 0;
int jacReconCount = 1;
while (!isConverged(data)) {
// Want to reset the Jacobian every so often even if backtracking is working
if ((jacReconCount) % FULL_RECALC_FREQ == 0) {
estimate = _initializationFunction.getInitializedMatrix(jacobianFunction, data.getX());
jacReconCount = 1;
} else {
estimate = _updateFunction.getUpdatedMatrix(jacobianFunction, data.getX(), data.getDeltaX(), data.getDeltaY(), estimate);
jacReconCount++;
}
// if backtracking fails, could be that Jacobian estimate has drifted too far
if (!getNextPosition(function, estimate, data)) {
estimate = _initializationFunction.getInitializedMatrix(jacobianFunction, data.getX());
jacReconCount = 1;
if (!getNextPosition(function, estimate, data)) {
if (isConverged(data)) {
return data.getX(); //non-standard exit. Cannot find an improvement from this position, so provided we are close enough to the root, exit.
}
String msg = "Failed to converge in backtracking, even after a Jacobian recalculation." + getErrorMessage(data, jacobianFunction);
s_logger.info(msg);
throw new MathException(msg);
}
}
count++;
if (count > _maxSteps) {
throw new MathException("Failed to converge - maximum iterations of " + _maxSteps + " reached." + getErrorMessage(data, jacobianFunction));
}
}
return data.getX();
}