I'm trying to use the callback
argument in the scipy.optimize.differential_evolution method to stop the minimization process after some specified max_time
parameter.
The code below apparently reaches the callbackF()
function once and then never again.
What am I doing wrong here?
from scipy.optimize import differential_evolution as DE
import time as t
# Max time in seconds
max_time = 3.
def callbackF(start_t):
time_elapsed = t.time() - start_t
if time_elapsed > max_time:
print("Stop")
return True
def DEdist(model):
res = (1. - model[0])**2 + 100.0 * (model[1] - model[0]**2)**2 + \
(1. - model[1])**2 + 100.0 * (model[2] - model[1]**2)**2
return res
start_t = t.time()
bounds = [[0., 10.], [0., 10.], [0., 10.], [0., 10.]]
result = DE(
DEdist, bounds, popsize=100, maxiter=1500, callback=callbackF(start_t))
print(t.time() - start_t)