如何在pyomo中将积分定义为目标函数? [英] How to define an Integral as an objective function in pyomo?

查看:55
本文介绍了如何在pyomo中将积分定义为目标函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将 pyomo 中的积分定义为目标函数的一部分.

I want to be able to define an integral in pyomo as part of an objective function.

我不知道积分需要什么样的表达式.
这是我最好的猜测:

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)

错误是:TypeError: A callable type that not a Pyomo expression can not be used to initialize an Expression object.TypeError: A callable type that not a Pyomo expression can not be used to initialize an Expression object.使用 'rule' 初始化函数类型.

但是,我不明白为什么积分内部的表达式是一个问题,因为这些变量似乎完全可以通过索引 model.t 进行索引:

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:

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

我对此有误解吗?

以下是我目前查阅的一些资源:

Here are some resources that I consulted thus far:

https://groups.google.com/forum/#!主题/pyomo-forum/6RhEXEMDTPchttps://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_参数https://projects.coin-or.org/Coopr/browser/pyomo/trunk/examples/dae/Heat_Conduction.py?rev=9315

我愿意接受有关 pyomo 的其他资源的建议/链接.

I'm open to suggestions/links about other resources about pyomo.

推荐答案

Gabe 说得对,这确实是 Integral 类中的一个 bug,已经在 github 仓库中修复了.示例模型中的另一个错误是目标组件的规范.您应该使用 'rule' 关键字而不是 'expr'

Gabe is right, this is indeed a bug in the Integral class and it has been fixed on the github repository. One other error in your example model is the specification of the Objective component. You should be using the 'rule' keyword instead of 'expr'

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

另外,我想重申一下 pyomo.dae 在线文档中提到的一些内容.Integral 组件是一个原型,尚未完全开发.我不建议将它用于需要高精度解决方案的复杂积分或模型.Integral 类使用梯形规则进行数值积分.我建议您将问题中的任何积分转换为微分方程,然后使用提供的自动离散化转换来求解它们.

Also, I want to reiterate something mentioned in the online documentation for pyomo.dae. The Integral component is a prototype and not fully developed. 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. I would recommend converting any integrals in your problem to differential equations and solving them using the provided automatic discretization transformations.

这篇关于如何在pyomo中将积分定义为目标函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆