Pyomo ValueError: Invalid constraint expression

Pyomo ValueError: Invalid constraint expression

我正在编写一个具有以下形式约束的 pyomo 整数程序:

def example_rule(model, j, t):
    value = sum(model.x[j,i]*(util[i][t]) for i in model.F)
    return 0 <= value <= 1
model.onelateral = Constraint(model.L, model.T, rule=example_rule)

util[i][t] 是一个包含始终为 0 或 1 的值的字典。 model.x[j,i] 是二元决策变量。

有时当我 运行 我的模型时,它工作正常。但是,有时当我在 util[i][t] 中更改 dimensions/values 时,它会抛出此错误:

ERROR: Constructing component 'example' from data=None failed:
    ValueError: Invalid constraint expression. The constraint expression resolved to a trivial Boolean (True) instead of a Pyomo object. Please modify your rule to return Constraint.Feasible instead of True.

Error thrown for Constraint 'example[L01]'

关于为什么它决定不喜欢 util[i][t] 的输入值,我找不到任何一致性。那里从来没有任何空值。

如果我 运行 没有这个约束的模型,它一直都可以正常工作。

我也试过将约束写成以下形式:

def example_rule(model,j):
    a = 0
    for t in model.T:
        n = 0
        for i in model.F:
            if model.x[j,i].value == 1:
                a = model.x[j,i] * util[i][t]
            if a == 1:
                n = n + a
    return 0 <= n <= 1
model.example = Constraint(model.L, rule=example_rule)

但是我得到了同样的错误信息。

我看过这里:https://groups.google.com/forum/#!msg/pyomo-forum/hZXDf7xGnTI/_aiAUN5IwgQJ 但这对我没有帮助。

我已经尝试过使用 cbc 和 glpk 求解器。我正在使用 Pyomo V5.2,Python V3.6.1.

提前感谢您的帮助。

您是否遇到过 util[i][t] 对于特定 t 的所有 i 为零的情况?乘以零的项会自动从表达式中删除,所以我猜你的错误是由 'value' 最终为零的情况引起的,在这种情况下 0 <= 值 <= 1 会 return一个简单的布尔值。

解决此问题的最简单方法是正式将 util 声明为 Param 组件并在声明中添加 mutable=True。这向 Pyomo 发出信号,表明您可以更改 util 参数的值,从而避免自动简化 0 值。

m.util = Param(m.F, m.T, initialize=util_init, mutable=True)

另一种方法是检查 util 的值并在整个列为零时跳过约束

def example_rule(model, j, t):
    if not any(util[i][t] for i in model.F):
        return Constraint.Skip
    temp = sum(model.x[j,i]*(util[i][t]) for i in model.F)
    return 0 <= temp <= 1
model.onelateral = Constraint(model.L, model.T, rule=example_rule)

我通过将我的约束声明更改为此来让它工作:

def example_rule(model,j,t):
    return (0,sum(model.x[j,i]*(util1[i][t]) for i in model.F),1)
model.example = Constraint(model.L, model.T, rule=example_rule)

不幸的是我不知道为什么这个有效而以前的没有!