or-toolsconstraint-programmingcp-sat

Adding a division constraint in Ortools results in MODEL_INVALID model when the nominator is a linear expression of two variables


I am trying to use google CP-SAT solver, version 9.11.4210 in a project. Part of the problem includes dividing a sum of two variables with another variable but the solver returns MODEL_INVALID. I have simplified the problem in the following:

model = CpModel()
v1 = model.NewIntVar(1, 1, "var1")
v2 = model.NewIntVar(1, 1, "var2")
div = model.NewIntVar(2, 2, "div")
model.add_division_equality(div, v1 + v2, v2)
solver = CpSolver()
status = solver.solve(model)

if status == MODEL_INVALID:
    print("The model is invalid")

The model is not invalid when I replace the numerator with, e.g. v1 + 1. Any ideas why this is happening or how to solve?


Solution

  • Have you checked the error message ?

    See how to enable logging

    It will tell you that product, division, modulo all accepts 1-var affine expressions as arguments (a * var + v).

    So you need to create intermediate variables for linear expressions with more than 1 term.