【问题标题】:How to retrieve value of constraint from Pyomo如何从 Pyomo 检索约束值
【发布时间】:2018-06-05 15:05:33
【问题描述】:

假设我有一个在我的抽象模型中以下列方式定义的约束:

def flow_constraint(model, t, b):
flows = sum(
            sum( (model.Factor[area_from, t, b] - model.Factor[area_to, t, b]) 
                * model.flow[area_from, area_to, t] for (area_to) in get_border(area_from))
                    for area_from in model.Areas)
return flows <= model.RAM[t, b] 

model.flow_constraint = Constraint(model.BranchesIndex, rule = flow_constraint)

有没有办法直接从模型中检索这个约束的值?

【问题讨论】:

    标签: pyomo


    【解决方案1】:

    约束值仅在具体实例的上下文中才有意义。对于抽象模型,约束已声明,但未定义。也就是说,容器(如 Constraint)已被声明为存在,但它们是空的。一旦有了具体实例(在 Abstract 模型上调用 create_instance() 或直接创建 ConcreteModel 之后),就有几个选项。

    您的约束实际上是一组约束(由model.BranchesIndex 索引)。您可以显示索引约束中所有约束的值:

    # (assuming m is a concrete instance from create_instance(), or a ConcreteModel)
    m.flow_constraint.display()
    

    您可以通过lowerbodyupper属性获取单个约束的数值。例如:

    print("%s <= %s <= %s" % ( 
        value(m.flow_constraint[t,b].lower),
        value(m.flow_constraint[t,b].body),
        value(m.flow_constraint[t,b].upper) ))
    

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2021-03-06
      • 2017-08-24
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      相关资源
      最近更新 更多