我正在 pyomo 中建立一個優化模型,并一直面臨這個我無法解決的錯誤。這是發生錯誤的部分:
model.ct2demand = ConstraintList()
for n in model.N:
for s in model.S:
for t in model.T:
for p in model.P:
lhs = model.f[p,t,s,n]*1000
rhs = model.y[p,t,s,n] model.sales[p,t,s,n] model.error[p,t,s,n]
model.ct2demand.add (lhs == rhs)
變數 f 和 error 是多維引數(非負實數),我將其作為 numpy 陣列輸入,并認為根據我的研究導致了這個問題,但我還沒有真正弄清楚為什么。y 和銷售額是決策變數。以下是錯誤訊息:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21076/2031668078.py in <module>
422 for p in model.P:
423 lhs = model.f[p,t,s,n]
--> 424 rhs = model.y[p,t,s,n] model.sales[p,t,s,n] model.error[p,t,s,n]
425 model.ct2demand.add (lhs == rhs)
426
pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.NumericValue.__add__()
pyomo\core\expr\numeric_expr.pyx in pyomo.core.expr.numeric_expr._generate_sum_expression()
pyomo\core\expr\numeric_expr.pyx in pyomo.core.expr.numeric_expr.SumExpression.add()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我做了一些研究,但仍然沒有真正了解是什么導致了這里的問題。任何指導將不勝感激!
uj5u.com熱心網友回復:
這就是正在發生的事情。當你初始化你的引數時,你沒有傳入一個鍵:值對(就像一個字典),所以pyomo只是將引數中的每個元素初始化為整個陣列,這顯然不是你想要的。
大多數時候,索引集不僅僅是計數數字,所以這并不會經常彈出,因為如果你的索引是 {'A', 'B', 'C', ...} 那么它就變成了清楚的是,您必須傳入一個鍵:值映射才能理順這一點。
這里有幾行來演示我在說什么。底線:numpy跳出你的模型,為你想在引數中使用的東西制作一個字典。(這也可以通過 w/ 來完成pandas,但這不是必需的)。如果您想復制/復制/實驗,請在末尾列出命令串列。
In [1]: import pyomo.environ as pyo
In [2]: import numpy as np
In [3]: np_data = np.array([[1, 2], [3, 4]])
In [4]: m = pyo.ConcreteModel()
In [5]: m.S = pyo.Set(initialize=range(2))
In [6]: m.T = pyo.Set(initialize=range(2))
In [7]: # let's make a parameter from the data...
In [8]: m.p = pyo.Param(m.S, m.T, initialize=np_data)
WARNING: DEPRECATED: The default domain for Param objects is 'Any'. However,
we will be changing that default to 'Reals' in the future. If you really
intend the domain of this Param (p) to be 'Any', you can suppress this
warning by explicitly specifying 'within=Any' to the Param constructor.
(deprecated in 5.6.9, will be removed in 6.0) (called from
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-
packages/pyomo/core/base/indexed_component.py:665)
In [9]: # note we get this odd warning about an "any" type. This is trigge
...: red by fact that we are initializing EACH item to the whole array
In [10]: m.p[0,1]
Out[10]:
array([[1, 2],
[3, 4]])
In [11]: m.p[1,1]
Out[11]:
array([[1, 2],
[3, 4]])
In [12]: # not good! pyomo wants a key:value pair
In [13]: my_better_data = {(0, 0) : 1,
...: (0, 1) : 2,
...: (1, 0) : 3,
...: (1, 1) : 4}
In [14]: m.p2 = pyo.Param(m.S, m.T, initialize=my_better_data)
In [15]: m.p2[0,1]
Out[15]: 2
In [16]: %hist
import pyomo.environ as pyo
import numpy as np
np_data = np.array([[1, 2], [3, 4]])
m = pyo.ConcreteModel()
m.S = pyo.Set(initialize=range(2))
m.T = pyo.Set(initialize=range(2))
# let's make a parameter from the data...
m.p = pyo.Param(m.S, m.T, initialize=np_data)
# note we get this odd warning about an "any" type. This is triggered by fact that we are initializing EACH item to the whole array
m.p[0,1]
m.p[1,1]
# not good! pyomo wants a key:value pair
my_better_data = {(0, 0) : 1,
(0, 1) : 2,
(1, 0) : 3,
(1, 1) : 4}
m.p2 = pyo.Param(m.S, m.T, initialize=my_better_data)
m.p2[0,1]
%hist
In [17]:
編輯以顯示多維度 np 陣列到 dict 的轉換
np.array只有在索引都非常容易排列到等效集的特殊情況下, 您才能將回傳轉換為字典。只需通過采樣一些值或完全列印出引數來仔細檢查索引是否按照您的喜好進行model.p.pprint()
In [30]: data = list(range(12)) # some fake data
In [31]: data_np = np.array(data)
In [32]: data_np = data_np.reshape(3,2,2)
In [33]: data_np
Out[33]:
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])
In [34]: data_dict = dict(np.ndenumerate(data_np))
In [35]: data_dict
Out[35]:
{(0, 0, 0): 0,
(0, 0, 1): 1,
(0, 1, 0): 2,
(0, 1, 1): 3,
(1, 0, 0): 4,
(1, 0, 1): 5,
(1, 1, 0): 6,
(1, 1, 1): 7,
(2, 0, 0): 8,
(2, 0, 1): 9,
(2, 1, 0): 10,
(2, 1, 1): 11}
In [36]:
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436260.html
上一篇:谷歌colab記憶體問題
下一篇:如何比較前一行的值的百分比差異
