Boolean operations on constraints in Google or-tools library

1,567 views
Skip to first unread message

Masoud Sanaei Ardakani

unread,
Jan 10, 2015, 5:18:00 AM1/10/15
to or-tools...@googlegroups.com

I'm beginner in constraint programming and I'm using Google or-tools library

I have a scheduling problem and I want to add following constraint to my solver:

 ((12 <= t1 <= 15) || (16<= t2 <= 18)) && ( t1 + t2 ) < 30

So I write following piece of code in c#:
 
var solver = new Solver("My_CP_Colver");
var t1 = solver.MakeIntVar(12, 20,"t1");
var t2 = solver.MakeIntVar(12, 20,"t2");
 
solver
.Add(???)//<- ((12 <= t1 <= 15) || (16<= t2 <= 18)) && ( t1 + t2 ) < 30


Any help to make above constraint please?

Sylvain Ducomman

unread,
Jan 10, 2015, 8:23:33 AM1/10/15
to or-tools...@googlegroups.com
Hi,
I’m not an expert in or-tools and I didn’t know c#, but I try to help you with what I know.

For the condition OR (||) you can use the constraint Max : example : (a or b) in or-tools is Max(a,b).
For the condition AND (&&) you can use the constraint Min : example : (a and b) in or-tools is Min(a,b)
And for the other constraint <= it’s the LessOrEqual constraint, or like python it’s directly <= .

After, for use this constraint (12 <= t1 <= 15) in the condition constraint, you should reified this constraint : you can use the constraint IsEqualCstVar(), that create a BoolVar and you use this BoolVar in the condition constraint.
All explain is for c++, but maybe you can find an example with the problem who_killed_agatha : examples/csharp/who_killed_agatha.cs

I hope I can help you,
Thank you
--
Sylvain Ducomman




--
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.

Michael Powell

unread,
Jan 10, 2015, 11:02:08 PM1/10/15
to or-tools...@googlegroups.com


On Saturday, January 10, 2015 at 4:18:00 AM UTC-6, Masoud Sanaei Ardakani wrote:

I'm beginner in constraint programming and I'm using Google or-tools library

I am a C# user of or-tools. It's been a little while since I last looked at my CP problem solvers...
 
I have a scheduling problem and I want to add following constraint to my solver:

 ((12 <= t1 <= 15) || (16<= t2 <= 18)) && ( t1 + t2 ) < 30

So I write following piece of code in c#:
 
var solver = new Solver("My_CP_Colver");
var t1 = solver.MakeIntVar(12, 20,"t1");
var t2 = solver.MakeIntVar(12, 20,"t2");
 
solver
.Add(???)//<- ((12 <= t1 <= 15) || (16<= t2 <= 18)) && ( t1 + t2 ) < 30

As far as I recall, once you have your problem 'modeled' (i.e. you have your variables all defined, etc, etc), they mix well with a fairly rich set of OR tools operator overloads. If you look at what's generated, you find that OR expressions are generated.

Just as a general rule, I can tell you (a < x < b) is probably not going to evaluate like you think it does, for starters; not withstanding OR itself. This is programming, indeed logic, 101, so to speak. Try this instead: (a < x && x < b).

That's the reader's digest version. I can't say for sure whether an expression as involved as that would be supported? Don't know, you would need to try it.

Try decomposing the logical expressions one at a time. Just remember that the solver 'owns' the memory. So you can keep them as local, or private, references. i.e. var firstExpr = a < x && x < b;
 
Any help to make above constraint please?

HTH 
Reply all
Reply to author
Forward
0 new messages