User Tools

Site Tools


Function value threshold in SciPy's minimize

I was minimizing a color difference function using scipy.optimize.minimize. The problem I had was that the solver wouldn't stop until the gradient (the rate of change) was small enough. This is sensible in most applications, but for me this meant wasting time refining the solution until the color difference was orders of magnitude smaller than any perceptible error. In practice most iterations were unnecessary and avoiding them would save a lot of time.

So how does one tell SciPy to finish as soon as the function value is small enough? Here's what I've come up with:

class StopMinimizationEarly(Exception):
    def __init__(self, x, fun):
        self.x =  x = fun
def error_function(x, ...):
    if fun <= acceptable_fun:
        raise StopMinimizationEarly(x, fun)
    return fun

Simply throw an exception in the minimized function and then catch it like this:

    result = scipy.optimize.minimize(error_function, ...)
    x, fun = result.x,
except StopMinimizationEarly as error:
    x, fun = error.x,

As far as I know, you can't directly tell SciPy to stop once a function value threshold is reached. This could be a decent idea for a feature request, but I'm not sure how many people would actually ever need this. For the time being, this is the best approach to the problem, as far as I know.

gsoc/scipy_minimize_value_threshold.txt · Last modified: 2020/07/23 10:37 (external edit)