简体   繁体   English

如何在pyomo中将Integral定义为目标函数?

[英]How to define an Integral as an objective function in pyomo?

I want to be able to define an integral in pyomo as part of an objective function. 我希望能够在pyomo定义一个积分作为目标函数的一部分。

I cannot figure out what kind of expression is needed for the integral. 我无法弄清楚积分需要什么样的表达式。
Here's my best guess: 这是我最好的猜测:

model = ConcreteModel()
model.t = ContinuousSet(bounds = (0,1))
model.y = Var(model.t)
model.dydt = DerivativeVar(model.y, wrt=(model.t))

def myintegral(model,i):
    return model.dydt[i]

model.n = Integral(model.t, wrt=model.t, rule=myintegral)  # this line is the trouble

def myobjective(model):
     return model.n

model.obj = Objective(rule=myobjective)

The error is: TypeError: A callable type that is not a Pyomo expression can not be used to initialize an Expression object. Use 'rule' to initalize with function types. 错误是: TypeError: A callable type that is not a Pyomo expression can not be used to initialize an Expression object. Use 'rule' to initalize with function types. TypeError: A callable type that is not a Pyomo expression can not be used to initialize an Expression object. Use 'rule' to initalize with function types.

But, I don't understand why the expression inside of the integral is a problem, since these variables seem to be totally indexable by the index model.t : 但是,我不明白为什么整数内部的表达式是一个问题,因为这些变量似乎完全可以被索引model.t索引。

# but, this is totally fine:  
print model.dydt[0]
print model.dydt[1]

Am I misunderstanding something about this? 我误解了这件事吗?

Here are some resources that I consulted thus far: 以下是我咨询过的一些资源:

https://groups.google.com/forum/#!topic/pyomo-forum/6RhEXEMDTPc https://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_parameters https://projects.coin-or.org/Coopr/browser/pyomo/trunk/examples/dae/Heat_Conduction.py?rev=9315 https://groups.google.com/forum/#!topic/pyomo-forum/6RhEXEMDTPc https://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_parameters https://projects.coin- or.org/Coopr/browser/pyomo/trunk/examples/dae/Heat_Conduction.py?rev=9315

I'm open to suggestions/links about other resources about pyomo . 我对有关pyomo其他资源的建议/链接pyomo

Gabe is right, this is indeed a bug in the Integral class and it has been fixed on the github repository. Gabe是对的,这确实是Integral类中的一个错误,它已在github存储库中修复。 One other error in your example model is the specification of the Objective component. 示例模型中的另一个错误是Objective组件的规范。 You should be using the 'rule' keyword instead of 'expr' 你应该使用'rule'关键字而不是'expr'

def myobjective(model):
     return model.n
model.obj = Objective(rule=myobjective)

Also, I want to reiterate something mentioned in the online documentation for pyomo.dae. 另外,我想重申pyomo.dae在线文档中提到的内容。 The Integral component is a prototype and not fully developed. Integral组件是原型,尚未完全开发。 I do not recommend using it for complex integrals or models that require high accuracy solutions. 我不建议将其用于需要高精度解决方案的复杂积分或模型。 The Integral class uses the trapezoid rule for numerical integration. Integral类使用梯形规则进行数值积分。 I would recommend converting any integrals in your problem to differential equations and solving them using the provided automatic discretization transformations. 我建议将问题中的任何积分转换为微分方程,并使用提供的自动离散化变换求解它们。

This looks like a bug. 这看起来像一个bug。 You should open up a ticket here: https://github.com/Pyomo/pyomo/issues 你应该在这里打开一张票: https//github.com/Pyomo/pyomo/issues

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

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