What is a solver friendly way of encoding integer division in MILP programs? Currently I'm using the following encoding (in Gurobi python), which may not be totally correct and I hope not that optimal.
# res is an integer variable in the solver
# val is an integer variable in the solver
# divVal is just a python variable, a constant for solver
offset = 0.999
divRes = val / divVal
model.addConstr(divRes - offset <= res)
model.addConstr(res <= divRes)
Above encoding essentially says that res
should be assigned a value between divRes - offset
and divRes
, since offset
is 0.999, there should be only 1 integer in the range and solver is forced to assign that to res
. Is there a better (faster) way of encoding this?
EDIT: By integer division I mean that the result of division is an integer. If there is any fractional part after the division, I want to discard that and round down the result which will be stored in res
. What I essentially want to do is shift a number by some x
bits. In MILP solver, that boils down to dividing a number by (1 << x)
, but there is some fractional part after the division which I want to get rid of.