Disjunction of constraints and SumLessOrEqual

794 views
Skip to first unread message

Ákos Menyhért

unread,
Mar 23, 2014, 10:58:09 AM3/23/14
to or-tools...@googlegroups.com

I would like to use the Java api and I can't make disjunction of constraints. I try to implement like this: (A==1 / B==1) /\ ((C==1 / D==1))... How can I do that?

And the other question is how can I implement the makeSumLessOrEqual(IntVar[] VARS, IntVar limit) because there is only makeSumLessOrEqual(IntVar[] VARS, int limit) function.

Thank you for your answers!

Alessandro Zanarini

unread,
Mar 23, 2014, 12:39:30 PM3/23/14
to or-tools...@googlegroups.com
Hello Akos,

For the first point, I don't know how to interpret the sign "/"... I assume you meant a logical OR? or  A== 1 is a reified constraint and then you wanted a division?
I assume the first option and that the variables are boolean (pseudo-code):

IntVar AB = MakeMax(A,B)
IntVar CD = MakeMax(C,D)
MakeMin(AB, CD) == 1

For second point, you can do this (pseudo-code):

IntVar [] vars = ...
IntVar sum = ...
IntVar limit = ...

MakeSum(vars, sum)
MakeLessOrEqual(sum, limit)

I hope this helps
Best,
Alessandro


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

Ákos Menyhért

unread,
Mar 23, 2014, 1:50:26 PM3/23/14
to or-tools...@googlegroups.com
First of all thanks for the answer. Sorry I had a mistake. / sign would be logical OR just it was escaped. The real problem is ( F[f] == r ) → ( fc ∈ FfN (rc ∈ RrN ( F[fc] == rc ))) where F is an IntVar array and RC and r are int. So ther are a lots of constraint which I generate with loops. 
Originally I implement this problem in Choco3 and I want to re-Implement in Or-tools for the comparison.

Thanks

Laurent Perron

unread,
Mar 23, 2014, 2:01:56 PM3/23/14
to or-tools...@googlegroups.com
Hi, 

I still have trouble understanding your formula. 
What is f, what is r

Anyway, back to your first question.

A == 1 -> solver.makeIsEqualCstVar(A, 1) 
this will create a boolean variable.

see examples/com/google/ortools/constraintsolver/samples/whoKilledAgatha.java for instance.

Once you have the boolean, Or(a, b) is the same as Max(a, b), and And(a, b) is Min(a, b).

It may be the case that you could expand everything with an AllowedAssignment constraint.
This is a table constraint where you give it a list of valid tuples. There are not examples in java, but plenty in the examples/cpp directory.

I hope this helps.

Thanks

Ákos Menyhért

unread,
Mar 23, 2014, 2:43:53 PM3/23/14
to or-tools...@googlegroups.com
In my example the f and r are integer constants. I would like to generate some constraints like that:

Forumula:
FA[i] == r --> (FA[j] == rn1 OR FA[j] == rn2 OR...) AND (FA[k] == rn1 OR FA[k] == rn2 OR...)  AND...
where i, j, k, r, rn1..N are integers (bounded range for example 0...200)
          FA is an IntVar array. 

For example:
FA[0]==0 --> (FA[1]==0 OR FA[1]==1 OR FA[1]==2) AND (FA[2]==0 OR FA[2]==1 OR FA[2]==2))
FA[0]==1 --> (FA[1]==0 OR FA[1]==3 OR FA[1]==5) AND (FA[3]==0 OR FA[2]==3 OR FA[3]==5)  AND (FA[4]==0 OR FA[4]==3 OR FA[4]==5))
FA[1]==5 --> (FA[6]==0 OR FA[6]==7 OR FA[6]==10))
....
In choco3 I create separated constraints for FA[i] == rn1 and I will use IntConstraintFactory.or(constraintsForOR) and the same for the AND constraints...

Thank you for your help.

Laurent Perron

unread,
Mar 23, 2014, 2:48:51 PM3/23/14
to or-tools...@googlegroups.com
OK, so you should be able to collapse all the FA[1] == 0 OR FA[1]== 1...
into

solver.makeIsMemberVar(FA[1], [0, 2, ...])

then And is Min, and implication is less or equal.

I hope this helps.

Thanks

Ákos Menyhért

unread,
Mar 24, 2014, 7:11:29 AM3/24/14
to or-tools...@googlegroups.com
If I've understood you well the next example will be this:

1: FA[0]==0 --> (FA[1]==0 OR FA[1]==2 OR FA[1]==4) AND (FA[2]==0 OR FA[2]==2 OR FA[2]==4))
2: FA[0]==1 --> (FA[1]==0 OR FA[1]==3 OR FA[1]==5) AND (FA[3]==0 OR FA[2]==3 OR FA[3]==5)  AND (FA[4]==0 OR FA[4]==3 OR FA[4]==5))
3: FA[1]==5 --> (FA[6]==0 OR FA[6]==7 OR FA[6]==10))

2:
IntVar a1 = solver.makeIsMemberVar(FA[1], [0,3,5]); // OR
IntVar a2 = solver.makeIsMemberVar(FA[3], [0,3,5]); // OR
IntVar a3 = solver.makeIsMemberVar(FA[4], [0,3,5]); // OR

IntExpr ands= solver.makeMin( [a1, a2, a3]); // AND

Constraint eq = solver.makeEquality(FA[0], 1);
solver.makeLessOrEqual(eq, ands); // Implication

But the makeLessOrEqual function does not have Constraint, IntExpression overload.


Sorry but I'm new in google or-tools and csp prolems.
Thanks for yout help!

Laurent Perron

unread,
Mar 24, 2014, 7:35:59 AM3/24/14
to or-tools...@googlegroups.com
Hi

You should not use IntExpr directly.

Replace the ands definition by:

IntVar ands= solver.makeMin( [a1, a2, a3]).var(); // AND

Thanks

Ákos Menyhért

unread,
Mar 24, 2014, 12:19:44 PM3/24/14
to or-tools...@googlegroups.com
Thanks for the realy helpful answers, but I have one more question :)

How can I modelling the "If then else" in or-tools?
Example:
if( FA[i] == R ){
    cpuCons[R][i] == CPUClaimOf(i);
}else{
    cpuCons[R][i] == 0;
}

where FA[i] is IntVar, R and i is integers.

Thanks for the answers!

Laurent Perron

unread,
Mar 25, 2014, 5:37:05 AM3/25/14
to or-tools...@googlegroups.com
cpuConst[R][i] = CPUClaimOf(i) * solver.makeIsEqualVar(FA[i], R);

Thanks

Ákos Menyhért

unread,
Mar 25, 2014, 7:00:13 PM3/25/14
to or-tools...@googlegroups.com
Thank you Laurent Perron!
I have to get used to this csp solver, but it is looks nice!

Thanks

Ákos Menyhért

unread,
Mar 29, 2014, 9:18:18 AM3/29/14
to or-tools...@googlegroups.com
Sorry but I have one more question... 

How can I modelling that sentense in or-tools: (A == i) --> (B == j && C == k), where A,B,C are IntVars and i,j,k are integers.

Thanks

On Tuesday, March 25, 2014 10:37:05 AM UTC+1, Laurent Perron wrote:

Laurent Perron

unread,
Mar 29, 2014, 4:41:44 PM3/29/14
to or-tools...@googlegroups.com
solver.makeEquality(solver.makeIsEqualCstVar(A, i), solver.makeMin(solver.makeIsEqualCstVar(B, j), solver.makeIsEqualCstVar(C, k)))

Thanks
Reply all
Reply to author
Forward
0 new messages