1

I need to add the following constraint using google CP-SAT solver:

(x+y+z)/(x+y+z+k) < 10

The addDivisionEquality method signature is:

Constraint addDivisionEquality (IntVar target, IntVar num, IntVar denom)    

Where

IntVar target = model.newIntVar(0, 10, "(x+y+z)/(x+y+z+k)");

But now I need to define the numerator and denominator as IntVar types while they are the sum of multiple intVars.

The Java package provides a class called SumOfVariables to sum intVars but the addDivisionEquality method requires IntVar. I would expect it to get LinearExpr instead.

How can I define the numerator and denominator as IntVar types?

etov
  • 2,972
  • 2
  • 22
  • 36
forhas
  • 11,551
  • 21
  • 77
  • 111

1 Answers1

3
IntVar numerator = model.newIntVar(0, 10, "(x+y+z)");
model.addEquality(target, LinearExpr.sum(new IntVar[] {x, y, z}));

This being said

model.addLessThan(LinearExpr.sum(new IntVar[] {x, y, z}), 
                  LinearExpr.scalProd(new IntVar[] {x, y, z, k}, new int[] {10, 10, 10, 10}));

is much simpler.

And finally, if all variables are positive,

(x + y + z) / (x + y + z + k) is always <= 1
Laurent Perron
  • 8,594
  • 1
  • 8
  • 22