我在 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。
我需要知道我是否可以以某種方式調整此代碼以使其正常作業。我已閱讀檔案。在第 339 到 340 頁中,它談到了插值,但我對 Pyomo 或 Python 不夠熟悉,無法理解它,所以如果有人幫助我,我將非常感激。
uj5u.com熱心網友回復:
來自這個和你另一個執行緒的很多資訊。我會盡量處理好一切。
問題不在于轉換pyomo.IndexedVar為float,因為當您在pyomo模型中創建變數時,您可以將其作為NonNegativeReal. 這個問題是非數字的隱式轉換(在你的代碼中,m.lammda[i,1]是一個類,不完全是一個數字。即使它有一個浮點值)與float陳述句。
正如您在另一個執行緒中的評論中所述,當您嘗試使用pyomo.environ.IndexedVarinto 擬合(插值)函式 Rbf 時,您會引發TypeError錯誤,試圖將 an 轉換IndexedVar為float(在 Rbf 函式內部)。要評估您可以使用model.m.lammda[i,1]()的值,因為呼叫該類會回傳該值,但這仍然無濟于事,因為肯定會引發另一個錯誤
我建議您訪問這篇文章以獲取有關使用外部函式作為pyomo約束或目標函式的潛在困難的更多資訊
Pyomo有一個分段函式庫,您可以在其中對線性(pyo.environ層也包含在 中pyo.environ.Piecewise)或多線性插值進行建模,但是您需要使用內核層,它使用 scipy 為插值生成 Dlunay,您可以使用help(pyomo.kernel.piecewise_nd). 這肯定對你有用,但它需要一些作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320762.html
