Forum Discussion

FICO's avatar
FICO
Community Manager
9 years ago

MPVAR mod integer

Hi all,

I'm wondering is it possible to modulo an mpvar.

I have a set of mpvars Xs which are binary and I want to have Sum(i in set) Xi mod 2 = 0 (in other words), the sum of the binary variable are even. However, I get the typical error saying that mpvar mod integer is not defined.

Is there anyway around this?

Thanks,

Cian

2 Replies

  • FICO's avatar
    FICO
    Community Manager

    In a (linear) mixed integer problem formulation you won't be able to use the "mod" operator on decision variables to state the problem. What about introducing an integer variable, say "y", and rewrite your constraint as follows:

    y is_integer

    forall(i in aSet) x(i) is_binary

    sum(i in aSet) x(i) = 2*y

  • FICO's avatar
    FICO
    Community Manager

    Enclosed you find a full example (just for the sake of completeness):

    model Modulo   options noimplicit, keepassert   uses "mmxprs";   declarations     I = 1..3     x: array (I) of mpvar     y: mpvar     obj: linctr   end-declarations   procedure createModel     ! all x variables are binary     forall (i in I) do       x(i) is_binary     end-do     ! y is an auxiliary integer variable     y is_integer     ! (sum (i in I) x(i)) mod 2 = 0     sum(i in I) x(i) = 2 * y     ! objective: maximize sum of x(i)     obj := sum(i in I) x(i)   end-procedure   procedure showSolution     writeln("(sum(i in I) x(i)) mod 2 = ",       (sum(i in I) x(i).sol) mod 2)     forall (i in I) do       writeln("x(",i,") = ", x(i).sol)     end-do   end-procedure   procedure main     createModel     maximize(obj)     showSolution   end-procedure   main end-model