How to check an IntVar has a specific value using BoolVar?

138 views
Skip to first unread message

Ellie Choi

unread,
Jul 11, 2023, 3:03:01 AM7/11/23
to or-tools-discuss
hi, 

how can I check If an IntVar has a specific value?
I want to write the code as below:

+) It seems that "model.Add(bool == (var1 == target))" raises the TypeError.

# ortools-9.6.2534 
# protobuf-4.23.4
##### Example code #####
model = cp_model.CpModel()
var1 = model.NewIntVar(0, 100, 'var1')
var2 = model.NewIntVar(0, 100*100, 'var2')
target = 24

for i in range(100):
   bool = model.NewBoolVar('bool')

   # check var1 has a value of target
   # I want to check the value of var1 like this
   model.Add(bool == (var1 == target))

   # if var1 has a value of target,  assign var1**2 to var2 
   # I want to use BoolVar like this
   model.AddMultiplicationEquality(var2, [var1, var1]).OnlyEnforceIf(bool) 

10858...@qq.com

unread,
Jul 11, 2023, 3:51:43 AM7/11/23
to or-tools-discuss

I think you can creat a new bool variable to judge var1 = target or vae1 != target.

Ellie Choi

unread,
Jul 11, 2023, 4:30:36 AM7/11/23
to or-tools-discuss
sorry, I'm affraid I couldn't catch the point.
Do you mean that it needs two boolean variables?
2023년 7월 11일 화요일 오후 4시 51분 43초 UTC+9에 10858...@qq.com님이 작성:

10858...@qq.com

unread,
Jul 11, 2023, 5:20:55 AM7/11/23
to or-tools-discuss
you can do like this

creat a new bool variable:
y = model.NewBoolVar("y")

add new constraints:
model.Add(var1 == target).OnlyEnforceIf(y)
model.Add(var1 != target).OnlyEnforceIf(y.Not())

change this constraint:
model.AddMultiplicationEquality(var2, [var1, var1]).OnlyEnforceIf(y) 

you can try, i think  it can solve you trouble

Laurent Perron

unread,
Jul 11, 2023, 5:21:23 AM7/11/23
to or-tools-discuss
Please do not spam.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/3dde0d8d-fa96-4a67-be20-58a7703ca971n%40googlegroups.com.

Laurent Perron

unread,
Jul 11, 2023, 5:22:47 AM7/11/23
to or-tools-discuss
Thanks for answering
 

--
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.
Reply all
Reply to author
Forward
0 new messages