# Constraint processing An efficient alternative for search.

## Presentation on theme: "Constraint processing An efficient alternative for search."— Presentation transcript:

Constraint processing An efficient alternative for search

2 Constraint Processing: Overview  Illustrating the idea with examples:  Numerical constraint nets, Spreadsheets  Defining constraint problems  Techniques for solving them:  A variety of backtracking techniques  Consistency and relaxation  Hybrid constraint techniques  Some applications:  Understanding line drawings  Disambiguating natural language

X A B X CD Variable box Multiply box 1.1 Ex.: Numerical constraint nets Given any set of equations: There is an associated constraint net: C = A * B D = C * B B = 1.1 33003630 3000

X A B X CD Variable box Multiply box 1.1 There is an associated constraint net: Numerical constraint nets (2) 3300 3000 3630

5 Example: Spreadsheets AB 1Ratio X 1.2 2 Ratio Y 1.1 4 1 th year 5Income X 3000 6Income Y 5000 7Expenses 9000 CD 2 nd year3 rd year 8Total = B1 * B5 = B1 * C5 = B2 * B6= B2 * C6 = B7 = C7 = B5+B6 -B7 = C5+C6 - C7 =D5+D6 -D7

6 Spreadsheets (2) AB 1Ratio X 1.2 2 Ratio Y 1.1 4 1 th year 5Income X 3000 6Income Y 5000 7Expenses 9000 CD 2 nd year3 rd year 8Total = B1 * B5 = B1 * C5 = B2 * B6= B2 * C6 = B7 = C7 = B5+B6 -B7 = C5+C6 - C7 =D5+D6 -D7 = B1 * B5 = B1 * C5 36004320 = B2 * B6= B2 * C6 55006050 = B7 = C7 9000 -B7 - C7 -D7 = B5+B6= C5+C6=D5+D6 - 1000 1001370

X A B X CD Variable box Multiply box 1.1 There is an associated constraint net: Numerical constraint nets (3) 3630 OR 4840 2000 OR 3000 44004000

Defining Constraint problems Definition File rouge

9 Defining Constraint problems  Definition: A constraint problem (or consistent labeling problem) consists of:  a finite set of variables: z1, z2, …, zn  for each variable: an associated finite domain of possible values di = {ai 1,ai 2, …, ai ni }  for each two variables zi, zj, i  j, a constraint c(zi, zj)  ex.: zi  zj  zi + i  zj - j  we restrict ourselves to binary CPS  Problem: build efficient techniques to assign to each zi a value ai j from its domain di such that all c(zi, zj) are true.

10 Example: q-queens: … is a solution  Given: a q X q chess board  Problem: find (all possible) ways of placing q queens on the board, such that no 2 queens attack each other.  4-queens:

11 Confused q-queens: = identical, BUT each 2 queens MUST attack each other.  C-4-queens:

12 Constraint formulation?  Q-queens and C-q-queens define a set of constraint problems:  depends on choice of domains, constraints  One possibility:  for each queen: a variable zi  for each zi, domain di is all possible board positions:  di = { (1,1), (1,2), …, (1,q), (2,1), (2,2), … }  c(zi, zj) = row(zi)  row(zj)  col(zi)  col(zj)  |row(zi) - row(zj)|  |col(zi) - col(zj)|

13 A ‘better’ formulation:  We agree that each queen is placed on a specific row: z1 z2 z3 z41234  for queen on row i: a variable zi  The representation:  for each zi, domain di = { 1, 2, …, q}  c(zi, zj) = zi  zj  |zi - zj|  |i - j|

14 Notes on C-q-queens:  For C-q-queens, the new second representation defines a slightly different problem: … is no longer a solution  Now C-q-queens always has q+2 solutions !  Advantage for complexity studies.  Exception: q = 3: are extra

Representing the search Or-tree representation Constraint Networks

16 The OR-tree representation: Select an order on the variables: z1, z2, …, zn z1......... a1 1 a1 2 … … … … … … … a1 n1 z2......... a2 1 a2 2 … … a2 n2 c(z1,z2)....... x v … … v c(z1,z2)....... x v … … v z3..... a3 1 a3 2 … … … a3 n3 c(z1,z3)... v v … … … x c(z1,z3)... v v … … … x c(z2,z3)... x v … … … c(z2,z3)... x v … … …

17 OR-tree: explicitly  For each layer (i) in the OR-tree:  Create a branch for every possible assignment to zi  Verify all constraints c(zj, zi), j  i  If all constraints are satisfied, proceed to level i+1  NOTE: this is only a representation for the search  search itself consists of building a (small) part of this tree (as in search techniques)

18 Constraint Networks z2z1z3 z4 c(z1,z2) c(z1,z4) c(z1,z3) c(z2,z3) c(z3,z4) c(z2,z4) {a1 1, a1 2, …, a1 n1 } {a4 1, a4 2, …, a4 n4 } {a2 1, a2 2, …, a2 n2 } {a3 1, a3 2, …, a3 n3 } + relaxation = select an arc (constraint) and remove the inconsistent domain values

19 z2 z1 z3 z4 c(z1,z2) c(z1,z4) c(z1,z3) c(z2,z3) c(z3,z4) c(z2,z4) {a1 1, a1 2, …, a1 n1 } {a4 1, a4 2, …, a4 n4 } {a2 1, a2 2, …, a2 n2 } {a3 1, a3 2, …, a3 n3 } Relaxation: c(z1,z4) a4 1 c(a1 i, a4 1 ) is never true!

Backtrack algorithms Chronological Backtracking BackjumpingBackmarking Intelligent Backtracking Dynamic Search Rearrangement

21 Example: 4-queens: Chronological Backtracking Traverse the OR-tree depth-first, left-to-right. z1.................. 1 z2....... 1 c(z1,z2).... x c(z1,z2).... x 2 x 3 v z3........... 1 c(z1,z3)........ x c(z1,z3)........ x 2 v c(z2,z3)........... x c(z2,z3)........... x 3 v x 4 v x 4 v 1 x 2 v v z4......................... 1 2 3 4 c(z1,z4)...................... x v v x c(z1,z4)...................... x v v x c(z2,z4)........................ x v c(z2,z4)........................ x v c(z3,z4)........................... x c(z3,z4)........................... x

22 The backtrack algorithm: Backtr( depth ) For k= 1 to n depth do z depth := a depth,k ; z depth := a depth,k ; Check all constraints c(z i, z depth ) Check all constraints c(z i, z depth ) with 1  i  depth until one fails; with 1  i  depth until one fails; If none failed then If none failed then If depth = n then return( z1, z2, …, zn) return( z1, z2, …, zn) End-For End-ForElse depth := depth + 1; depth := depth + 1; Backtr( depth ) ; Backtr( depth ) ; depth := depth - 1; depth := depth - 1; Call : Backtr( 1 ) z depth.... a depth,k c(z1,z depth )... v c(z1,z depth )... v c(z2,z depth )... x c(z2,z depth )... x c(z1,z depth )... v c(z2,z depth )... x c(z1,z depth )... v c(z1,z depth )... v c(z2,z depth )... v c(z2,z depth )... v c(z depth-1,z depth ) v c(z depth-1,z depth ) v z depth+1.. a depth+1,1

Backjumping Avoid trashing

24 Trashing: Example: c-4-queens: z1.................. 2 z2....... 1 c(z1,z2).... v c(z1,z2).... v 2 v 3 v z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 4 x z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4)............... c(z3,z4)............... 1x2vv3x4vx1x2vv3x4vx1x2vv3x4vv 1x2vx3x4x 1x2vvv3x4x1x2vx3x4x1x2vx3x4x Only z1 and z2 are tested against the values of z4: z3 is not even considered! Useless to backtrack over z3 ! Redundant computation !

25 Trashing: solution z1.................. 2 z2....... 1 c(z1,z2).... v c(z1,z2).... v 2 v 3 v z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 4 x z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4)............... c(z3,z4)............... 1x2vv3x4vx1x2vv3x4vx1x2vv3x4vv 1x2vx3x4x 1x2vvv3x4x1x2vx3x4x1x2vx3x4x Backjump

26 Trashing: Another occurrence z1.................. 2 z2....... 1 c(z1,z2).... v c(z1,z2).... v 2 v 3 v z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 4 x z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4)............... c(z3,z4)............... 1x2vv3x4vx1x2vv3x4vx1x2vv3x4vv 1x2vx3x4x 1x2vvv3x4x1x2vx3x4x1x2vx3x4x Backjump

27 Backjumping (Gaschnig’78) If all the assignments to zi fail, If all the assignments to zi fail,  The principle: and c(zk, zi) is the deepest constraint causing the fail Then backjump to change the assignment of zk zi.................. ai 1 ai 2... ai ni c(z1,zi)................. x v v x c(z1,zi)................. x v v x c(z2,zi)................... x v c(z2,zi)................... x v c(zk,zi)...................... x c(zk,zi)...................... x … … all fail ! deepest fail-level: k

28 The backjump algorithm: BackJ( depth, out: jumpback ) For k= 1 to n depth do z depth := a depth,k ; z depth := a depth,k ; Check all constraints c(z i, z depth ) Check all constraints c(z i, z depth ) with 1  i  depth until one fails; with 1  i  depth until one fails; If none failed then If none failed then If depth = n then return( z1, z2, …, zn) return( z1, z2, …, zn) End-For End-For c(z1,z depth )... v c(z1,z depth )... v c(z2,z depth )... x c(z2,z depth )... x z depth.... a depth,k checkdepth k = 2 z depth.... a depth,l c(z1,z depth )... v c(z1,z depth )... v c(z2,z depth )... v c(z2,z depth )... v c(z m,z depth )... x c(z m,z depth )... x checkdepth l = m checkdepth k := deepest i checked; Else depth := depth + 1; depth := depth + 1; BackJ( depth, jumpback); BackJ( depth, jumpback); depth := depth - 1; depth := depth - 1; If jumpback  depth then return; If jumpback  depth then return; jumpback := max(checkdepth k )

Backmarking Avoiding other redundancies

30 More redundant checks: z1.................. 2 z2....... 1 c(z1,z2).... v c(z1,z2).... v 2 v 3 v z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 4 x z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4)............... c(z3,z4)............... 1x2vv3x4vx1x2vv3x4vx1x2vv3x4vv 1x2vx3x4x 1x2vvv3x4x1x2vx3x4x1x2vx3x4x z1 is checked against z3 BUT we only back- track over z2 !

31 Occur very often: z1.................. 2 z2....... 1 c(z1,z2).... v c(z1,z2).... v 2 v 3 v z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 4 x z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4)............... c(z3,z4)............... 1x2vv3x4vx1x2vv3x4vx1x2vv3x4vv 1x2vx3x4x 1x2vvv3x4x1x2vx3x4x1x2vx3x4x

32 Trashing compared to Redundant Checks: z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4) c(z3,z4) 1x2vx3x4x Only when a complete BLOCK of checks FAILS. Only when a complete BLOCK of checks FAILS.  Trashing: Also for SUCCESSFUL checks and for the checks performed on 1 ROW only. Also for SUCCESSFUL checks and for the checks performed on 1 ROW only.  Redundant Checks: z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 1x2vv3x4vx1x2vv3x4vx

33 Avoiding redundant checks:  2 approaches: 1. TABULATION:  lemma generation (store results of checks in a table) + lemma application (table look-up for new checks)  improves speed to some extent  but does not really avoid the redundant checks  increases the storage requirements 2. BACKMARKING:  ~ TOTALLY speed saving (no redundant checks)  only limited space-overhead (2 arrays)

34 Backmarking (Gaschnig ‘77):  The 2 arrays: zk.... c(z2,zk).... c(z2,zk).... c(z1,zk).. c(z1,zk).. ak 1 x ak 2 v x … v v … v ak nk v x ak 1 … ak nk c(z k-1,zk)........ c(z k-1,zk)........ = checkdepth zk, l. = checkdepth zk, l.  Checkdepth(k,l): checkdepth zk,1 = 1 checkdepth zk,2 = 2 checkdepth zk,… = k-1 checkdepth zk,nk = 2 ak 2

35 Backmarking (Gaschnig ‘77):  The 2nd array: zk.... c(z2,zk).... c(z2,zk).... c(z1,zk).. c(z1,zk).. ak 1 x ak 2 v v … v v … v ak nk v x ak 1 … ak nk c(z k-1,zk)........ c(z k-1,zk)........ ak 2 = to which (lowest) level did we backup between visiting these 2 blocks for zk. = to which (lowest) level did we backup between visiting these 2 blocks for zk.  Backup(k): zk-1 zk-2

36 A property of Checkdepth k,l The last check needs to be a FAIL. Otherwise, checking would have continued ! The last check can be either fail or succeed.  If Checkdepth k,l  k-1:  If Checkdepth k,l = k-1: zk.... ak,l zk.... ak,l c(z1,zk).. v c(z2,zk).. v … c(zk-1,zk). x/v zk.... ak,l zk.... ak,l c(z1,zk).. v c(z2,zk).. v … c(zi,zk)... x

37 Properties of Checkdepth versus Backup (1):  If checkdepth (k,l)  backup(k) : checkdepth(k,l) checkdepth(k,l)  k-1 The assignment ak,l to zk caused fail the previous time, and it will cause fail again this time (at the same depth) zk....... ak,l zk....... ak,l c(z1,zk)..... v c(z2,zk)..... v … c(zi,zk)......x … c(zk-1,zk) checkdepth(k,l) backup(k)

38 Properties of Checkdepth versus Backup (2):  If checkdepth (k,l)  backup(k) : zk....... ak,l zk....... ak,l c(z1,zk)..... v c(z2,zk)..... v … c(zi,zk)......v … c(zj,zk).... v/x c(zj,zk).... v/x checkdepth(k,l) backup(k) All checks for variables zm, with m lower than backup(k) succeeded before and will succeed again.

39 The Backmark algorithm: BackM( depth, out: checkdepth, backup ) For k= 1 to n depth do z depth := a depth,k ; z depth := a depth,k ; Check all constraints c(z i, z depth ) with 1  i  depth until one fails; Check all constraints c(z i, z depth ) with 1  i  depth until one fails; If none failed then If none failed then … … … End-For If checkdepth( depth, k )  backup( depth ) Then1 backup( depth ) *property 1 applied* *property 2 applied*

40 z1.................. 2 Backmarking at work: z2....... 1 c(z1,z2).... v c(z1,z2).... v z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. 1x2vv z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... 1x2vx3x4x 3x4vx v 2 v 1x2v 1x2v3x4x vv c(z3,z4)......... c(z3,z4)......... 3x4vx

41 At a much later stage: 3 v 1x2vv3x4vv 1x2vx3x4x1x2vx3x4x z1.................. 2 z2....... c(z1,z2).... c(z1,z2).... z3.... c(z2,z3).... c(z2,z3).... c(z1,z3).. c(z1,z3).. z4... c(z1,z4).. c(z1,z4).. c(z2,z4)... c(z2,z4)... c(z3,z4)......... c(z3,z4)......... 4 x 3

42 Discussion and results: Note: initial values for checkdepth(k,l) and backup(k) are all 1  Experimental results:  For c-4-queens problem BTBJBMNodes Checks 2927 29 160139 90  Overall:  BackMarking = best algorithm (not absolute)  Combining BJ and BM? YES !  But does not produce the combined optimisation.

Intelligent Backtracking A generic framework for improved backward behavior

44 Intelligent Backtracking (Bruynooghe - Pereira ‘80)  =~ Dependency-directed Backtracking (Doyle)   one specific algorithm more: a generic strategy which gives rise to many different algorithms. more: a generic strategy which gives rise to many different algorithms.  Key idea: During the construction of the tree, infer and store “no-goods”, Use the “no-goods” to improve the backward behavior. A set of assignments of values to variables that cannot occur simultaniously in any solution.  A no-good is:

45 Example: 8-queens: z1 z2 z3 z4 z5 z6 z7 z8 1 2 3 4 5 6 7 8 Some simple no-goods, because they violate 1 single constraint: {z1 = 1, z6 = 1} {z3 = 5, z6 = 2} {z2 = 3, z6 = 3} {z4 = 2, z6 = 4} {z3 = 5, z6 = 5} {z1 = 1, z6 = 6} {z2 = 3, z6 = 7} {z3 = 5, z6 = 8} Deduced no-good: { z1 = 1, z2 = 3, z3 = 5, z4 = 2 }

46 Reason: z1 z2 z3 z4 z5 z6 z7 z8 1 2 3 4 5 6 7 8 Deduced no-good: { z1 = 1, z2 = 3, z3 = 5, z4 = 2 } Because with these 4 assignments there is no possibility left for placing z6 !

47 A form of Backjumping: z1 z2 z3 z4 z5 z6 z7 z8 1 2 3 4 5 6 7 8 BUT: Which (basic) no-goods to store? Which other no-goods to infer? Controlproblem! No point in back- tracking over the value of z5, because the reason for failure (the no-good) does not involve z5 ! Backtrack on the deepest variable invol- ved in the failure (z4) instead.

48 Another example: z1 z2 z3 z4 z5 z6 z7 z8 1 2 3 4 5 6 7 8 Some simple no-goods: {z3 = 4, z6 = 1} {z5 = 3, z6 = 2} {z5 = 3, z6 = 3} {z5 = 3, z6 = 4} {z2 = 1, z6 = 5} {z4 = 6, z6 = 6} {z3 = 4, z6 = 7} {z4 = 6, z6 = 8} Deduced no-good: { z2 = 1, z3 = 4, z4 = 6, z5 = 3 }

49 Deals with Backmarking- type of redundancy: z1 z2 z3 z4 z5 z6 z7 z8 1 2 3 4 5 6 7 8 Deduced no-good: { z2=1, z3=4, z4=6, z5=3} Each time this combin- ation occurs again (for other values of z1): don’t check, backtrack !

Dynamic Search rearrangement

51 Dynamic search rearrangement  = any backtrack algorithm +  select the order of the variables in the tree dynamically  Purpose: Decrease the size of the tree due to the “first-fail principle” If assigning a value to zi is more likely to fail than assigning to zj: Then: assing to zi first.  The first-fail principle:

52 Effect?  1. Suppose our guess was right… zi... c(z1,zi).. c(z1,zi).. c(z2,zi).. x c(z2,zi).. x ai1vx…aik x zj... c(z1,zj).. c(z1,zj).. c(z2,zj).. c(z2,zj).. aj1 v vaj2 xajl v v ai1vx…aik xai1vx…aik x zi... c(z1,zi).. c(z1,zi).. c(z2,zi). c(z2,zi). Done Smaller search tree !  … then we gain in any case

53 Effect (2)?  2. Suppose our guess was only partly right … ai1vv…aik xai1vv…aik x zi... c(z1,zi).. c(z1,zi).. c(z2,zi). c(z2,zi). Less checks are redone !  … we still gain: Assingment to zi does not fail, but has less successes ! zj... c(z1,zj).. c(z1,zj).. c(z2,zj).. c(z2,zj).. aj1 v vaj2 xajl v v zi... c(z1,zi).. c(z1,zi).. c(z2,zi).. x c(z2,zi).. x ai1vv…aik x zj... c(z1,zj).. c(z1,zj).. c(z2,zj).. c(z2,zj).. aj1 v vaj2 xajl v v

54 Some general first-fail heuristics:  Select the variable with the smallest domain first decreases the branching factor (less possibilities -> probably less successes)  Select the variable with the highest number of non- trivial constraints first: z2z1z3 z4 c(z1,z2) c(z1,z4) = “true” c(z1,z3) “true” = c(z2,z3) c(z3,z4) = “true” c(z2,z4)  + Heuristics based on problem at hand.