Pyomo ValueError: Invalid constraint expression

感情迁移 提交于 2020-04-11 03:54:27

问题


I am writing a pyomo integer programme with a constraint of the form:

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] is a dict containing values that are always 0 or 1. model.x[j,i] is the binary decision variable.

Sometimes when I run my model, it works fine. However, sometimes when I change the dimensions/values within util[i][t] it throws this error:

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]'

I cannot find any consistency in why it decides it doesn't like the input values for util[i][t]. There are never any null values in there.

If I run the model without this constraint in, it works fine all the time.

I have also tried writing the constraint in the form:

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)

But I get the same error message.

I have looked here: https://groups.google.com/forum/#!msg/pyomo-forum/hZXDf7xGnTI/_aiAUN5IwgQJ But this did not help me.

I have tried this using both cbc and glpk solvers. I am using Pyomo V5.2, Python V3.6.1.

Thank you for your help in advance.


回答1:


Do you have cases where util[i][t] is zero over all i's for a particular t? Terms that are multiplied by zero are automatically removed from the expression so I'm guessing your error is caused by a case where 'value' ends up as zero in which case 0 <= value <= 1 would return a trivial boolean value.

The easiest way to fix this is to formally declare util as a Param component and add mutable=True to the declaration. This signals to Pyomo that you might change the value of the util parameter and therefore avoids the automatic simplification of 0 values.

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

Another way would be to check the values of util and skip the constraint if an entire column is zero

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)



回答2:


I got it to work by changing my constraint declaration to this:

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)

Unfortunately I do not know why this worked and the previous didn't!



来源:https://stackoverflow.com/questions/45616967/pyomo-valueerror-invalid-constraint-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!