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

## Presentation on theme: "Global Constraints Toby Walsh NICTA and University of New South Wales www.cse.unsw.edu.au/~tw."— Presentation transcript:

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

Quick advert ZUNSW is in Sydney ZRegularly voted in top 10 cities in World ZUNSW is one of top universities in Australia ZIn top 100 universities in world ZTalk to me about our PhD programme! ZAlso happy to have PhDs/PostDocs visit for weeks/months/years … ZAttend CP/KR/ICAPS in Sept

Value precedence ZGlobal constraint used to deal with value symmetry ZGood example of “global” constraint where we can use an efficient encoding ZEncoding gives us GAC ZAsymptotically optimal, achieve GAC in O(nd) time ZGood incremental/decremental complexity

Value symmetry ZDecision variables: ZCol[Italy], Col[France], Col[Austria]... ZDomain of values: Zred, yellow, green,... ZConstraints Col[Italy]=/=Col[France] Col[Italy]=/=Col[Austria] …

Value symmetry ZSolution: ZCol[Italy]=green Col[France]=red Col[Spain]=green … ZValues (colours) are interchangeable: ZSwap red with green everywhere will still give us a solution

Value precedence ZOld idea ZUsed in bin-packing and graph colouring algorithms ZOnly open the next new bin ZOnly use one new colour ZApplied now to constraint satisfaction

Value precedence ZSuppose all values from 1 to m are interchangeable ZMight as well let X1=1

Value precedence ZSuppose all values from 1 to m are interchangeable ZMight as well let X1=1 ZFor X2, we need only consider two choices ZX2=1 or X2=2

Value precedence ZSuppose all values from 1 to m are interchangeable ZMight as well let X1=1 ZFor X2, we need only consider two choices ZSuppose we try X2=2

Value precedence ZSuppose all values from 1 to m are interchangeable ZMight as well let X1=1 ZFor X2, we need only consider two choices ZSuppose we try X2=2 ZFor X3, we need only consider three choices ZX3=1, X3=2, X3=3

Value precedence ZSuppose all values from 1 to m are interchangeable ZMight as well let X1=1 ZFor X2, we need only consider two choices ZSuppose we try X2=2 ZFor X3, we need only consider three choices ZSuppose we try X3=2

Value precedence ZSuppose all values from 1 to m are interchangeable ZMight as well let X1=1 ZFor X2, we need only consider two choices ZSuppose we try X2=2 ZFor X3, we need only consider three choices ZSuppose we try X3=2 ZFor X4, we need only consider three choices ZX4=1, X4=2, X4=3

Value precedence ZGlobal constraint ZPrecedence([X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) for all j { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_13.jpg", "name": "Value precedence ZGlobal constraint ZPrecedence([X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) for all j

Value precedence ZGlobal constraint ZPrecedence([X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) ZE.g ZPrecedence([1,1,2,1,3,2,4,2,3]) ZBut not Precedence([1,1,2,1,4])

Value precedence ZGlobal constraint ZPrecedence([X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) ZPropagator proposed by [Law and Lee 2004] ZPointer based propagator (alpha, beta, gamma) but only for two interchangeable values at a time

Value precedence ZPrecedence([j,k],[X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) ZOf course ZPrecedence([X1,..Xn]) iff Precedence([i,j],[X1,..Xn]) for all i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_16.jpg", "name": "Value precedence ZPrecedence([j,k],[X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) ZOf course ZPrecedence([X1,..Xn]) iff Precedence([i,j],[X1,..Xn]) for all i

Value precedence ZPrecedence([j,k],[X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) ZOf course ZPrecedence([X1,..Xn]) iff Precedence([i,j],[X1,..Xn]) for all i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_17.jpg", "name": "Value precedence ZPrecedence([j,k],[X1,..Xn]) iff min({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) ZOf course ZPrecedence([X1,..Xn]) iff Precedence([i,j],[X1,..Xn]) for all i

Puget’s method ZIntroduce Zj to record first time we use j ZAdd constraints ZXi=j implies Zj <= i ZZj=i implies Xi=j ZZi < Zi+1

Puget’s method ZIntroduce Zj to record first time we use j ZAdd constraints ZXi=j implies Zj < i ZZj=i implies Xi=j ZZi < Zi+1 ZBinary constraints Zeasy to implement

Puget’s method ZIntroduce Zj to record first time we use j ZAdd constraints ZXi=j implies Zj < I ZZj=i implies Xi=j ZZi < Zi+1 ZUnfortunately hinders propagation ZAC on encoding may not give GAC on Precedence([X1,..Xn]) ZConsider X1=1, X2 in {1,2}, X3 in {1,3}, X4 in {3,4}, X5=2, X6=3, X7=4

Propagating Precedence ZSimple ternary encoding ZIntroduce sequence of variables, Yi ZRecord largest value used so far ZY1=0

Propagating Precedence ZSimple ternary encoding ZPost sequence of constraints C(Xi,Yi,Yi+1) for each 1<=i<=n These hold iff Xi<=1+Yi and Yi+1=max(Yi,Xi)

Propagating Precedence ZSimple ternary encoding ZPost sequence of constraints ZEasily implemented within most solvers ZImplication and other logical primitives ZGAC-Schema (alias “table” constraint) Z…

Propagating Precedence ZSimple ternary encoding ZPost sequence of constraints ZC(Xi,Yi,Yi+1) for each 1<=i<=n ZThis decomposition is Berge-acyclic ZConstraints overlap on one variable and form a tree

Propagating Precedence ZSimple ternary encoding ZPost sequence of constraints ZC(Xi,Yi,Yi+1) for each 1<=i<=n ZThis decomposition is Berge-acyclic ZConstraints overlap on one variable and form a tree ZHence enforcing GAC on the decomposition achieves GAC on Precedence([X1,..Xn]) ZTakes O(n) time ZAlso gives excellent incremental behaviour

Propagating Precedence ZSimple ternary encoding ZPost sequence of constraints ZC(Xi,Yi,Yi+1) for each 1<=i<=n ZThese hold iff Xi<=1+Yi and Yi+1=max(Yi,Xi) ZConsider Y1=0, X1 in {1,2,3}, X2 in {1,2,3} and X3=3

Precedence and matrix symmetry ZAlternatively, could map into 2d matrix ZXij=1 iff Xi=j ZValue precedence now becomes column symmetry ZCan lex order columns to break all such symmetry ZAlternatively view value precedence as ordering the columns of a matrix model

Precedence and matrix symmetry ZAlternatively, could map into 2d matrix ZXij=1 iff Xi=j ZValue precedence now becomes column symmetry ZHowever, we get less pruning this way ZAdditional constraint that rows have sum of 1 ZConsider, X1=1, X2 in {1,2,3} and X3=1

Partial value precedence ZValues may partition into equivalence classes ZValues within each equivalence class are interchangeable ZE.g. Shift1=nursePaul, Shift2=nursePeter, Shift3=nurseJane, Shift4=nursePaul..

Partial value precedence ZShift1=nursePaul, Shift2=nursePeter, Shift3=nurseJane, Shift4=nursePaul.. ZIf Paul and Jane have the same skills, we can swap them (but not with Peter who is less qualified) ZShift1=nurseJane, Shift2=nursePeter, Shift3=nursePaul, Shift4=nurseJane …

Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_31.jpg", "name": "Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i

Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_32.jpg", "name": "Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i

Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_33.jpg", "name": "Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i

Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_34.jpg", "name": "Partial value precedence ZValues may partition into equivalence classes ZValue precedence easily generalized to cover this case ZWithin each equivalence class, vi occurs before vj for all i

Variable and value precedence ZValue precedence compatible with other symmetry breaking methods ZInterchangeable values and lex ordering of rows and columns in a matrix model

Conclusions ZSymmetry of interchangeable values can be broken with value precedence constraints ZValue precedence can be decomposed into ternary constraints ZEfficient and effective method to propagate ZCan be generalized in many directions ZPartial interchangeability, …

Global constraints ZHardcore algorithms ZData structures ZGraph theory ZFlow theory ZCombinatorics Z… ZComputational complexity ZGlobal constraints are often balanced on the limits of tractability!

Computational complexity 101 ZSome problems are essentially easy ZMultiplication, O(n 1.58 ) ZSorting, O(n logn) ZRegular language membership, O(n) ZContext free language membership, O(n 3 ).. ZP (for “polynomial”) ZClass of decision problems recognized by deterministic Turing Machine in polynomial number of steps ZDecision problem ZQuestion with yes/no answer? E.g. is this string in the regular language? Is this list sorted? …

NP ZNP ZClass of decision problems recognized by non-deterministic Turing Machine in polynomial number of steps ZGuess solution, check in polynomial time ZE.g. is propositional formula  satisfiable? (SAT) ZGuess model (truth assignment) ZCheck if it satisfies formulae in polynomial time

NP ZProblems in NP ZMultiplication ZSorting Z.. ZSAT Z3-SAT ZNumber partitioning ZK-Colouring ZConstraint satisfaction Z…

NP-completeness ZSome problems are computationally as hard as any problem in NP ZIf we had a fast (polynomial) method to solve one of these, we could solve any problem in NP in polynomial time ZThese are the NP-complete problems ZSAT (Cook’s theorem: non-deterministic TM => SAT) Z3-SAT Z….

NP-completeness ZTo demonstrate a problem is NP- complete, there are two proof obligations: Zin NP ZNP-hard (it’s as hard as anything else in NP)

NP-completeness ZTo demonstrate a problem is NP- complete, there are two proof obligations: Zin NP ZPolynomial witness for a solution ZE.g. SAT, 3-SAT, number partitioning, k- Colouring, … ZNP-hard (it’s as hard as anything else in NP)

NP-completeness ZTo demonstrate a problem is NP- complete, there are two proof obligations: ZNP-hard (it’s as hard as anything else in NP) ZReduce some other NP-complete to it ZThat is, show how we can use our problem to solve some other NP-complete problem ZAt most, a polynomial change in size of problem

Global constraints are NP- hard ZCan solve 3SAT using a single global constraint! ZGiven 3SAT problem in N vars and M clauses Z3SAT([X1,…Xn]) where n=N+3M+2 ZConstraint holds iff X1=N, X2=M, ZX_2+i is 0/1 representing value assigned to xi ZX_2+N+3j, X_2+N+3j+1 and X_2+N+3j+2 represents jth clause

Our hammer ZUse tools of computational complexity to study global constraints

Questions to ask? ZGACSupport? is NP-complete ZDoes this value have support? ZBasic question asked within many propagators ZMaxGAC? is DP-complete ZAre these domains the maximal generalized arc-consistent domains? ZTermination test for a propagator ZDP = NP u coNP ZPropagation “harder” than solving the problem!

Questions to ask? ZIsItGAC? is NP-complete ZAre the domains GAC? ZWakeup test for a propagator ZNoGACWipeOut? is NP-complete ZIf we enforce GAC, do we not get a wipeout? ZUsed in many reductions ZGACDomain? is NP-hard ZReturn the maximal GAC domains ZWhat a propagator actually does!

Relationships between questions ZNoGACWipeOut = GACSupport = GACDomain ZNoGACWipeOut in P GACDomain in P ZNoGACWipeOut in NP GACDomain in NP ZGACDomain in P => MaxGAC in P => IsItGAC in P ZIsItGAC in NP => MaxGAC in NP => GACDomain in NP ZOpen if arrows cannot be reversed!

Constraints in practice ZSome constraints proposed in the past are intractable ZNValues(N,X1,..Xn) ZCardPath(N,[X1,..,Xn],C) Z…

NValues ZNValues(N,X1,..,Xm) ZN values used in X1,…,Xm ZUseful for resource allocation

NValues ZNValues(Y,X1,..,Xn) ZReduction of 3SAT to NValues Z3SAT problem in N vars, M clauses  Xi in {i,-i} for 1 ≤ i ≤ N  XN+s in {i,-j,k} if s-th clause is: (i or -j or k) Z Y= N ZHence 3SAT has a solution => NoGACWipeOut answers “yes”

NValues ZNValues(N,X1,..,Xm) ZReduction of 3SAT to NValues Z3SAT problem in n vars, l clauses  Xi in {i,-i} for 1 ≤ i ≤ n  Xn+s in {i,-j,k} if s-th clause is: (i or -j or k) ZN = n ZHence 3SAT has a solution NoGACWipeOut answers “yes” ZEnforce lesser level of local consistency (e.g. BC)

Generalizing constraints ZTake a tractable constraint ZGCC([X1,..,Xn],[l1,..,lm],[u1,..,um]) ZValue j occurs between lj and uj times in X1,..,Xn ZGeneralize some constants to variables ZE.g. GCC([X1,..,Xn],[O1,..,Om]) ZNP-hard to enforce GAC!

Generalizing constraints ZGCC([X1,..,Xn],[O1,..,Om]) ZReduction from 1in3SAT on positive clauses ZIf jth clause is (x or y or z) then Xj in {x,y,z} ZIf x occurs k times in all clauses then Ox in {0,k} ZHence 1in3SAT has a solution iff NoGACWipeOut answers “yes” ZThus enforcing GAC is NP-hard

Meta-constraints ZGlobal constraint used in sequencing problems ZCardPath(C,[X1,..Xn],N) iff C(Xi,..Xi+k) holds N times ZE.g. number of changes is CardPath(=/=,[X1,..Xn],N) ZFixed parameter tractable Zk fixed, GAC takes O(nd^k) time Zk = O(n), GAC is NP-hard even when C is polynomial to test

Meta-constraints ZCardPath(C,[X1,..Xn],N) iff C(Xi,..Xi+k) holds N times ZReduce 3SAT in N variables and M clauses to CardPath where k=N+2 ZNM vars Xi to represent repeated truth assignment ZM vars Yj to represent jth clause ZC(X1,..,XN,Yj,X1’) iff Yj=k and Xk=1 and X1=X1’ or Yj=-k and Xk=0 and X1=X1’ ZC(X2,..,XM.Yj,X1’,X2’) iff X2=X2’ Z..

Conclusions ZComputational complexity is a useful hammer to study global constraints ZUncovers fundamental limits of reasoning with global constraints ZLesser consistency needs to be enforced ZGeneralization intractable Z..

Global grammar constraints ZOften easy to specify a global constraint ZALLDIFFERENT([X1,..Xn]) iff Xi=/=Xj for i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/9/2414271/slides/slide_59.jpg", "name": "Global grammar constraints ZOften easy to specify a global constraint ZALLDIFFERENT([X1,..Xn]) iff Xi=/=Xj for i

Global grammar constraints ZPromising direction initiated is to specify constraints via automata/grammar ZSequence of variables = string in some formal language ZSatisfying assignment = string accepted by the grammar/automata Global constraints meets formal language theory

REGULAR constraint ZREGULAR(A,[X1,..Xn]) holds iff ZX1.. Xn is a string accepted by the deterministic finite automaton A ZProposed by Pesant at CP 2004 ZGAC algorithm using dynamic programming ZHowever, DP is not needed since simple ternary encoding is just as efficient and effective

REGULAR constraint ZDeterministic finite automaton (DFA) Z ZQ is finite set of states ZSigma is alphabet (from which strings formed) ZT is transition function: Q x Sigma -> Q Zq0 is starting state ZF subseteq Q are accepting states ZDFAs accept precisely regular languages ZRegular language can be specified by rules of the form: NonTerminal -> Terminal | Terminal NonTerminal

REGULAR constraint ZDFAs accept precisely regular languages ZRegular language can be specified by rules of the form: NonTerminal -> Terminal NonTerminal -> Terminal NonTerminal | NonTerminal Terminal -Alternatively given by regular expressions -More limited than BNF which can express context- free grammars

REGULAR constraint ZRegular language ZS -> 0 | 0A| AB | AC | 1B | 1 ZA -> 0 | 0A ZB -> 1 | 1B ZC -> 1 | 1C | 0 | 0A ZDFA ZQ={q0,q1,q2} ZSigma={0.1} ZT(q0,0)=q0. T(q0,1)=q1 ZT(q1,0)=q2, T(q1,1)=q1 ZT(q2,0)=q2 ZF={q0,q1,q2}

REGULAR constraint ZRegular language ZS -> 0 | 0A| AB | AC | 1B | 1 ZA -> 0 | 0A ZB -> 1 | 1B ZC -> 1 | 1C | 0 | 0A ZDFA ZQ={q0,q1,q2} ZSigma={0.1} ZT(q0,0)=q0. T(q0,1)=q1 ZT(q1,0)=q2, T(q1,1)=q1 ZT(q2,0)=q2 ZF={q0,q1,q2} CONTIGUITY constraint

REGULAR constraint ZMany global constraints are instances of REGULAR ZAMONG, CONTIGUITY, LEX, PRECEDENCE, STRETCH,.. ZDomain consistency can be enforced in O(ndQ) time using dynamic programming ZContiguity example: {0,1}, {0}, {1}, {0,1}, {1}

REGULAR constraint ZREGULAR constraint can be encoded into ternary constraints ZIntroduce Qi+1 Zstate of the DFA after the ith transition ZThen post sequence of constraints ZC(Xi,Qi,Qi+1) iff DFA goes from state Qi to Qi+1 on symbol Xi

REGULAR constraint ZREGULAR constraint can be encoded into ternary constraints ZConstraint graph is Berge-acyclic ZConstraints only overlap on one variable ZEnforcing GAC on ternary constraints achieves GAC on REGULAR in O(ndQ) time

REGULAR constraint ZPRECEDENCE([X1,..Xn]) iff Zmin({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) for all ji+1 ZT(fail,v)=fail

REGULAR constraint ZPRECEDENCE([X1,..Xn]) iff Zmin({i | Xi=j or i=n+1}) < min({i | Xi=k or i=n+2}) for all ji+1 ZT(fail,v)=fail ZREGULAR encoding of this is just these transition constraints (can ignore fail)

REGULAR constraint ZSTRETCH([X1,..Xn]) holds iff ZAny stretch of consecutive values is between shortest(v) and longest(v) length ZAny change (v1,v2) is in some permitted set, P ZFor example, you can only have 3 consecutive night shifts and a night shift must be followed by a day off

REGULAR constraint ZSTRETCH([X1,..Xn]) holds iff ZAny stretch of consecutive values is between shortest(v) and longest(v) length ZAny change (v1,v2) is in some permitted set, P ZDFA ZQi is ZQ0= ZT(,a)= if q+1<=longest(a) ZT(,b)= if (a,b) in P and q>=shortest(a) ZAll states are accepting

Other generalizations of REGULAR ZREGULAR FIX(A,[X1,..Xn],[B1,..Bm]) iff ZREGULAR(A,[X1,..Xn]) and Bi=1 iff exists j. Xj=I ZCertain values must occur within the sequence ZFor example, there must be a maintenance shift ZUnfortunately NP-hard to enforce GAC on this

Other generalizations of REGULAR ZREGULAR FIX(A,[X1,..Xn],[B1,..Bm]) ZSimple reduction from Hamiltonian path ZAutomaton A accepts any walk on a graph Zn=m and Bi=1 for all i

Chomsky hierarchy ZRegular languages ZContext-free languages ZContext-sensitive languages Z..

Chomsky hierarchy ZRegular languages ZGAC propagator in O(ndQ) time ZConext-free languages ZGAC propagator in O(n^3) time and O(n^2) space ZAsymptotically the same as parsing! ZConext-sensitive languages ZChecking if a string is in the language PSPACE-complete ZUndecidable to know if empty string in grammar and thus to detect domain wipeout and enforce GAC!

Context-free grammars ZApplications ZHierarchy configuration ZBioinformatics ZNatual language parsing ZRostering Z… ZCFG(G,[X1,…Xn]) holds iff ZX1.. Xn is a string accepted by the context free grammar G

CFG propagator ZAdapt CYK parser ZWorks on Chomsky normal form ZNon-terminal -> Terminal ZNon-terminal -> Non-terminal Non- terminal ZUsing dynamic programming ZComputes V[i,j], set of possible parsings for the ith to the jth symbols

Conclusions ZGlobal grammar constraints ZSpecify wide range of global constraints ZProvide efficient and effective propagators automatically ZNice marriage of formal language theory and constraint programming!

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

Similar presentations