简体   繁体   English

Pyomo:DAE参数估计和模型的逐步仿真

[英]Pyomo: DAE parameter estimation and stepwise simulation of model

I started with Pyomo and have some specific questions about the package. 我从Pyomo开始,对软件包有一些特定的问题。 I'm working with DAE-Toolbox and want to use this toolbox for simulation and parameter estimation. 我正在使用DAE-Toolbox,并希望将此工具箱用于仿真和参数估计。 Here my code: 这是我的代码:

DAE-Model-script(from pyomo-examples): DAE-Model-script(来自pyomo-examples):

from pyomo.environ import *
from pyomo.dae import *

model = ConcreteModel()

time_vec = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
model.t = ContinuousSet(initialize= time_vec)

meas_time_vec = [1,2,3,5]
model.MEAS_t = Set(within=model.t,initialize =meas_time_vec)    # Measurement times, must be subset of t

meas_data_vec ={1:0.264,2:0.594,3: 0.801,5: 0.959}
model.x1_meas = Param(model.MEAS_t, initialize=meas_data_vec)

model.x1 = Var(model.t, initialize =0)
model.x2 = Var(model.t, initialize=1)

model.p1 = Var(bounds=(-1.5,1.5))
model.p2 = Var(bounds=(-1.5,1.5))

model.x1dot = DerivativeVar(model.x1,wrt=model.t)
model.x2dot = DerivativeVar(model.x2)

def _init_conditions(model):
    yield model.x1[0] == model.p1
    yield model.x2[0] == model.p2
model.init_conditions = ConstraintList(rule=_init_conditions)

# Alternate way to declare initial conditions
#def _initx1(model):
#   return model.x1[0] == model.p1      
#model.initx1 = Constraint(rule=_initx1)

#def _initx2(model):
#   return model.x2[0] == model.p2
#model.initx2 = Constraint(rule=_initx2)

def _x1dot(model,i):
    return model.x1dot[i] == model.x2[i]
model.x1dotcon = Constraint(model.t, rule=_x1dot)

def _x2dot(model,i):
    return model.x2dot[i] == 1-2*model.x2[i]-model.x1[i]
model.x2dotcon = Constraint(model.t, rule=_x2dot)

def obj(model):
    return sum((model.x1[i]-model.x1_meas[i])**2 for i in model.MEAS_t)
model.obj = Objective(rule=obj)

The run-script: 运行脚本:

rom pyomo.environ import *
from pyomo.dae import *
from Parameter_Estimation2 import model
import copy


#model copy
model_sim_window = copy.deepcopy(model)
model_sim_loop = copy.deepcopy(model)



#(1) Parameter Estimation
discretizer = TransformationFactory('dae.collocation')
discretizer.apply_to(model,nfe=8,ncp=5)
solver=SolverFactory('ipopt')
results = solver.solve(model,tee= True)
p_1 = value(model.p1)
p_2 = value(model.p2)
print('p_1',p_1)
print('p_2',p_2)


#(2) Simulation-window
model_sim_window.obj.deactivate()
model_sim_window.t.value = (0,20)
model_sim_window.p1.value = p_1
model_sim_window.p2.value = p_2
sim = Simulator(model_sim_window, package='casadi')
tsim, profiles = sim.simulate(numpoints=100, integrator='cvodes')


#(3)Simulation-loop
model_sim_loop.obj.deactivate()
model_sim_loop.p1.value = p_1
model_sim_loop.p2.value = p_2
sim = Simulator(model_sim_loop, package='casadi')
x0=[0,0]
result =[x0]
t_vec =[0]

for t in range(0,20):

    model_sim_loop.t.value = (t,t+1)
    tsim, profiles = sim.simulate(numpoints=10, integrator='cvodes',initcon=x0)
    result.append(profiles[-1])
    t_vec.append(tsim[-1])
    x0= profiles[-1]


print('result',result)
print('time',t_vec)

Now, the questions: 现在,问题是:

  1. Is there a way to reuse the model-instance for parameter estimation 有没有一种方法可以重用模型实例进行参数估计
    (comment 1) and for simulation (comment 2)? (评论1)和仿真(评论2)? I solved this problem with a 我用一个解决了这个问题
    "ugly" deepcopy. “丑陋”的深拷贝。

  2. Has pyomo.dae simulator a step method, that can be used for a step wise pyomo.dae模拟器具有逐步方法,可以用于逐步方法
    integration of the dae-system in a loop. dae系统集成在一个循环中。 I want to the change the inputs (measurements, control signals) between the steps without a reinit of my 我想在步骤之间更改输入(测量,控制信号),而无需重新初始化
    model.I know, that cvodes has such a method. 我知道,cvodes有这种方法。

  3. How can pyomo be used for model predictive control. pyomo如何用于模型预测控制。 Are there any 有没有
    examples? 例子? What is a good starting point? 一个好的起点是什么?

Thanks & Bye 谢谢再见

Hendrixon 亨德里克森

1) There is a bug in the Simulator which prevents it from being applied to discretized models so the approach you're using with two models is the way to do it currently (note, you should use model.clone() to copy the model instead of deepcopy). 1)Simulator中存在一个错误,该错误阻止了将其应用于离散化模型,因此您目前在两种模型上使用的方法是目前的处理方式(请注意,您应该使用model.clone()复制模型而不是Deepcopy)。 This bug in the Simulator will be fixed in the next Pyomo release. Simulator中的此错误将在下一个Pyomo版本中修复。

2) See this section from the Simulator documentation which I think is exactly what you're asking for. 2)请参阅模拟器文档中的这一部分 ,我认为正是您所要求的。

3) See this paper on a framework for nonlinear model predictive control built on Pyomo. 3)参见本文关于基于Pyomo的非线性模型预测控制的框架。

Thanks for your response! 感谢您的答复!

1) I solved this problem using an abstract model. 1)我使用抽象模型解决了这个问题。

2) I have read the section from Simulator documentation and I made some tests. 2)我已经阅读了模拟器文档中的这一节,并进行了一些测试。 But I want to implement the pyomo.dae Simulator in a "real-time" application. 但是我想在“实时”应用程序中实现pyomo.dae Simulator。 My workflow: 我的工作流程:

  1. Read measurement data from plant 从工厂读取测量数据

  2. set inputs of model 设置模型输入

  3. do a simulation step 做一个模拟步骤

  4. wait until next data comes in 等到下一个数据输入

  5. set inputs..... 设置输入.....

  6. do a simulation step (start with the states from the last simulation step,no init is needed) 做一个模拟步骤(从最后一个模拟步骤的状态开始,不需要初始化)

  7. ...and so on ...等等

I know that the package pyfmi (from Jmodelica) has such a step method for the Simulator/Integration, they use cvode. 我知道pyfmi软件包(来自Jmodelica)具有用于Simulator / Integration的这种逐步方法,它们使用cvode。 Currently I'm using the IDAS-Solver because I have DAE-System. 目前,由于我拥有DAE系统,因此我正在使用IDAS-Solver。 Has the pyomos IDAS interface such a step-by-step integration method? pyomos IDAS接口是否具有这种逐步集成方法?

3) very interesting! 3)非常有趣!

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM