Global Constraints
E N D
Presentation Transcript
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! • Also happy to have PhDs/PostDocs visit for weeks/months/years …
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
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
Suduko 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]) ..
(I)LP everything has to be a linear inequality limited range of solution methods (simplex, ...) good at proving optimality Comparison 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
Global constraints • Any non-binary constraint • AllDifferent • Nvalues • Element • Lex ordering • Stretch constraint • Sequence constraint • ... • Each comes with an efficient propagator ...
Golomb 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
Golomb 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? • ...
Optimal 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(n^2)
Golomb rulers as CSP • Variable Xi for each tick • Value is position • Auxiliary variable Dij for each inter-tick distance • Dij=|Xi-Xj| • Two (global) constraints • X1<X2<..Xn • AllDifferent(D11,D12,D13,...)
Golomb ruler as CSP • Not yet achieved “dream” of declarative programming • Need to break symmetry of inverting ruler • D12< Dn-1n • Add implied constraints • D12<D13 ... • Pure declarative specifications not quite enough!
AllDifferent • AllDifferent([X1,..Xn]) iff Xi=/=Xj for i<j • 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
AllDifferent • AllDifferent([X1,..Xn]) iff Xi=/=Xj for i<j • 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(n^2) time)
AllDifferent • 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(dn^3/2) • BC algorithm using combinatorics due to Puget [AAAI98], runs in O(nlogn)
BC 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 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 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!
BC 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
BC on AllDifferent • Consider X1 in {1,2}, X2 in {1,2}, X3 in {1,2,3}
BC 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
BC on AllDifferent • Naïve algorithm considers O(n^2) intervals • Puget orders intervals • Ordering has O(nlogn) cost • Then can go through them in order
Beyond AllDifferent • NValues([X1,...,Xn],M) iff |{j | Xi=j}|=M • AllDifferent is special case when M=n • Useful when values represent a resource • Minimize the number of resources used
Beyond AllDifferent • Global 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 resource • You have at least one night shift but no more than four each week
Conclusions • 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 vars=number of values)
Lex 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
Motivation • Many problems can be modelled by matrices of decision variables.
Motivation • Many 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 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
Motivation • Many problems can be modelled by matrices of decision variables • Frequently these matrices exhibit row and/or column symmetry
Motivation • 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
Motivation • 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
Motivation • 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.
Symmetry 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
Example • Consider 2 identical bins: A B
Example • Consider 2 identical bins: A B • We must pack 6 items: 1 2 3 4 5 6
Example • Here is one solution: 5 6 3 4 1 2 A B
Example • Here is another: 6 5 4 3 2 1 A B
Example • Is there any fundamental difference? 5 6 a) 3 4 1 2 A B 6 5 b) 4 3 2 1 A B
Example • Consider a matrix model: 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 6 5 b) 4 3 2 1 A B
If we insist that row A lex row B, we remove a) from the solution set. Example • Consider a matrix model: 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 6 5 b) 4 3 2 1 A B
Example • 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 6 5 b) 4 3 2 1 A B 1 2 3 4 5 6 A 0 1 1 0 0 1 B 1 0 0 1 1 0 6 5 c) 3 4 2 1
If we insist that col 3 lex col 4, we remove c) from the solution set. Example • 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 6 5 b) 4 3 2 1 A B 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)
Breaking Row (Column) Symmetry • Lexicographic ordering is total • Forcing the rows to be lexicographically ordered breaks all row symmetry • lexicographic ordering • [A B C] lex [D E F] lex [G H I] • anti-lexicographic ordering • [G H I]
Breaking 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!
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 0 1 0 1 0 1 1 0 swap the columns swap row 1 and row 3 1 0 1 0
GACLex • A new family of global constraints.
GACLex • A new family of global constraints. Non-binary constraint. Specialised propagator.
GACLex • A new family of global constraints • Linear time complexity