问题
I have a problem that I don't know how to add indicator constraints in pulp
. Can anyone help me?
For example: I have a decision variable x[(i,j)]
, LpBinary and a continuous variable u[i]
When x[(i,j)]
equals 1
, then u[i] + q[j] == u[j]
(q
is just the demand of customers) Thank you for your helping.
回答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.
来源:https://stackoverflow.com/questions/54898987/how-to-add-indicator-constraints-in-pulp-python