Global Constraints

62
Global Constraints Toby Walsh NICTA and University of New South Wales www.cse.unsw.edu.au/~tw

description

Global Constraints. Toby Walsh NICTA and University of New South Wales www.cse.unsw.edu.au/~tw. Quick advert. UNSW is in Sydney Regularly voted in top 10 cities in World UNSW is one of top universities in Australia In top 100 universities in world Talk to me about our PhD programme! - PowerPoint PPT Presentation

Transcript of Global Constraints

Global ConstraintsToby Walsh

NICTA and University of New South Waleswww.cse.unsw.edu.au/~tw

Quick advert UNSW is in Sydney

Regularly voted in top 10 cities in World

UNSW is one of top universities in Australia In top 100 universities in

world Talk to me about our

PhD programme! Also happy to have

PhDs/PostDocs visit for weeks/months/years …

QuickTime™ and a decompressor

are needed to see this picture.

Quick advert UNSW is in Sydney

Regularly voted in top 10 cities in World

UNSW is one of top universities in Australia In top 100 universities

in world Talk to me about our

PhD programme! Also happy to have

PhDs/PostDocs visit for weeks/months/years …

Attend CP/KR/ICAPS in Sept

QuickTime™ and a decompressor

are needed to see this picture.

Constraint programming“Constraint programming

represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it”. Gene Freuder, Constraints, April

1997

SudukoPuzzle = [X11,X12,X13, …X19, X21,X22, ………X29, .. X91,X92, ……….X99]Puzzle :: 1..9alldifferent([X11,..X19])alldifferent([X21,..X29])..alldifferent([X11,..X91])..alldifferent([X11,..X33])..

Comparison with ORCP

rich modelling language

many different global constraints

fast local inference on these global constraints

good for finding feasible and tightly constrained solutions

(I)LPeverything has to

be a linear inequality

limited range of solution methods (simplex, ...)

good at proving optimality

Global constraintsAny non-binary constraint

AllDifferentNvaluesElementLex orderingStretch constraintSequence constraint...

Each comes with an efficient propagator ...

All Different

Golomb rulersMark ticks on a rule

Distance between any two ticks (not just neighbouring) is distinct

Special type of graceful graphApplications

Radio-astronomy, crystallorgraphy, ...

Prob006 in CSPLib

Golomb rulersSimple solution

Exponentially long rulerTicks at 0, 1, 3, 7, 15, 31, ...

Goal is to find minimal length rulerSequence of optimization problemsIs there a ruler of length m?Is there a ruler of length m-1?...

Optimal Golomb rulersKnown for up to 23 ticksDistributed internet project to find

larger0,10,1,30,1,4,60,1,4,9,110,1,4,10,12,17

Solutions grow as approximately O(n^2)

Golomb rulers as CSPVariable Xi for each tick

Value is positionAuxiliary variable Dij for each

inter-tick distanceDij=|Xi-Xj|

Two (global) constraintsX1<X2<..XnAllDifferent(D11,D12,D13,...)

Golomb ruler as CSPNot yet achieved “dream” of

declarative programmingNeed to break symmetry of

inverting rulerD12< Dn-1n

Add implied constraintsD12<D13 ...

Pure declarative specifications not quite enough!

AllDifferentAllDifferent([X1,..Xn]) iff Xi=/=Xj for i<jUseful in wide range of applications

Timetabling (exams with common student must occur at different times)

Production (each product must be produced at a different time)

...Can propagate just using binary

inequalitiesBut this decomposition hurts

propagation

AllDifferentAllDifferent([X1,..Xn]) iff Xi=/=Xj for

i<jCan propagate just using binary

inequalitiesBut this decomposition hurts

propagationX1 in {1,2}, X2 in {1,2}, X3 in {1,2,3}X3 cannot be 1 or 2How can we automate such reasoning?How can we do this efficiently (less than

O(n^2) time)

AllDifferentOne of the oldest global constraints

In ALICE language [Lauriere 78]Found in every constraint solver

todayGAC algorithm based on matching

theory due to Regin [AAAI 94], runs in O(dn^3/2)

BC algorithm using combinatorics due to Puget [AAAI98], runs in O(nlogn)

BC on AllDifferentApplication of Hall's Theorem

Sometimes called the “marriage” theorem

Given k setsThen there is an unique and distinct

element in each set iff any union of j of the sets has at least j elements for 0<j<=k

E.g. S1={1,2}, S2={1,2} but not S1={1,2},S2={1,2} and S3={1,2}

Hall's theoremYou wish to marry n men and women

Each woman declares who they are willing to marry (some set of men)

Each man will be “happy” with whoever is willing to marry them

Given any subset of j women, the number of men they are willing to marry must be j or more (thus this condition is necessary)

What is surprising is that it is also sufficient!

BC on AllDifferentHall Interval

Interval of values in which as many variables as domain values

E.g. X1 in {1,2,3}, X2 in {1,2}, X3 in {1,2,3}

3 variables in the interval [1..3]AllDifferent([X1,..Xn]) is BC iff

Each interval, the number of vars it covers is less than or equal to the width of the interval

No variable outside a Hall Interval has a value within it

BC on AllDifferentConsider X1 in {1,2}, X2 in

{1,2}, X3 in {1,2,3}

BC on AllDifferentConsider X1 in {1,2}, X2 in

{1,2}, X3 in {1,2,3}Then [1..2] is a Hall Interval

covered by X1 and X2X3 has values inside this Hall

IntervalWe can prune these and make

AllDifferent BC

BC on AllDifferentNaïve algorithm considers

O(n^2) intervalsPuget orders intervals

Ordering has O(nlogn) costThen can go through them in

order

Beyond AllDifferentNValues([X1,...,Xn],M) iff

|{j | Xi=j}|=MAllDifferent is special case when

M=nUseful when values represent

a resourceMinimize the number of

resources used

Beyond AllDifferentGlobal cardinality constraint

GCC([X1,..Xn],[a1,..am],[b1,...bm]) iff aj <= |{i | Xi=j }| <= bj for all j

In other words, j occurs between aj and bj times

Again useful when values represent a resourceYou have at least one night shift but

no more than four each week

ConclusionsAllDifferent is one of the oldest

(and most useful) global constraintsEfficient propagators exist for

achieving GAC and BCWhen to choose BC over GAC?

Heuristic choice: BC often best when many more values than variables, GAC when we are close to a permutation (number of vars=number of values)

Lex orderingWidely useful

Especially for symmetry breakingBreaking row and column symmetry in

matrix modelsAvailable in most (all?) solvers

Good example of pointer based global constraintPointers save re-doing workGood incremental behaviourO(n) in general, but amortised O(n) cost

down a branch

MotivationMany problems can be

modelled by matrices of decision variables.

MotivationMany problems can be modelled by

matrices of decision variables.E.g. Combinatorial Problems

Balanced Incomplete Block Design.X[i,j]=0 or 1

0 1 1 0 0 1 01 0 1 0 1 0 00 0 1 1 0 0 11 1 0 0 0 0 1 Parameters = (7,7,3,3,1)0 0 0 0 1 1 11 0 0 1 0 1 00 1 0 1 1 0 0

0 1 1 0 0 1 01 0 1 0 1 0 00 0 1 1 0 0 11 1 0 0 0 0 1 0 0 0 0 1 1 11 0 0 1 0 1 00 1 0 1 1 0 0

MotivationMany problems can be modelled

by matrices of decision variablesFrequently these matrices exhibit

row and/or column symmetry

MotivationMany problems can be modelled

by matrices of decision variablesFrequently these matrices exhibit

row and/or column symmetryWe can permute the rows/columns

in any (non)solution to obtain another (non)solution

MotivationWhen rows and columns can be

permutedAn n by m matrix model with row

and column symmetry has n! m! symmetriesgrows super-exponentially

Too many symmetric search statesIt can be very expensive to visit all the

symmetric branches of a search tree

MotivationBreaking symmetry is very

important!Breaking all row and column

symmetries is difficult No one has an effective way of dealing

with all row and column symmetries.Symmetry breaking methods have to

deal with very large number of symmetries.

The effort required could easily be exponential.

Symmetry in CPAdd symmetry breaking constraints

Leave at least one solutionEliminate some/all symmetric solutions

Modify search algorithmIgnore symmetric parts of the search

spaceAdapt branching heuristic

To explore branches which are most likely not to be symmetric

ExampleConsider 2 identical bins:

BA

ExampleConsider 2 identical bins:

• We must pack 6 items: 3 41 652

BA

ExampleHere is one solution:

1 2

3

65

4

BA

ExampleHere is another:

BA

1

3

5

2

6

4

ExampleIs there any fundamental difference?

135

2

64a)

b)

A B

BA135

2

64

ExampleConsider a matrix model:

135

2

64a)

b)

A B

BA135

2

64

1 2 3 4 5 6

A 1 0 1 0 1 0

B 0 1 0 1 0 1

1 2 3 4 5 6

A 0 1 0 1 0 1

B 1 0 1 0 1 0

ExampleConsider a matrix model:

If we insist that row A lex row B, we remove a) from the solution set.

b)

BA135

2

64

1 2 3 4 5 6

A 1 0 1 0 1 0

B 0 1 0 1 0 1

1 2 3 4 5 6

A 0 1 0 1 0 1

B 1 0 1 0 1 0

ExampleNotice that items 3 and 4 are identical.

b)

BA135

2

64

c)145

2

63

1 2 3 4 5 6

A 1 0 1 0 1 0

B 0 1 0 1 0 1

1 2 3 4 5 6

A 0 1 1 0 0 1

B 1 0 0 1 1 0

ExampleNotice that items 3 and 4 are identical.

b)

BA135

2

64

If we insist that col 3 lex col 4, we remove c) from the solution set.

1 2 3 4 5 6

A 1 0 1 0 1 0

B 0 1 0 1 0 1

1 2 3 4 5 6

A 0 1 1 0 0 1

B 1 0 0 1 1 0

Lexicographic Ordering

Used to order dictionaries

[A,B,C] ≤ lex [D,E,F]

A<D or (A=D and B<E ) or (A=D and B=E and C<F) or (A=D and B=E and C=F)

Lexicographic ordering is total Forcing the rows to be lexicographically

ordered breaks all row symmetry

Breaking Row (Column) Symmetry

A B C

D E F

G H I[A B C] lex [D E F] lex [G H I]

[G H I]

lexicographic ordering

anti-lexicographic ordering

Breaking Row and Column SymmetriesBreaking both row and column symmetries

is difficultRows and columns intersectAfter constraining the rows to be

lexicographically orderedwe distinguish the columnsthe columns are not symmetric

anymore!

Good News Each symmetry class of assignments

has at least one element where both the rows and the columns are lexicographically ordered But there may be no element with rows lex

ordered and columns anti-lex ordered To break row and column symmetries,

we can insist that the rows and columns are both lexicographically ordered (double-lex)

Extends to higher dimensions

Bad News A symmetry class of assignments may have more than

one element where both the rows and the columns are lexicographically ordered

Double-lex does not break all row and column symmetries

01

01

10

01

10

10

swap the columnsswap row 1 and row 3

GACLexA new family of global

constraints.

GACLexA new family of global

constraints.Non-binary constraint. Specialised propagator.

GACLexA new family of global

constraintsLinear time complexity

GACLexA new family of global

constraintsLinear time complexityEnsures that a pair of vectors

of variables are lexicographically ordered.

0 1 4 2

2 9 8 7

lex

How GACLex WorksConsider the following

examplex {2} {1,3,4} {2,3,4} {1} {3,4,5}

y {0,1,2} {1} {1,2,3} {0} {0,1,2}

• We want to enforce GAC on: x lex y.

A Tale of Two PointersWe use two pointers, alpha

and beta, to avoid repeatedly traversing the vectors

A Tale of Two Pointers

x {2} {1,3,4} {2,3,4} {1} {3,4,5}

y {0,1,2} {1} {1,2,3} {0} {0,1,2}

Most significant indexAlpha: all variables at more significant indices are ground and equal

A Tale of Two Pointers

• beta: most significant index from which the two vectors’ tails necessarily violate the constraint.

x {2} {1,3,4} {2,3,4} {1} {3,4,5}

y {0,1,2} {1} {1,2,3} {0} {0,1,2}

Pointer initializationalpha

Scan through vector from startAt most O(n) time

betaScan through vector from endAt most O(n) time

FailureInconsistent if beta < alpha.

• alpha: index such that all variables at more significant indices are ground and equal.

• beta: most significant index from which the two vectors’ tails necessarily violate the constraint.

How GACLex WorksWe maintain alpha and beta as assignments

made.

• alpha: index such that all variables at more significant indices are ground and equal.

• beta: most significant index from which the two vectors’ tails necessarily violate the constraint.

How GACLex WroksWe maintain alpha and beta as assignments

made.When beta = alpha + 1 we enforce bounds

consistency on: xalpha < yalpha

• alpha: index such that all variables at more significant indices are ground and equal.

• beta: most significant index from which the two vectors’ tails necessarily violate the constraint.

How GACLex WorksWe maintain alpha and beta as assignments made.When beta = alpha + 1 we enforce bounds

consistency on: xalpha < yalpha

When beta > alpha + 1 we enforce bounds consistency on: xalpha <= yalpha

• alpha: index such that all variables at more significant indices are ground and equal.

• beta: most significant index from which the two vectors’ tails necessarily violate the constraint.

ComplexityInitialisation: O(n)Propagation:

• We enforce bounds consistency between at most n pairs of variables: xalpha < yalpha or xalpha yalpha.

• Cost: b. Overall cost: O(nb). Complexity can be amortized down

branch of search treealpha and beta move at most O(n)

steps

ConclusionsGlobal constraints

One of the special features of constraint programming

Interesting meeting point for algorithms, combinatorics, computational complexity, formal languages, …