non-binary constraints toby walsh [email protected]

30
Non-binary Constraints Toby Walsh [email protected]

Upload: kimberly-mcgarry

Post on 28-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

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

Non-binary Constraints

Toby [email protected]

Page 2: 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

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

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

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

Some non-binary examples

Timetabling– Variables: Lecture1. Lecture2, …– Values: time1, time2, …– Constraint that lectures do not conflict:

alldifferent(Lecture1,Lecture2,…).

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

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

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

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 they’re not needed– But in practice, they are!

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

Modeling with non-binary constraints

Benefits include:– Compact, declarative specifications

(discussed next)

– Efficient constraint propagation(discussed after next section)

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

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

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

Modeling with non-binary constraints

It’s possible but it’s 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?

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

Theoretical comparison

Constraint algorithms:– Tree search (labeling)– Constraint propagation at each node

Binary constraint propagation– Arc-consistency

Non-binary constraint propagation– Generalized arc-consistency

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

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

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

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

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

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

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

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}

{2,3} {2,3}

X1

X3X2

\= \=

\=

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

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 we’ll explore exponentially fewer nodes

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

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

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

Practical benefits

How do these (theoretical) differences affect you practically?

Case study– Golomb rulers

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

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

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

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 problemsIs there a ruler of length m?Is there a ruler of length m-1?….

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

Optimal Golomb rulers

Known for up to 23 ticks Distributed internet project to find large

rulers0,1

0,1,30,1,4,6

0,1,4,9,110,1,4,10,12,17

0,1,4,10,18,23,25

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

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|

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

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

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

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!

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

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

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

Other modeling issues

Additional (implied) constraints– Don’t change set of solutions– But may reduce search significantly

E.g. D12 < D13, D23 < D24, … Pure declarative specifications are not

enough!

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

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

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

Experimental results

Runtime/sec

Naïve model

Alldifferent model

8-Find 2.0 0.1

8-Prove 12.0 10.2

9-Find 31.7 1.6

9-Prove 168 9.7

10-Find 657 24.3

10-Prove > 10^5 68.3

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

Something to try at home?

Circular (or modular) Golomb rulers

2-d Golomb rulers

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

Conclusions

Benefits of non-binary constraints– Compact, declarative models– Efficient and effective constraint propagation

Supported by many constraint toolkits– alldifferent, atmost, cardinality, …

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

Conclusions

Modeling decisions:– Auxiliary variables– Implied constraints– Symmetry breaking constraints

More to constraints than just declarative problem specifications!