Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply / add Google OR Tools IntVar's and Constants in Java

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?

like image 263
F. G. Avatar asked Mar 11 '26 10:03

F. G.


1 Answers

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

like image 64
Laurent Perron Avatar answered Mar 13 '26 00:03

Laurent Perron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!