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.
- 5,150
- 13
- 26
- 34
- 26
- 5
-
1what happens with `x[(i,j)] == 0` ? – Stuart Mitchell Feb 27 '19 at 20:53
-
nothing happens @StuartMitchell, I just now concern about when x[(i,j)] = 1 For example: x[1,2] = 1 => u[1]+q[2]=u[2] . It seems like I update the data for continuous variables u[i – Quang Phạm Ngọc Feb 28 '19 at 01:16
-
Can you give a minimal complete and verifiable example? - https://stackoverflow.com/help/mcve – kabdulla Feb 28 '19 at 19:28
1 Answers
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.
- 5,199
- 3
- 17
- 30