Assign value to boolvar based on other boolvars (CP-SAT | C#)

103 views
Skip to first unread message

Tyler Wallace

unread,
Aug 31, 2020, 11:19:19 AM8/31/20
to or-tools-discuss
Hey! I'm wondering if there is a more direct way to do the following: 

x, y, z are all boolvars. z is only true (1) if x & y are also true (1)

model.Add(z == 1).OnlyEnforceIf(new ILiteral[] { x, y });
model.Add(z == 0).OnlyEnforceIf(new ILiteral[] { x.Not(), y.Not() });
model.Add(z == 0).OnlyEnforceIf(new ILiteral[] { x.Not(), y });
model.Add(z == 0).OnlyEnforceIf(new ILiteral[] { x, y.Not() });

Laurent Perron

unread,
Aug 31, 2020, 11:30:10 AM8/31/20
to or-tools-discuss
model.Add(z == 1).OnlyEnforceIf(new ILiteral[] { x, y });

->

model.AddBoolOr(new ILiteral[] {x.Not(), y.Not(), z }); ?

--
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/aefa541a-6dda-4373-8afb-dac7d480087dn%40googlegroups.com.


--
--Laurent

Tyler Wallace

unread,
Aug 31, 2020, 11:38:40 AM8/31/20
to or-tools-discuss
Thanks for the quick reply.

I admittedly do not understand the AddBoolOr and AddBoolAnd methods very well, so I've avoided them. I thought what you suggested was the equivalent of saying at least one of these has to be true, but all could be true: x == false OR y == false OR z == true. 

Anywhere I can read more about how it works or do you mind providing a quick explanation of AddBoolOr?

Xiang Chen

unread,
Aug 31, 2020, 11:50:34 AM8/31/20
to or-tools...@googlegroups.com
https://en.wikipedia.org/wiki/Material_implication_(rule_of_inference)

If you want
x and y <=> z
instead of just
x and y => z
you also have to add
z => x
z => y
using AddImplication

Tyler Wallace

unread,
Aug 31, 2020, 12:00:13 PM8/31/20
to or-tools-discuss
Ok. So if I have multiple boolvars that have to be true for another to be true, I can use implications. That sounds useful when I have more variables.

To expand it a bit, if I have v, w, x, y all have to be true for z to be true. (Is that written as: v and w and x and y <=> z ?) I can do:
model.Add(z == 1).OnlyEnforceIf(new ILiteral[] { v, w, x, y });
model.AddImplication(z, v);
model.AddImplication(z, w);
model.AddImplication(z, x);
model.AddImplication(z, y);

blind.lin...@gmail.com

unread,
Aug 31, 2020, 3:34:31 PM8/31/20
to or-tools...@googlegroups.com
On the topic of not understanding AddBoolOr...I didn't understand that
either so I wrote up a short blog post about it here:
https://activimetrics.com/blog/ortools/cp_sat/addboolor/. I wrote it
more as a note to my future self, but maybe it will help you too.

James
> >> <https://groups.google.com/d/msgid/or-tools-discuss/aefa541a-6dda-4373-8afb-dac7d480087dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> >> .
> >>
> >
> >
> > --
> > --Laurent
> >
> >
>
> --
> 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/1bb46d38-cc47-4d91-b007-101f2e9a7404n%40googlegroups.com.


--

James E. Marca
Activimetrics LLC

Laurent Perron

unread,
Aug 31, 2020, 4:58:48 PM8/31/20
to or-tools-discuss
Nice blog entry.
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



Reply all
Reply to author
Forward
0 new messages