简体   繁体   中英

How to get the last best guess from LeastSquareOptimizer?

Is there a way to get the last best guess of the LeastSquaresOptimizer?

I am using Apache Commons Math to perform a least squares optimization. To do this, I must provide a maxEvaluations() and maxIterations() value. The issue is, if the optimization does not converge before it hits the maximum number of evaulations or iterations it returns an org.apache.commons.math4.exception.TooManyIterationsException: illegal state: maximal count (6,000) exceeded: iterations . If this happens, I would like to see what the last best guess of the optimizer was. How do I do this?

LeastSquaresProblem problem = new LeastSquaresBuilder()
                                     .start(new double[]{0,0,0,1,1,1,0,0,0})
                                     .model(costFunc)
                                     .target(gravity)
                                     .lazyEvaluation(false)
                                     .maxEvaluations(150000)
                                     .maxIterations(6000)
                                     .build();

LeastSquaresOptimizer.Optimum optimum;
try {
    optimum = new LevenbergMarquardtOptimizer()
                      .withCostRelativeTolerance(1.0e-10)
                      .optimize(problem);
} catch (Exception e) {
    throw new Exception(e);
} 

There's probably a better way, but what about storing the best guess manually? The optimizer calls problem::evaluate(RealVector point) repeatedly. When you replace it by your own function, you get the input and output.

This replacement should be rather trivial using

LeastSquaresProblem wrappedProblem = new LeastSquaresAdapter(problem) {
    @Override public LeastSquaresProblem.Evaluation evaluate(RealVector point) {
        LeastSquaresProblem.Evaluation result = super(point);
        ... store the point and the result, if it's an improvement
        return result;
    }
}

I'm afraid, the data is mutable, so you'll need to clone it, so it won't get overwritten.


I may be wrong as I've got no experience with this library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM