320 likes | 344 Vues
Toby Walsh National ICT Australia and University of New South Wales www.cse.unsw.edu.au/~tw. Global Constraints . Course outline. Introduction All Different Lex ordering Value precedence Complexity GAC-Schema Soft Global Constraints Global Grammar Constraints Roots Constraint
E N D
Toby Walsh National ICT Australia and University of New South Wales www.cse.unsw.edu.au/~tw Global Constraints
Course outline • Introduction • All Different • Lex ordering • Value precedence • Complexity • GAC-Schema • Soft Global Constraints • Global Grammar Constraints • Roots Constraint • Range Constraint • Slide Constraint • Global Constraints on Sets
Conclusions • Constraint programming is a powerful paradigm for solving many combinatorial optimization problems • Global constraints are integral to this success • As we shall see, there are many different types of global constraints • However, a few are central like AllDifferent, Roots, Range, Slide, ...
Example application All Different
Golomb ruler s • Mark ticks on a rule • Distance between any two ticks (not just neighbouring) is distinct • Applications • Radio-astronomy • Crystallorgraphy • ... • Prob006 in CSPLib, www.csplib.org
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} • X1 and X2 consume between them 2 values • X3 cannot therefore use either of these values • Therefore we can prune X3=1 and X3=2 leaving just X3=3 • How can we automate such reasoning? • How can we do this efficiently (< O(n^2))
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 • Clearly 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 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
GAC on AllDifferent • GAC algorithm based on matching theory due to Regin [AAAI 94] • runs in O(dn^3/2) • Matching = set of edges with no two edges having a node in common • Maximal matching = largest possible matching • Value graph = bipartite graph between variables and their possible values
GAC on AllDifferent • Consider X1 in {1,3}, X2 in {1,3} and X3 in {1,2}
GAC on AllDifferent • Consider X1 in {1,3}, X2 in {1,3} and X3 in {1,2} X1 X2 X3 1 2 3
GAC on AllDifferent • Consider X1 in {1,3}, X2 in {1,3} and X3 in {1,2} X1 X2 X3 1 2 3
GAC on AllDifferent • Consider X1 in {1,3}, X2 in {1,3} and X3 in {1,2} X1 X2 X3 1 2 3
GAC on AllDifferent • Consider X1 in {1,3}, X2 in {1,3} and X3 in {1,2} X1 X2 X3 1 2 3
GAC on AllDifferent • Consider X1 in {1,3}, X2 in {1,3} and X3 in {2} X1 X2 X3 1 2 3
GAC on AllDifferent • How do we find edges that don't belong to any maximum matching? • Find one maximum matching • Identify strongly connected components • Prune other edges • Dominate cost is that of finding maximum matching • Augmenting path algorithm of Hopcroft and Karp
Beyond AllDifferent • Symmetric AllDifferent • Xi=j iff Xj=i • Useful in sports scheduling • Team1 plays 2 iff Team2 plays 1 • GAC can be enforced in O(nm) time • Again based on maximum matching but now in non-bipartite graph
Beyond AllDifferent • Soft AllDifferent • Relax constraint that all values are different • Introduce “cost” variable • Number of variables that need to change value to make it all different • Number of binary inequalities not satisfied
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
Propagating GCC • Regin gives O(n^2d) flow based algorithm for achieving GAC [AAAI 96] • Improved recently to O(n^3/2 d) using matching theory • We'll come back to a similar flow based propagator for the RANGE constraint
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)