Hi together,
I am currently trying to use the method='differential_evolution' with the keyword workers, which enables parallelisation using multiple cpu-cores.
With workers=1 everything runs fine.
When i set workers>1 on my windows maschine the prozess just runs forever/freezes, while i can see the number of selected cores on my pc to start to work for a few seconds, but then stop again. I can only end this my manually restarting the kernel.
I also tried the same thing in a colab notebook (as it probably uses some linux system). There i get after some time the error message
"AbortFitException: fit aborted: too many function evaluations 12000".
When i call the differential_evolution method of scipy directly this does not happen and the fit works out fine, also with workers>1.
I set up an example code (based on the differential evolution example from scipy), comparing the two cases:
# scipy directly
import numpy as np
from scipy.optimize import rosen, differential_evolution
bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
if __name__ == "__main__":
result = differential_evolution(rosen, bounds, updating='deferred',workers=2)
result.x, result.fun
->
(array([1., 1., 1., 1., 1.]), 0.0)
# lmfit
import lmfit
def resid(params):
x1 = params['x1'].value
x2 = params['x2'].value
x3 = params['x3'].value
x4 = params['x4'].value
x5 = params['x5'].value
x=np.array([x1,x2,x3,x4,x5])
return rosen(x)
params = lmfit.Parameters()
params.add('x1', 0.5, min=0, max=2.0)
params.add('x2', 1.0, min=0, max=2.0)
params.add('x3', 1.0, min=0, max=2.0)
params.add('x4', 1.5, min=0, max=2.0)
params.add('x5', 1.0, min=0, max=2.0)
if __name__ == "__main__":
o2 = lmfit.minimize(resid, params, args=None, method='differential_evolution', updating='deferred', workers=2)
print("\n\n# Fit using differential_evolution:")
lmfit.report_fit(o2)
-> freezes/runs forever
# expected result/result with workers=1
->
# Fit using differential_evolution:
[[Fit Statistics]]
# fitting method = differential_evolution
# function evals = 12001
# data points = 1
# variables = 5
chi-square = 3.5540e-09
reduced chi-square = 3.5540e-09
Akaike info crit = -9.45518933
Bayesian info crit = -19.4551893
## Warning: uncertainties could not be estimated:
[[Variables]]
x1: 0.99987098 (init = 0.5)
x2: 0.99981640 (init = 1)
x3: 0.99996904 (init = 1)
x4: 0.99925413 (init = 1.5)
x5: 0.99844780 (init = 1)
Thus this code gives sensible results for workers=1, but runs forever/gives the abovementioned error for workers>2.
Any ideas what is generating this behaviour? Do i have to use/pass the workers keyword in a different way within lmfit?
Thanks a lot for the help.
Cheers,
Frederic