【问题标题】:How to check which are the indexes of a pyomo variable如何检查 pyomo 变量的索引
【发布时间】:2020-08-17 08:59:28
【问题描述】:

解决我的 pyomo 模型后,我迭代变量以对数据做一些事情。现在我正在尝试根据 pyomo 集的变量索引来编写代码。我正在寻找一种方法来执行以下操作:

model = ConcreteModel()
model.I = Set()
model.p = Var(model.I)

#objective, etc.
...
# solve model
...

for v in instance.component_objects(pyo.Var, active=True):
# Now the next line is what I try to achieve:
    used_sets = v.get_sets()
    if model.I in used_sets:
        # Do stuff

有没有简单的方法可以做到这一点?感谢您的帮助!

【问题讨论】:

    标签: python pyomo


    【解决方案1】:

    下面的一些变体可能会起作用。您可以向Var 询问索引集的名称。请注意,二维索引在内部构造为虚拟集。不确定是否有办法将其展开为原生组件。 (请参阅下面的m.display() 结果。)

    我会说你的结构看起来很奇怪。如果你正在制作一个具体的模型,你已经知道变量......所以测试它们以查看它们的索引是多少似乎有点循环。也许有更简单的选择。

    # accessing indexing sets...
    
    from pyomo.environ import *
    m = ConcreteModel()
    m.I = Set(initialize=[1,2,3])
    m.J = Set(initialize=list('abc'))
    m.X = Var(m.I)
    m.Y = Var(m.J)
    m.Z = Var(m.J, m.I)
    for v in m.component_objects(Var):
        my_index_name = v.index_set().getname()
        if my_index_name == 'I':
            print(f'variable {v.getname()} is indexed by I')
    
    
    m.display()
    

    输出:

    variable X is indexed by I
    Model unknown
    
      Variables:
        X : Size=3, Index=I
            Key : Lower : Value : Upper : Fixed : Stale : Domain
              1 :  None :  None :  None : False :  True :  Reals
              2 :  None :  None :  None : False :  True :  Reals
              3 :  None :  None :  None : False :  True :  Reals
        Y : Size=3, Index=J
            Key : Lower : Value : Upper : Fixed : Stale : Domain
              a :  None :  None :  None : False :  True :  Reals
              b :  None :  None :  None : False :  True :  Reals
              c :  None :  None :  None : False :  True :  Reals
        Z : Size=9, Index=Z_index
            Key      : Lower : Value : Upper : Fixed : Stale : Domain
            ('a', 1) :  None :  None :  None : False :  True :  Reals
            ('a', 2) :  None :  None :  None : False :  True :  Reals
            ('a', 3) :  None :  None :  None : False :  True :  Reals
            ('b', 1) :  None :  None :  None : False :  True :  Reals
            ('b', 2) :  None :  None :  None : False :  True :  Reals
            ('b', 3) :  None :  None :  None : False :  True :  Reals
            ('c', 1) :  None :  None :  None : False :  True :  Reals
            ('c', 2) :  None :  None :  None : False :  True :  Reals
            ('c', 3) :  None :  None :  None : False :  True :  Reals
    
      Objectives:
        None
    
      Constraints:
        None
    

    【讨论】:

    • 感谢您的建议!您已经描述了问题。我的大多数变量都有多个索引。因此,我需要找到一种方法来放松它们。确实,这个问题可能看起来不是很直观,但是如果您添加更多变量(或约束),这应该会促进以后的工作。否则我会做一个硬编码的字典,但我不太喜欢这个选项。
    【解决方案2】:

    我今天需要做类似的事情。我得到了一些工作,但这取决于一个“私有”属性,这当然不是理想的。

    from pyomo.environ import *
    
    m = ConcreteModel()
    m.I = Set(initialize=[1, 2, 3])
    m.J = Set(initialize=list('abc'))
    m.X = Var(m.I)
    m.Y = Var(m.J)
    m.Z = Var(m.J, m.I)
    
    for v in m.component_objects(Var):
        if v.index_set()._implicit_subsets is None:
            index_set = v.index_set()
            index_set_name = index_set.name
            print('{} is indexed in {}'.format(v.name, index_set_name))
            print('{} indexed in I? {}'.format(v.name, index_set is m.I))
        else:
            index_sets = v.index_set()._implicit_subsets
            index_sets_names = [index_set.name for index_set in index_sets]
            print('{} is multi-indexed in {}'.format(v.name, ', '.join(index_sets_names)))
            print('{} indexed in I? {}'.format(v.name, m.I in index_sets))
    

    输出:

    X 在 I 中被索引 X 索引在 I?真的 Y 在 J 中编入索引 Y 索引在 I?错误的 Z 在 J 中是多索引的,I Z 索引在 I?真的

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 2018-11-10
      相关资源
      最近更新 更多