Python中的模拟退火拟合

问题描述

我正在尝试熟悉非线性拟合程序:双重退火。为此,我生成了一些合成数据,并尝试在它们上面拟合一个基本的 Furth 公式,请参阅下面的代码

import numpy as np
from numpy import savetxt
from numpy import genfromtxt
import random
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit
from lmfit import Minimizer,Parameters,report_fit
from scipy.optimize import dual_annealing
def Furth(A,B,a,x):
    model = A*x + B * (np.exp(-a*x)-1) 
    return model 

生成合成数据:

x = np.arange(200)*0.5
x = x[1:]
A =1.88
B = 2.35
a = 5602
y = Furth(A,x) + np.random.randn(x.size)

定义适合的函数

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

    model = A*x + B * (np.exp(-c*x)-1) 
    return model - data
params = Parameters()
params.add('A',min=0,max = 100000)
params.add('B',min=-100,max = 100000)
params.add('c',max = 100000)
from scipy.optimize import dual_annealing
# do fit,here with the default leastsq algorithm

minner = Minimizer(fit_msd2,params,fcn_args=(x,y))
print(minner)
result = minner.minimize(method="dual_annealing")
print(result)
# calculate final result
final = x + result.residual
#print(final)

# write error report
report_fit(result)
fig,ax = plt.subplots(figsize = (10,10))
ax.grid()
ax.set_ylabel('$\Delta_{msd}$(t) [$\mu$m]',fontsize=18)
ax.set_xlabel('Time Lag $\Delta t$ [s]',fontsize=18)
ax.loglog(x,y,label = 'Synthetic data')
ax.loglog(x,final,'r-',linewidth=3,label='Fit')
ax.legend(loc='best',fontsize = 12)

Graphical Fit results

我认为合适的工作不正常,我无法理解是哪些原因。我可以使用任何库或检查路由来评估这一点吗? 如果没有,有人可以建议另一种方法来执行这种模拟退火拟合吗?

提前致谢

解决方法

我最好的猜测是使用优化器的参数。由于您的损失似乎太大,优化器目前过早地接受了解决方案。由于 lmfit 建立在 scipy 之上,因此可以像这样传递优化器的参数:

private JObject AddValue(string propertyName,dynamic propertyValue)
{            
    var payload = new JObject()
    {
        {"success","true"},{propertyName,propertyValue}
    };
    return payload;
}

上面的参数化让优化器更具探索性,尤其是在优化的开始阶段,让它优化的时间更长。可以在 here 中找到 string propertyName = "list"; string[] propertyValue = ["abc","def"]; 参数的完整列表。

这会带来改进吗?

另外,我建议使用你的错误的平方:

ldapsearch -x -D 'cn=ldapadm,dc=sanchez,dc=com' -W                 -b 'cn=Total,cn=Connections,cn=Monitor' -s base '(objectClass=*)' '*' '+'
Enter LDAP Password: