我在Python中定义了一个插值函数,如下所示:
rbfX = sp.interpolate.Rbf(X2, Y2, Wx2, function='multiquadric')我想在Pyomo代码中使用它,其中m.lammda[i,1]和m.phi[i,1]是飞机在由前面的插值表示的风场中移动的位置。注意,m是一个具体的模型,并且
type(m.lammda)
Out[7]: pyomo.core.base.var.IndexedVar我目前尝试的解决方案是
def Wind_lammda_definition1(model, i):
abcdX=float(m.lammda[i,1])
abcdY=float(m.phi[i,1])
return m.Wind_lammda[i,1] ==rbfX(abcdX, abcdY)
m.Wind_lammda_const1 = Constraint(m.N, rule = Wind_lammda_definition1)因为我想知道轨迹中任何一点的风速。
当我运行这个程序时,我得到了这个错误:
ERROR: Rule failed when generating expression for Constraint
Wind_lammda_const1 with index 0: TypeError: Implicit conversion of Pyomo
NumericValue type `lammda[0,1]' to a float is disabled. This error is
often the result of using Pyomo components as arguments to one of the
Python built-in math module functions when defining expressions. Avoid
this error by using Pyomo-provided math functions.
ERROR: Constructing component 'Wind_lammda_const1' from data=None failed:
TypeError: Implicit conversion of Pyomo NumericValue type `lammda[0,1]' to
a float is disabled. This error is often the result of using Pyomo
components as arguments to one of the Python built-in math module
functions when defining expressions. Avoid this error by using Pyomo-
provided math functions.
Traceback (most recent call last):
File "D:\whatever\node1_test.py", line 530, in <module>
m.Wind_lammda_const1 = Constraint(m.N, rule = Wind_lammda_definition1)
TypeError: Implicit conversion of Pyomo NumericValue type `lammda[0,1]' to a float is
disabled. This error is often the result of using Pyomo components as
arguments to one of the Python built-in math module functions when
defining expressions. Avoid this error by using Pyomo-provided math
functions.如果我调用m.lammda[i,1]和m.phi[i,1]作为参数,而不是abcdX和abcY作为参数,仍然会出现错误。
我需要知道我是否能以某种方式修改这段代码以使其工作。我已经读过documentation了。在第339到340页中,它谈到了插值,但我对Pyomo或Python还不够熟悉,无法理解它,所以,如果有人用它来帮助我,我将非常感激。
发布于 2021-10-14 03:21:27
从这里得到的很多信息和你的另一个帖子。我会尽我所能处理好这一切。
问题不在于将pyomo.IndexedVar转换为float,因为当您在pyomo模型中创建变量时,您可以将其作为NonNegativeReal来执行。这个问题更确切地说是不是数字的隐式转换(在您的代码中,m.lammda[i,1]是一个类,而不完全是数字。即使它具有浮点值)与float语句一起使用。
正如您在另一个线程中声明的in your comment,当您尝试将pyomo.environ.IndexedVar用于拟合(插值)函数Rbf时,在尝试将IndexedVar转换为float (在Rbf函数内部)时,会产生TypeError of error。要计算值,可以使用model.m.lammda[i,1](),因为调用该类将返回值,但这仍然没有帮助,因为肯定会引发另一个错误
我建议您访问this post,获取有关将外部函数用作pyomo约束或目标函数的潜在困难的更多信息
Pyomo有一个Piecewise Function Library,你可以在其中模拟线性(pyo.environ层也在pyo.environ.Piecewise中得到了它)或多线性插值,但你需要使用kernel layer,它使用scipy来生成插值的Dlunay,你可以使用help(pyomo.kernel.piecewise_nd)进行检查。这肯定会对你起作用,但它需要一些工作。
https://stackoverflow.com/questions/69551804
复制相似问题