Search code examples
optimizationmathematical-optimizationor-toolsconstraint-programmingcp-sat

Incorporating constraints in relaxed manner in CP-SAT


I have a fairly easy problem setting. I want to Add a const value to a decision variables x and y after models finds x and y with already defined constraints. But I want to add that const value only if the new x and y values are in their domain range and follows all other constraints.

I attempted the problem as follows:

"""Model finding x and y values based on previously defined constraints"""

# defines NewBoolVar(b1, b2)
# This is the trick I tried to used
# defined a temporary bool var - NewBoolVar(temp_bool) and always wants it to be true

model.Add(1>=0).OnlyEnforceIf(temp_bool)     #always keeps temp_bool true

model.Add(x == x + const). OnlyEnforceIf(b1)
model.Add(y == y + const). OnlyEnforceIf(b2)

model.AddBoolOr([b1, temp_bool])
model.AddBoolOr([b2, temp_bool])

with the above formulation I assume the model should give what I expect it to give. But I am not getting what I want. Even x and y being in domain range and follows other constraints they are not added with the const value. I agree that I have this temp_bool variable which is doing it job and not giving me the expected results.

So I want to know is there any trick which I can use to get what I expect? Any other way one can suggest?

thanks :)


Solution

  • Nothing is correct in what your wrote:

    • == is not an assignment but an equality test. Thus it will always fail.
    • model.Add(1 >= 0).OnlyEnforceIf(b): what is that??? model.AddBoolOr(b) or slightly worse model.Add(b == 1).
    • Furthermore, your equation is logically equivalent to b implies true which is actually does not impact b. Both b=true and b=false are valid solutions.
    • model.AddBoolOr(b1, temp_bool): temp_bool is always true. The the bool_or is always satisfied and does not constrain b1.