# Non-binary Constraints Toby Walsh

## Presentation on theme: "Non-binary Constraints Toby Walsh"— Presentation transcript:

Non-binary Constraints Toby Walsh tw@cs.york.ac.uk

Outline Definition of non-binary constraints Modeling with non-binary constraints Constraint propagation with non-binary constraints Practical benefits: case study – Golomb rulers

Definitions Binary constraint – Relation on 2 variables identifying those pairs of values disallowed (nogoods) – E.g. not-equals constraint: X1 \= X2. Non-binary constraint – Relation on 3 or more variables identifying tuples of values disallowed – E.g. alldifferent(X1,X2,X3).

Some non-binary examples Timetabling – Variables: Lecture1. Lecture2, … – Values: time1, time2, … – Constraint that lectures do not conflict: alldifferent(Lecture1,Lecture2,…).

Some non-binary examples Scheduling – Variables: Job1. Job2, … – Values: machine1, machine2, … – Constraint on number of jobs on each machine: atmost(2,[Job1,Job2,…],machine1), atmost(1,[Job1,Job2,…],machine2).

Why use non-binary constraints? Binary constraints are NP-complete – Any non-binary constraint can be represented using binary constraints – E.g. alldifferent(X1,X2,X3) is equivalent to X1 \= X2, X1 \= X3, X2 \= X3 In theory therefore theyre not needed – But in practice, they are!

Modeling with non-binary constraints Benefits include: – Compact, declarative specifications (discussed next) – Efficient constraint propagation (discussed after next section)

Modeling with non-binary constraints Consider writing your own alldifferent constraint: alldifferent([]). alldifferent([Head|Tail]):- onedifferent(Head,Tail), alldifferent(Tail). onedifferent(El,[]). onedifferent(El,[Head|Tail]):- El \= Head, onedifferent(El,Tail).

Modeling with non-binary constraints Its possible but its not very pleasant! Nor is it very compact – alldifferent([X1,…Xn]) expands into n(n-1)/2 binary not-equals constraints, Xi \= Xj – one non-binary constraint or O(n^2) binary constraints?

Theoretical comparison Constraint algorithms: – Tree search (labeling) – Constraint propagation at each node Binary constraint propagation – Arc-consistency Non-binary constraint propagation – Generalized arc-consistency

Binary constraint propagation Arc-consistency (AC) is very popular – A binary constraint r(X1,X2) is AC iff for every value for X1, there is a consistent value (often called support) for X2 and vice versa – We can prune values that are not supported – A problem is AC iff every constraint is AC AC offers good tradeoff between amount of pruning and computational effort

Binary constraint propagation X2 \= X3 is AC X1 \= X2 is not AC – X2=1 has no support so can this value can be pruned X2 \= X3 is now not AC – No support for X3=2 – This value can also be pruned Problem is now AC {1} {1,2}{2,3} \= X1 X3X2

Non-binary constraint propagation generalized arc-consistency (GAC) for non- binary constraints – A non-binary constraint is GAC iff for every value for a variable there are consistent values for all other variables in the constraint – We can prune values that are not supported GAC = AC on binary constraints

GAC is stronger than AC Pigeonhole problem – 3 pigeons in 2 holes Non-binary model – alldifferent(X1,X2,X3) is not GAC Binary model – X1 \= X2, X1 \= X3, X2 \= X3 are all AC {2,3} X1 X3X2 \=

Using GAC within search Tree search – Instantiate chosen variable with value (label) – Maintain (incrementally enfoce) some level of consistency Maintaining GAC can be exponentially better than maintaining AC – Construct generalized pigeonhole example on which well explore exponentially fewer nodes

Achieving GAC By exploiting semantics of constraints, we can often enforce GAC efficiently – Consider alldifferent([X1,…Xn]) with each Xi having domain of size m – Generic GAC algorithm runs in O(m^n) – Specialized GAC algorithm for alldifferent runs in O(m^2 n^2) based on network flow

Practical benefits How do these (theoretical) differences affect you practically? Case study – Golomb rulers

Golomb rulers Mark ticks on a ruler – Distance between any two (not necessarily consecutive) ticks is distinct Applications in radio-astronomy – http://csplib.cs.strath.ac.uk/prob006

Golomb rulers Simple solution – Exponentially long ruler – Ticks at 0,1,3,7,15,31,63,… Goal is to find miminal length rulers – turn optimization problem into sequence of satisfaction problems Is there a ruler of length m? Is there a ruler of length m-1? ….

Optimal Golomb rulers Known for up to 23 ticks Distributed internet project to find large rulers 0,1 0,1,3 0,1,4,6 0,1,4,9,11 0,1,4,10,12,17 0,1,4,10,18,23,25

Modeling the Golomb ruler Variable, Xi for each tick Value is position on ruler Naïve model with quaternary constraints – For all i,j,k,l |Xi-Xj| \= |Xk-Xl|

Problems with naïve model Large number of quaternary constraints – O(n^4) constraints Looseness of quaternary constraints – Many values satisfy |Xi-Xj| \= |Xk-Xl| – Limited pruning

A better non-binary model Introduce auxiliary variables for inter-tick distances – Dij = |Xi-Xj| – O(n^2) ternary constraints Post single large non-binary constraint – alldifferent([D11,D12,…]). – Relatively tight!

Other modeling issues Symmetry – A ruler can always be reversed! – Break this symmetry by adding constraint: D12 < Dn-1,n – Also break symmetry on Xi X1 < X2 < … Xn – Such tricks important in many problems

Other modeling issues Additional (implied) constraints – Dont change set of solutions – But may reduce search significantly E.g. D12 < D13, D23 < D24, … Pure declarative specifications are not enough!

Solving issues Labeling strategies often very important – Smallest domain often good idea – Focuses on hardest part of problem Best strategy for Golomb ruler is instantiate variables in strict

Experimental results Runtime/secNaïve modelAlldifferent model 8-Find2.00.1 8-Prove12.010.2 9-Find31.71.6 9-Prove1689.7 10-Find65724.3 10-Prove> 10^568.3

Something to try at home? Circular (or modular) Golomb rulers 2-d Golomb rulers

Conclusions Benefits of non-binary constraints – Compact, declarative models – Efficient and effective constraint propagation Supported by many constraint toolkits – alldifferent, atmost, cardinality, …

Conclusions Modeling decisions: – Auxiliary variables – Implied constraints – Symmetry breaking constraints More to constraints than just declarative problem specifications!