我正在使用pyfmi在Python中加载Modelica生成的模型。加载模型后,我要执行优化和参数估计。问题在于,对于每个参数估计(优化迭代),通常需要加载FMU,它需要大约300-400次信号传输,但由于二进制加载错误而无法收敛,我应该在哪里寻找灵魂?欢迎任何提示。
def fun2optim(theta):## Funtion to optimize with the initial guess of paramameter values theta
model = load_fmu("MOdel_0IV_0curves.fmu")## LOAD THE FMU
res = model.simulate(input=foo(theta),final_time=1)
results_VV=np.array([]) ###SAVE THE OUTPUT IN ARRAY
for i in range(200,400):
out=(res[output_IV[i]])
results=out[0::5] #Dymola FMU has 5 same IV curve points
results_VV=np.append(results_VV,results)
return(results_VV)
def RMSE (theta): ## results_V are the ideal values
tt=sum(np.sqrt((fun2optim(theta)-results_V)**2).mean())
return(tt)
from scipy import optimize
res11=optimize.minimize(RMSE,thetaInit,method='nelder-mead', options={'xtol': 1e-4, 'disp': True})
50至60次发信号后出现错误:
FMUException: Error loading the binary. Could not load the DLL: A dynamic link library (DLL) initialization routine failed.
答案 0 :(得分:4)
在使用Dymola FMU之前,我也遇到过类似的问题,我最大的猜测是某些东西没有正确卸载,最终导致了问题。
我建议将您的代码更改为:
model = load_fmu("MOdel_0IV_0curves.fmu")## LOAD THE FMU
def fun2optim(theta):## Funtion to optimize with the initial guess of paramameter values theta
global model
model.reset()
res = model.simulate(input=foo(theta),final_time=1)
results_VV=np.array([]) ###SAVE THE OUTPUT IN ARRAY
for i in range(200,400):
out=(res[output_IV[i]])
results=out[0::5] #Dymola FMU has 5 same IV curve points
results_VV=np.append(results_VV,results)
return(results_VV)
通过这种方式,您不必每次都重新加载FMU(只需重新设置FMU),这也会提高性能。