How To Add Indicator Constraints In Pulp Python?
Solution 1:
Welcome to SO!
My interpretation of your question is that you have binary variables x[(i,j)]
, and continuos variables u[i]
. When x[(i,j)]==1
then you want to enforce a constraint as follows u[i] + q[j] == u[j]
. If x[(i,j)]==0
then no such constraint is enforced.
This can be done as follows:
for i in set_I:
for j in set_J:
u[j] >= u[i] + q[j] - (1 - x[(i,j)])*M
u[j] <= u[i] + q[j] + (1 - x[(i,j)])*M
Where M
is a value that is a bit bigger than the largest possible range in u[i]
values + the largest possible q[j]
value. To understand why this works consider the two cases, first if x[(i,j)]==1
these constraints become:
u[j] >= u[i] + q[j]
u[j] <= u[i] + q[j]
Which can be abbreviated as: u[j] == u[i] + q[j]
, the constraint you want in the x[(i,j)]==1
case.
In the x[(i,j)]==0
case, these constraints become:
u[j] >= u[i] + q[j] - M
u[j] <= u[i] + q[j] + M
Recall that M is a large number, what we are saying is u[j] >= some_value - large_number
, which provided you choose M
so that its large enough will not have any effect at all (as required). Similarly the constraint u[j] <= some_value + large_number
has not effect provided M
is sufficiently large.
Post a Comment for "How To Add Indicator Constraints In Pulp Python?"