Restrict domain of a Gurobi variable to a specific set of integer values

96 views Asked by At

I am defining a decision variable in Gurobi as follows:

x = model.addVar(vtype=GRB.INTEGER, lb=0, ub=24)

However, instead of putting a lower and upper bound, I want the domain of the variable to be limited to only [0, 4, 8, 12, 16, 20, 24]. Since I know these are the only possible values my variable can take.

Can I limit the domain to the above specific set, rather than a general lower and upper bound.

2

There are 2 answers

1
Bhartendu Awasthi On

Not sure if we can directly define integer variables over a specific discrete set of values. However, one can add binary variables, and a couple of constraints to restrict the values that an integer variable can take:

0*bool_1 + 4*bool_2 + 8*bool_3 + 12*bool_4 + 16*bool_5 + 20*bool_6 +  24*bool_7 <= x
bool_1 + bool_2 + bool_3 + bool_4 + bool_5 + bool_6 + bool_7 <= 1
where bool_1 ... are binary variables
import gurobipy as gp
from gurobipy import GRB

model = gp.Model()

domain = [0, 4, 8, 12, 16, 20, 24]

bin_vars = model.addVars(len(domain), vtype = GRB.BINARY)

x = model.addVar(vtype=GRB.INTEGER)
model.addConstr(gp.quicksum(i * j for i, j in zip(bin_vars.values(), domain)) == x)
model.addConstr(bin_vars.sum() <= 1)

model.setObjective(x, sense = GRB.MAXIMIZE)

model.optimize()
1
Reinderien On

This is pretty easily possible but only with two variables. Let xi be an integer variable such that 0 <= xi <= 24/4, let x be a continuous, unbounded variable, and then add a constraint so that x = 4*xi.