Roman Lents
Roman Lents

Reputation: 131

Make a constraint as 2 values in Pyomo

I have an Obective function in Pyomo and I want to make a constraints for decision varibales.

Objective: model.round = Objective(expr = 2.2*model.x1 + 3.6*model.x2 + 1.1*model.x3 + 5.1*model.x4, sense=maximize) And I want a constraint to be a list of two values. For example: model.x1_cons = Constraint(expr = 2.2*model.x1 == [2 or 3]) So I want 2.2*model.x1 to be either 2 or 3 and no values in between. But I cannot get how to make it in Pyomo as there is only equlity or inequality possible.

Upvotes: 0

Views: 675

Answers (2)

Tom Krumpolc
Tom Krumpolc

Reputation: 56

Assuming you have the imports for the rest of your model, you can try the following:

from pyomo.environ import Var, Binary 

y = Var(domain=Binary)  # A binary variable

model.x1_cons = Constraint(expr = 2.2*model.x1 == 2+y)

if y = 1, then Constraint == 3. If y=0, then Constraint == 2

Upvotes: 1

bburnak
bburnak

Reputation: 31

It looks like your problem is not a pyomo issue, but a modeling issue. You need to use binary variables, i.e. a variable should be defined in {0,1}, not [0,1]. Once you have that, you have an option to use big-M relaxation, which would read like this.

-model.y*model.very_large_number <= 2.2*model.x1 - 2 <= model.y*model.very_large_number

-(1-model.y)*model.very_large_number <= 2.2*model.x1 - 3 <= (1-model.y)*model.very_large_number

As you can notice, depending on the value of your binary variable, the constraint can either equal to 2 or 3.

Upvotes: 0

Related Questions