How to implement CPSat addDifferent with IntVar and LinearExpression in Java

Viewed 69

I'm back at Linear Expressions with Google Or Tools CP Sat in Java. Clunky doesn't describe it even close, Java is a mess here.

My goal is to add a constant to my IntVar and exclude this value for the next IntVar.

(Btw. it's already awkward how i add a constant to my IntVar, is there any other way?)

IntVar a = model.newIntVar(0, someValue, "a");
IntVar b = model.newIntVar(0, someValue, "b");
IntVar one = model.newIntVar(1, 1, "I have to do this");

LinearExpr expr = LinearExpr.scalProd(new IntVar[] {a, one}, new int[] {1, constant}) // a + constant
model.addDifferent(b, expr) // Error because addDifferent is only usable with (IntVar, IntVar)

How can I implement addDifferent with IntVar and LinearExpression?

1 Answers

Indeed. The API is too restrictive.

option 1: You will need to create a new variable, bind it to the expression and use it in addDifferent

option 2:

 // write  b - a != constant 
 model.addDifferent(LinearExpr::scalProd(new IntVar[] {a, b}, new long[] {-1, 1}), constant); 
    
Related