Or-tools: CP solver - Use Add(Bool,Implication,etc) with BoundIntegerExpression

Created on 8 Aug 2018  Â·  5Comments  Â·  Source: google/or-tools

Hi,
I am trying to use the new cp solver, but I am not sure if or how it is possible:

from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.CpSolver()

a = model.NewIntVar(0,1,"")
b = model.NewIntVar(0,1,"")
model.AddImplication(a != 0,b > 0)

throws

NotSupported: model.GetOrMakeBooleanIndex(b >= 1)

Is there a workaround to make b > 0 a BoolVar or do I have to use the old solver?

Help Needed Python

Most helpful comment

@Phibedy you may use an intermediate literal like in #796
Supposing you have (a == -1) => (b == 0):

from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.CpSolver()

a = model.NewIntVar(-1,N,"")
b = model.NewIntVar(0,1,"")
c = model.NewBoolVar("")

model.Add((a == -1)).OnlyEnforceIf(c)
model.Add((a != -1)).OnlyEnforceIf(c.Not())
model.Add((b == 0)).OnlyEnforceIf(c)
model.Add((b != 0)).OnlyEnforceIf(c.Not())

note: we add the opposite (redundant) constraints to help the solver to cut some branches (i.e. don't test/assign a = -1 if b != 0).

All 5 comments

Does this could fix your issue ?

model.Add(b > 0).OnlyEnforceIf(a.Not())

src: https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md

@Mizux Thank you for answering that fast. In my case a is a timestamp and -1 says that no timestamp is set. So I could constrain a bool to a != -1 but that is not possible either?

These are booleans a != 0 is a, b > 0 is also b.

Otherwise, please have a look at :
https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md
to see how to link integer values and boolean values.

I hope this helps.
Laurent Perron | Operations Research | [email protected] | (33) 1 42 68 53
00

Le mer. 8 août 2018 à 05:18, Phibedy notifications@github.com a écrit :

Hi,
I am trying to use the new cp solver, but I am not sure if or how it is
possible:

from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.CpSolver()

a = model.NewIntVar(0,1,"")
b = model.NewIntVar(0,1,"")
model.AddImplication(a != 0,b > 0)

throws

NotSupported: model.GetOrMakeBooleanIndex(b >= 1)

Is there a workaround to make b > 0 a BoolVar or do I have to use the old
solver?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/google/or-tools/issues/793, or mute the thread
https://github.com/notifications/unsubscribe-auth/AKj17V14Zp6va8p60oYb7fiIiHqazHqqks5uOtb3gaJpZM4Vz0q9
.

@Phibedy you may use an intermediate literal like in #796
Supposing you have (a == -1) => (b == 0):

from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.CpSolver()

a = model.NewIntVar(-1,N,"")
b = model.NewIntVar(0,1,"")
c = model.NewBoolVar("")

model.Add((a == -1)).OnlyEnforceIf(c)
model.Add((a != -1)).OnlyEnforceIf(c.Not())
model.Add((b == 0)).OnlyEnforceIf(c)
model.Add((b != 0)).OnlyEnforceIf(c.Not())

note: we add the opposite (redundant) constraints to help the solver to cut some branches (i.e. don't test/assign a = -1 if b != 0).

@Mizux Thank you again :)

Was this page helpful?
0 / 5 - 0 ratings