问题
I am looking for the best way to model and solve the following linear problem using Pulp where I have conditional statements on my variables to be added to the constraints:
Here is an example:
Max (x1*100 - a*80 - b*100) + (x2*80 - c*120 - d*75)
s.t.
a + b = x1
c + d = x2
x1 > 0
x2 > 0
if x1 > 0 then x2 = 0
if x2 > 0 then x1 = 0
a, b, c, d <= 100
I have declared x1, x2, a, b, c, and d as variables in my pulp problem.
I tried to add 2 indicator functions in my obj function (one for x1 and one for x2) but they are not accepted by Pulp.
I found some good answers: Converting conditional constraints to linear constraints in Linear Programming
But do not know the exact wording to use to code it.
回答1:
Make 2 Binary Variables X1 and X2
Then set
X1+X2 <=1
And
0 <= x1 <= X1*M
0 <= x2 <= X2*M
where M is a sufficiently large number (note the smaller M is the easier the problem is to solve)
来源:https://stackoverflow.com/questions/57171854/conditional-statements-on-variables-added-to-constraints-in-linear-programming