# How do I get scipy.minimize to terminate below a certain loss threshold?

I was looking at the scipy.minimize documentation to see if I could find a way to terminate optimization when the loss gets below some cut-off, and I couldn't see an obvious way to do it.

The callback option only lets me pass it the xk, but I need to pass additional parameters to evaluate my loss.

Any thoughts on a straightforward way to do this?

• I think they have tolerances for the vector and the function. – nicoguaro May 18 at 3:09
• I tried them, I think those tolerances are for the difference in loss between steps for some number of steps, rather than the actual value of the loss. – user49404 May 18 at 3:10
• Yes, you're right. The callback options seems to see the way to go, maybe some of the solvers let you to pass a function value. – nicoguaro May 18 at 3:20
• I went ahead and just modified the scipy optimize.py source to do what I wanted. <_<. The callback doesn't work for me here because I need to pass additional parameters that scipy isn't allowing. – user49404 May 18 at 3:28

## 1 Answer

So I went ahead and just modified the scipy optimize.py source to do what I wanted. I assume this is not best practices, but... moving on. So I used the inspect module to figure out where the corresponding functions are. So for the Powell optimization routine, I found:

def _minimize_powell


on line 2509 of scipy/optimize/optimize.py. Then I went to line 2593, where I noticed a couple of termination conditions, and I added my own termination condition:

        if fval < 0.1:
break


which now terminates optimization and returns the optimization struct as desired when this condition is met.