Home > Software design >  dependent binary variables in Pyomo
dependent binary variables in Pyomo

Time:08-09

I'm having problems formulating a constraint in Pyomo where I want to determine a dependency between two variables. In my model I have 3 binary variables in total:

model.x = pyo.Var(model.M, domain=pyo.Binary)
model.y = pyo.Var(model.M, domain=pyo.Binary)
model.z = pyo.Var(model.M, domain=pyo.Binary)

My objective function with the parameter model.l would be:

obj_expr = sum(model.l[i] * model.x[i]   model.l[i] * model.y[i] for i in model.M)
model.obj = pyo.Objective(sense=pyo.maximize, expr=obj_expr)

I would like model.z to be the opposite of model.x, so that a 1 in model.x[i] would be a 0 in model.z[i].

I tried to do this with the following constraint:

    def dependent_var_con(model, i):
        lhs = model.z[i]
        rhs = model.x[i]
        return lhs != rhs 
    model.con = pyo.Constraint(model.M, expr=dependent_var_con)

Unfortunately this doesn't work and I'm getting the following error:

Cannot convert non-constant Pyomo expression (z[0] == x[0]) to bool. This error is usually caused by using a Var, unit, or mutable Param in a Boolean context such as an "if" statement, or when checking container membership or equality.

Does anyone have an idea how to deal with this issue? Thank you!

CodePudding user response:

You cannot use the "not equal" logical operator in a linear program. All you get are inequalities and equal. :)

However, if you just want to invert a binary variable, it is pretty straightforward with a little algebra. In your case:

z[i] == 1 - x[i]

pop that in a constraint for all i

  • Related