Home > Software engineering >  Gurobipy Optimization: Constraint to make variable value to be greater than 100 OR equal to 0
Gurobipy Optimization: Constraint to make variable value to be greater than 100 OR equal to 0

Time:11-10

I need to tell Gurobi that the variable can take two possible values: 0 or any value greater than 100. As I need it to test other values ​​greater than 100, I cannot set this as a binary variable. Basically the solution must be x = 0 or x> = 100

I tried:

model.addConstrs (x [t, m] >= 100 for t in T for m in M)
model.addConstrs (x [t, m] < 1 for t in T for m in M)

But this wouldn't work (even if no errors were reported) because I let x be less than 0, which is not possible in my case and also because the logic just doesn't make sense. How can I adequately represent my case on Gurobipy?

Some help? Thanks!

CodePudding user response:

No need to do this with a constraint: this is known as a semicontinuous variable. Set the lower bound (LB attribute) for x to 100 and set the variable type (VType attribute) to 'S' (or 'N' if x should also be integer).

CodePudding user response:

You can use two dummy variables to carry out the checks and then use a or constraint to make sure one of them is true. Suppose you use the dummy variables a and b and you use a result variable y for the or statement. In the end just add a constraint stating that y should be true everywhere. Small code snippet below:

for t in T:
    for m in M:
        model.addGenConstrIndicator(a[t, m], 1, x[t, m], GRB.EQUAL, 0)
        model.addGenConstrIndicator(b[t, m], 1, x[t, m], GRB.GREATER_EQUAL, 100)
        model.addGenConstrOr(y[t, m], [a[t, m], b[t, m]])
        model.addConstrs(y[t, m] == 1)
  • Related