3

When using optimization algorithms in Python such as scipy.optimize.minimize() from the Scipy library, one can specify the maximum number of iterations via the optional argument "maxiter". However, I would rather like to restrict the total computation time of this algorithm, since I would like to use it in a time-critical application. The number of iterations is secondary.

Is there a way to restrict the maximum computation time in the Scipy optimization functions? If this means that the result is less accurate, that's fine.

Ethunxxx
  • 1,229
  • 4
  • 16
  • 34
  • And what do you want to happen when the computation time is used up? Return the current state of calculation or just give up in total? – MooingRawr Apr 25 '17 at 15:05
  • I would like to get the current state regardless of how good it is at that point in time. – Ethunxxx Apr 25 '17 at 15:18
  • If you can estimate how long the optimizer will take per iteration (based on the size of the dataset) then you could specify the max number of iterations. It would require a bit of testing but with a few reference points at different sizes you should be able to get a good enough estimate. – TLOwater Apr 25 '17 at 15:38
  • 2
    First, do you know how to track time for your own code? Some of those `scipy` functions have a `callback` hook. I seen it used to display/store intermediate results, but may be it can used to quit the loop. If so, you track time in that function. – hpaulj Apr 25 '17 at 15:42
  • I'm not aware if `scipy.optimize`'s routines are safe in this regard. A partial result may not be just "less accurate," in fact it may be utter garbage. A better approach might be looking for heuristics for the initial guess and [pre-conditioning](https://en.wikipedia.org/wiki/Preconditioning). – Cong Ma Apr 26 '17 at 05:40
  • @hpaulj - from the docs, it looks like a `callback` hook is supported, but it doesn't appear that there is a mechanism for passing back a "stop iterating" value, or raising an exception that would break out of the simulation loop. You would have to do something underhanded, like having the callback look up at the locals in the parent frame, find the "all done" variable in the caller, and set it to True. – PaulMcG May 24 '17 at 12:08
  • I was thinking more in terms of saving current calculation info, and raising an exception. – hpaulj May 24 '17 at 12:40
  • If an exception from the `callback` is too crude of an exit, you may need to write a `custom` method. Most defined methods have a `while` loop testing `gtol` and `maxiter`. `callback` is called toward the end of that loop. Do your own time test during that loop. – hpaulj May 24 '17 at 17:20
  • Callback example: https://stackoverflow.com/questions/16739065/how-to-display-progress-of-scipy-optimize-function – hpaulj May 24 '17 at 17:38

0 Answers0