我可以使用集合列表在 pyomo 中初始化 Set() 吗?换句话说,我想做这样的事情:
from pyomo.environ import *
model = AbstractModel()
a = set([1,2,3])
b = set([4,5,6])
model.c = Set(initialize = [a,b])
instance = model.create_instance()
不幸的是,这给了我一个错误:
ERROR: Constructing component 'a' from data=None failed:
TypeError: Problem inserting set([1, 2, 3]) into set c
还有另一种方法可以达到我所缺少的相同效果吗?
TL;DR:我正在研究网络拦截模型。我的模型集代表网络中的一组路径。我想使用(python)集来存储路径,因为模型约束仅限于可行路径。因此,我需要检查路径中是否有任何边被拦截,并且哈希函数将允许我有效地检查路径上是否发生了拦截边。换句话说,我稍后有一个功能:
def is_feasible(model, path):
return any([edge in path and model.Interdicts[edge].value] for edge in model.edges)
其中 path 是我的 Set 的一个元素,model.Interdicts 是 Var(model.edges, within = binary)
我的后备方案是使用引用外部列表中的路径的索引来初始化我的 Set ,但随后我不得不将我的 pyomo 模型与非模型元素混合以评估模型约束,这是一个真正令人头疼的问题(但后来也是大多数网络拦截建模...)