Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyomo ValueError: Invalid constraint expression

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.

like image 746
fishy321 Avatar asked Aug 10 '17 14:08

fishy321


1 Answers

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)
like image 157
Bethany Nicholson Avatar answered Nov 15 '22 03:11

Bethany Nicholson