linear-programmingmixed-integer-programming

How to express Modulo operator constraint in linear programming


I want to state the modulo operation constraint in any open source optimisation modelling language.

How can we state the following constraint : y = x mod 3. Where x, y are integer variables.

Struggling to formulate the above.


Solution

  • y = x mod 3 can be modelled as follows:

    x = 3b + y
    0 <= y <= 2
    x, y, b are integers
    

    Since y can only take values : 0, 1 or 2, x can be determined by some unique values of b and y such that x = 3b + y

    b and y are respectively the quotient and remainder of x mod 3

    edited : per @reinderien comments