Title: Global%20Constraints
1Global Constraints
- Toby Walsh
- NICTA and University of New South Wales
- www.cse.unsw.edu.au/tw
2Quick 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
3Quick 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
4Constraint 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
5Suduko
- Puzzle
- X11,X12,X13, X19,
- X21,X22, X29,
- ..
- X91,X92, .X99
- Puzzle 1..9
- alldifferent(X11,..X19)
- alldifferent(X21,..X29)
- ..
- alldifferent(X11,..X91)
- ..
- alldifferent(X11,..X33)
- ..
6Comparison with OR
- CP
- rich modelling language
- many different global constraints
- fast local inference on these global constraints
- good for finding feasible and tightly constrained
solutions
- (I)LP
- everything has to be a linear inequality
- limited range of solution methods (simplex, ...)
- good at proving optimality
7Global constraints
- Any non-binary constraint
- AllDifferent
- Nvalues
- Element
- Lex ordering
- Stretch constraint
- Sequence constraint
- ...
- Each comes with an efficient propagator ...
8All Different
9Golomb rulers
- Mark ticks on a rule
- Distance between any two ticks (not just
neighbouring) is distinct - Special type of graceful graph
- Applications
- Radio-astronomy, crystallorgraphy, ...
- Prob006 in CSPLib
10Golomb rulers
- Simple solution
- Exponentially long ruler
- Ticks at 0, 1, 3, 7, 15, 31, ...
- Goal is to find minimal length ruler
- Sequence of optimization problems
- Is there a ruler of length m?
- Is there a ruler of length m-1?
- ...
11Optimal Golomb rulers
- Known for up to 23 ticks
- Distributed internet project to find larger
- 0,1
- 0,1,3
- 0,1,4,6
- 0,1,4,9,11
- 0,1,4,10,12,17
- Solutions grow as approximately O(n2)
12Golomb rulers as CSP
- Variable Xi for each tick
- Value is position
- Auxiliary variable Dij for each inter-tick
distance - DijXi-Xj
- Two (global) constraints
- X1ltX2lt..Xn
- AllDifferent(D11,D12,D13,...)
13Golomb ruler as CSP
- Not yet achieved dream of declarative
programming - Need to break symmetry of inverting ruler
- D12lt Dn-1n
- Add implied constraints
- D12ltD13 ...
- Pure declarative specifications not quite enough!
14AllDifferent
- AllDifferent(X1,..Xn) iff Xi/Xj for iltj
- Useful 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 inequalities
- But this decomposition hurts propagation
15AllDifferent
- AllDifferent(X1,..Xn) iff Xi/Xj for iltj
- Can propagate just using binary inequalities
- But this decomposition hurts propagation
- X1 in 1,2, X2 in 1,2, X3 in 1,2,3
- X3 cannot be 1 or 2
- How can we automate such reasoning?
- How can we do this efficiently (less than O(n2)
time)
16AllDifferent
- One of the oldest global constraints
- In ALICE language Lauriere 78
- Found in every constraint solver today
- GAC algorithm based on matching theory due to
Regin AAAI 94, runs in O(dn3/2) - BC algorithm using combinatorics due to Puget
AAAI98, runs in O(nlogn)
17BC on AllDifferent
- Application of Hall's Theorem
- Sometimes called the marriage theorem
- Given k sets
- Then there is an unique and distinct element in
each set iff any union of j of the sets has at
least j elements for 0ltjltk - E.g. S11,2, S21,2 but not S11,2,S21,2
and S31,2
18Hall's theorem
- You 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!
19BC on AllDifferent
- Hall 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
20BC on AllDifferent
- Consider X1 in 1,2, X2 in 1,2, X3 in 1,2,3
21BC on AllDifferent
- Consider X1 in 1,2, X2 in 1,2, X3 in 1,2,3
- Then 1..2 is a Hall Interval covered by X1 and
X2 - X3 has values inside this Hall Interval
- We can prune these and make AllDifferent BC
22BC on AllDifferent
- Naïve algorithm considers O(n2) intervals
- Puget orders intervals
- Ordering has O(nlogn) cost
- Then can go through them in order
23Beyond AllDifferent
- NValues(X1,...,Xn,M) iff j
XijM - AllDifferent is special case when Mn
- Useful when values represent a resource
- Minimize the number of resources used
24Beyond AllDifferent
- Global cardinality constraint
- GCC(X1,..Xn,a1,..am,b1,...bm) iff
aj lt i Xij lt bj for
all j - In other words, j occurs between aj and bj times
- Again useful when values represent a resource
- You have at least one night shift but no more
than four each week
25Conclusions
- AllDifferent is one of the oldest (and most
useful) global constraints - Efficient propagators exist for achieving GAC and
BC - When 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 varsnumber of values)
26Lex ordering
- Widely useful
- Especially for symmetry breaking
- Breaking row and column symmetry in matrix models
- Available in most (all?) solvers
- Good example of pointer based global constraint
- Pointers save re-doing work
- Good incremental behaviour
- O(n) in general, but amortised O(n) cost down a
branch
27Motivation
- Many problems can be modelled by matrices of
decision variables.
28Motivation
- Many problems can be modelled by matrices of
decision variables. - E.g. Combinatorial Problems
- Balanced Incomplete Block Design.
- Xi,j0 or 1
- 0 1 1 0 0 1 0
- 1 0 1 0 1 0 0
- 0 0 1 1 0 0 1
- 1 1 0 0 0 0 1 Parameters
(7,7,3,3,1) - 0 0 0 0 1 1 1
- 1 0 0 1 0 1 0
- 0 1 0 1 1 0 0
0 1 1 0 0 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 0 0
0 0 1 0 0 0 0 1 1 1 1 0 0 1 0 1 0 0 1 0 1 1
0 0
29Motivation
- Many problems can be modelled by matrices of
decision variables - Frequently these matrices exhibit row and/or
column symmetry
30Motivation
- Many problems can be modelled by matrices of
decision variables - Frequently these matrices exhibit row and/or
column symmetry - We can permute the rows/columns in any
(non)solution to obtain another (non)solution
31Motivation
- When rows and columns can be permuted
- An n by m matrix model with row and column
symmetry has - n! m! symmetries
- grows super-exponentially
- Too many symmetric search states
- It can be very expensive to visit all the
symmetric branches of a search tree
32Motivation
- Breaking 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.
33Symmetry in CP
- Add symmetry breaking constraints
- Leave at least one solution
- Eliminate some/all symmetric solutions
- Modify search algorithm
- Ignore symmetric parts of the search space
- Adapt branching heuristic
- To explore branches which are most likely not to
be symmetric
34Example
- Consider 2 identical bins
B
A
35Example
- Consider 2 identical bins
B
A
3
4
1
6
5
2
36Example
6
5
3
4
1
2
B
A
37Example
5
6
3
4
1
2
B
A
38Example
- Is there any fundamental difference?
5
6
a)
3
4
1
2
A
B
5
6
b)
3
4
1
2
B
A
39Example
1 2 3 4 5 6 A 1 0 1 0
1 0 B 0 1 0 1 0 1
5
6
a)
3
4
1
2
A
B
1 2 3 4 5 6 A 0 1 0 1
0 1 B 1 0 1 0 1 0
5
6
b)
3
4
1
2
B
A
40Example
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
5
6
b)
3
4
1
2
B
A
41Example
- Notice that items 3 and 4 are identical.
1 2 3 4 5 6 A 1 0 1 0
1 0 B 0 1 0 1 0 1
5
6
b)
3
4
1
2
B
A
1 2 3 4 5 6 A 0 1 1 0
0 1 B 1 0 0 1 1 0
5
6
c)
4
3
1
2
42Example
- Notice that items 3 and 4 are identical.
1 2 3 4 5 6 A 1 0 1 0
1 0 B 0 1 0 1 0 1
5
6
b)
3
4
1
2
B
A
1 2 3 4 5 6 A 0 1 1 0
0 1 B 1 0 0 1 1 0
43Lexicographic Ordering
- Used to order dictionaries
- A,B,C lex D,E,F
- AltD or
- (AD and BltE ) or
- (AD and BE and CltF) or
- (AD and BE and CF)
44Breaking Row (Column) Symmetry
- Lexicographic ordering is total
- Forcing the rows to be lexicographically ordered
breaks all row symmetry
lexicographic ordering
A B C
D E F
G H I
A B C ? lex D E F ? lex G H I
anti-lexicographic ordering
G H I
45Breaking Row and Column Symmetries
- Breaking both row and column symmetries is
difficult - Rows and columns intersect
- After constraining the rows to be
lexicographically ordered - we distinguish the columns
- the columns are not symmetric
anymore!
46Good 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
47Bad 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
1
0
1
0
1
0
0
1
swap the columns swap row 1 and row 3
0
1
0
1
48GACLex
- A new family of global constraints.
49GACLex
- A new family of global constraints.
Non-binary constraint. Specialised propagator.
50GACLex
- A new family of global constraints
- Linear time complexity
51GACLex
- A new family of global constraints
- Linear time complexity
- Ensures that a pair of vectors of variables are
lexicographically ordered.
0 1 4 2
? lex
2 9 8 7
52How GACLex Works
- Consider the following example
x 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.
53A Tale of Two Pointers
- We use two pointers, alpha and beta, to avoid
repeatedly traversing the vectors
54A 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 index
Alpha all variables at more significant indices
are ground and equal
55A 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
- beta most significant index from which the two
vectors tails necessarily violate the constraint.
56Pointer initialization
- alpha
- Scan through vector from start
- At most O(n) time
- beta
- Scan through vector from end
- At most O(n) time
57Failure
Inconsistent if beta lt 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.
58How GACLex Works
We 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.
59How GACLex Wroks
We maintain alpha and beta as assignments
made. When beta alpha 1 we enforce bounds
consistency on xalpha lt 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.
60How GACLex Works
We maintain alpha and beta as assignments
made. When beta alpha 1 we enforce bounds
consistency on xalpha lt yalpha When beta gt alpha
1 we enforce bounds consistency on xalpha lt
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.
61Complexity
- Initialisation O(n)
- Propagation
- We enforce bounds consistency between at most n
pairs of variables xalpha lt yalpha or xalpha ?
yalpha. - Cost b. Overall cost O(nb).
-
- Complexity can be amortized down branch of search
tree - alpha and beta move at most O(n) steps
62Conclusions
- Global constraints
- One of the special features of constraint
programming - Interesting meeting point for algorithms,
combinatorics, computational complexity, formal
languages,