0

I am doing linear programming with pulp in Python. And here is one of my constraints: pulp is a third-party pack.

from pulp import *

x1 = LpVariable('x1', 1, 5, cat='Integer')
x2 = LpVariable('x2', 2, 6, cat='Integer')

prob += x1 % 3 != x2 % 3

And obviously, % doesn't support LpVariable.

So, is there anyway that I can do to solve this problem?

Thanks in advance.

sascha
  • 32,238
  • 6
  • 68
  • 110
Yuan
  • 23
  • 4
  • Is this LpVariable your custom class or third party library?, If this is your custom class, then how you want this operation to be performed must be defined by you?, One quick suggestion would be **overload `% `** operator for you LpClass. – Shivam Seth May 10 '20 at 09:09

1 Answers1

2

You need to linearize this manually.

Let's call 3 the divisor.

A linearization could look like (untested; but basic theory should be ok -> try to think about it before copy-paste):

Helper-variables
----------------

add new var quotient_1   : integer-variable in [0, inf)
add new var remainder_1  : integer-variable in [0, divisor - 1]

add new var quotient_2   : integer-variable in [0, inf)
add new var remainder_2  : integer-variable in [0, divisor - 1]

Helper-constraints
------------------

x1 = quotient_1 * divisor + remainder_1
x2 = quotient_2 * divisor + remainder_2

Now it's still needed to model the disjunction:

Helper-variables
----------------

add new var disjunction  : boolean-variable

meaning: 
    disjunction = 1 <-> remainder_1 > remainder_2
    disjunction = 0 <-> remainder_2 < remainder_1

Constraints
-----------

(1-disjunction) * divisor + remainder_1 >= remainder_2 + 1
disjunction * divisor + remainder_2 >= remainder_1 + 1    
sascha
  • 32,238
  • 6
  • 68
  • 110