mardi 11 août 2020

How to use if conditions which contain variables in Pyomo constraint

I am trying to add a simple constraint to my Pyomo model:

maximum{x[1]-a,b}+maximum{x[2]-a,b} >= c

where a,b,c are given predefined parameters and x is the decision variable indexed by set model.I which is initialize to [1,2].

In order to achieve this, I tried the following method with no success:

from pyomo.environ import *
from pyomo.opt import SolverFactory

from pyomo.environ import *
model = AbstractModel()
model.I = Set()  

model.a = Param()
model.b = Param()
model.c = Param()

model.x = Var(model.I)
def my_rule(model):
    total = 0
    for i in model.I:
        val = model.x[i] - model.a
        if val < model.b:
            val = model.b
        total = total + val            
    return total >= model.c
model.my_constraint = Constraint(rule=my_rule)

data = {None:{
    'I': {None: [1,2]},
    'a': {None: 3},
    'b': {None: 5},
    'c': {None: 4}}
}
real_model = model.create_instance(data)

The error given is: WARNING: DEPRECATED: Chained inequalities are deprecated. Use the inequality() function to express ranged inequality expressions. ERROR: Rule failed when generating expression for constraint my_constraint: TypeError: Attempting to form a compound inequality with two lower bounds

        The inequality expression:
            x[1] - 3  <  5.0
        contains non-constant terms (variables) that were evaluated in an
        unexpected Boolean context at
          File '<ipython-input-11-e3a6c44c5b8e>', line 14:
    if val < model.b:

        Evaluating Pyomo variables in a Boolean context, e.g.
            if expression <= 5:
        is generally invalid.  If you want to obtain the Boolean value of
        the expression based on the current variable values, explicitly
        evaluate the expression using the value() function:
            if value(expression) <= 5:
        or
            if value(expression <= 5):

Could any one help me with this? Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire