namedunframed
namedunframed

Reputation: 31

Dual annealing optimization with Python

I am currently trying to fit a set of data using the class Minimize(). I would like to implement this method with the dual_annealing algorithm but unfortunately I cannot This is the code:

def fit_msd2(params, x, data):
    A = params['A']
    a = params['a']

    model = x + A * (np.exp(-a*x)-1) 
    return model - data
# create a set of Parameters
params = Parameters()
params.add('A', value=527, min=0)
params.add('a', value=0.016, min=0)
from scipy.optimize import dual_annealing
# do fit, here with the default leastsq algorithm
minner = Minimizer(fit_msd2, params, fcn_args=(tL, mean_msd_29))
result = minner.minimize(method = dual_annealing)
# calculate final result
#final = mean_msd_29 + result.residual

# write error report
report_fit(result)

And this is the Attribute error I receive back:

AttributeError                            Traceback (most recent call last)
<ipython-input-531-a8c40dd82948> in <module>
      2 # do fit, here with the default leastsq algorithm
      3 minner = Minimizer(fit_msd2, params, fcn_args=(tL, mean_msd_29))
----> 4 result = minner.minimize(method = dual_annealing)
      5 # calculate final result
      6 final = mean_msd_29 + result.residual

~/opt/anaconda3/lib/python3.8/site-packages/lmfit/minimizer.py in minimize(self, method, params, **kws)
   2260         kwargs.update(kws)
   2261 
-> 2262         user_method = method.lower()
   2263         if user_method.startswith('leasts'):
   2264             function = self.leastsq

AttributeError: 'function' object has no attribute 'lower'

Please, if you can suggest some edit to the code, I'll strongly appreciate it!

Upvotes: 0

Views: 1574

Answers (1)

joni
joni

Reputation: 7157

The method argument of the minimize method is required to be a str, see the docs. Use

result = minner.minimize(method = "dual_annealing")

instead.

Upvotes: 1

Related Questions