如何从 Pyomo 检索约束值

How to retrieve value of constraint from Pyomo

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

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)

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

约束值仅在具体实例的上下文中才有意义。对于抽象模型,约束已声明但未定义。也就是说,容器(如约束)已被声明存在,但它们是空的。一旦你有了一个具体的实例(在抽象模型上调用 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) ))