AllDifferent OnlyEnforceIf doesn't work as expected

171 views
Skip to first unread message

Daniel Karapetyan

unread,
Dec 19, 2018, 11:39:34 AM12/19/18
to or-tools-discuss
Hi,

I am trying to implement a constraint that ensures that at least two of the three variables have different values (in general, I need to ensure that at least k out of n variables have different values).  I tried the following code but it incorrectly tells me that the problem is unsat.  Replacing AddAllDifferent([x, y]) with Add(x != y) fixed the issue but it's hard (and I assume inefficient) to generalise to larger n's.

    model = cp_model.CpModel()
    s1
= model.NewIntVar(1, 5, 's1')
    s2
= model.NewIntVar(1, 5, 's2')
    s3
= model.NewIntVar(1, 5, 's3')

    v1
= model.NewBoolVar('v1')
    v2
= model.NewBoolVar('v2')
    v3
= model.NewBoolVar('v3')

   
# This constraint is added for testing purposes; a feasible solution is s1 = s2 = 1 and s3 = 2
    model
.Add(s1 == s2)

    model
.AddAllDifferent([s1, s2]).OnlyEnforceIf(v1)
    model
.AddAllDifferent([s2, s3]).OnlyEnforceIf(v2)
    model
.AddAllDifferent([s1, s3]).OnlyEnforceIf(v3)
           
    model
.AddBoolOr([v1, v2, v3])

    solver
= cp_model.CpSolver()
    status
= solver.Solve(model)

Am I not allowed to use AllDifferent with OnlyEnforceIf?

Thank you,
Daniel.

Laurent Perron

unread,
Dec 19, 2018, 12:15:43 PM12/19/18
to or-tools-discuss
Enforcement is only supported on linear equations, bool and, bool or and intervals.

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Karapetyan

unread,
Dec 25, 2018, 5:46:18 AM12/25/18
to or-tools-discuss
I see, thank you Laurent!
Reply all
Reply to author
Forward
0 new messages