我正在处理由多个集合索引的pyomo变量。我已经在一些集合上创建了切片,并且希望使用这些切片来访问变量值,给定沿切片集合的索引。你知道吗
我希望的代码是:
from pyomo.environ import *
m = ConcreteModel()
m.time = Set(initialize=[1,2,3])
m.space = Set(initialize=[1,2,3])
m.comp = Set(initialize=['a','b','c'])
m.v = Var(m.time, m.space, m.comp, initialize=1)
slice = m.v[:, :, 'a']
for x in m.space:
value_list = []
for t in m.time:
value_list.append(slice[t, x].value)
# write value_list to csv file
但这给了我:
>>> value_list
[<pyomo.core.base.indexed_component_slice._IndexedComponent_slice object at 0x7f4db9104a58>, <pyomo.core.base.indexed_component_slice._IndexedComponent_slice object at 0x7f4db9104a58>, <pyomo.core.base.indexed_component_slice._IndexedComponent_slice object at 0x7f4db9104a58>]
而不是像我希望的那样列出一系列的价值观。你知道吗
是否可以仅从通配符索引访问与变量片对应的值?你知道吗
我尝试使用了一些_IndexedComponent_slice的方法,但没有成功。例如:
>>> for item in slice.wildcard_items(): item
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/rparker1/Idaes/pyomo/pyomo/core/base/indexed_component_slice.py", line 194, in <genexpr>
return ((_iter.get_last_index_wildcards(), _) for _ in _iter)
File "/home/rparker1/Idaes/pyomo/pyomo/core/base/indexed_component_slice.py", line 350, in __next__
_comp = _comp.__getitem__( _call[1] )
AttributeError: '_GeneralVarData' object has no attribute '__getitem__'
我希望某些方法能给我一个字典,将通配符索引映射到vardata对象,但找不到。任何帮助找到这样一本字典或其他解决方案是非常感谢。你知道吗
_IndexedComponent_slice
对象有点棘手,因为它们被设计用来处理层次模型。因此,它们更应该被看作是一个特殊的迭代器,而不是字典的视图。特别地,这些“切片状”对象将__getitem__
、__getattr__
和__call__
的分辨率推迟到迭代时间。所以,当你说slice.value
时,属性查找实际上不会发生,直到你在片上迭代。你知道吗获取变量值的最简单方法是迭代切片:
如果您想要一个新的组件,您可以用类似于字典的方式来处理它(就像原始的
Var
),那么您需要使用切片创建一个Reference
组件:这些可以像任何其他组件一样附加到模型,并且(对于常规切片)将采用引用对象的
ctype
(因此在本例中,r
的外观和行为与Var
一样)。你知道吗相关问题 更多 >
编程相关推荐