2

I currently started working with Google OR Tools CP-Sat Solver in Java and facing Problems with simple mathematical equations including constants and the OR-Tools internal "IntVar".

A Small Example of my Problem:

    // Variables
    IntVar a = model.newIntVar(0, 5, "a");
    IntVar b = model.newIntVar(0, 5, "b");
    int c = 1;

    // Constraint
    model.addEquality(a, a * c); // Cannot apply * with IntVar and int
    model.addEquality(a, a + b); // Cannot Apply + with IntVars
    
    // What I want to achieve
    model.addEquality(a, a * c + b);
    

I'm used to Python where these type - problematic didnt really exist, there a simple model.Add(a == a * c + b) did the job.

Also Or-Tools LinearExpr.sum or LinearExpr.term didnt help me at all.

Has anyone worked with CP-Sat Optimization Problems in Java and knows a workaround?

F. G.
  • 56
  • 1
  • 5

1 Answers1

1

There are no operator overloading in java. So you are stuck with the LinearExpr methods.

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
  • As far as i understand im also stuck with LinearExpr because i cant cast a LinearExpr Object into IntVar. With `d = LinearExpr.term(a,c)` I do the multiplying part and end up with a LinearExpr Object wich I cant use in `LinearExpr.sum(d, b)` bc it expects only IntVar Objects. – F. G. May 07 '21 at 14:13
  • so LinearExpr.ScalProd(new IntVar[]{a, b}, new int[]{c, 1}). Clunky, but this is java. I do not want to support nested expressions. – Laurent Perron May 07 '21 at 14:51
  • Thats the way. Thanks a lot :) – F. G. May 07 '21 at 16:07