1 / 54

Constraint processing

Constraint processing. An efficient alternative for search. Constraint Processing: Overview. Illustrating the idea with examples: Numerical constraint nets, Spreadsheets. Defining constraint problems Techniques for solving them: A variety of backtracking techniques

dooley
Download Presentation

Constraint processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  4. There is an associated constraint net: A • C D X 3000 3300 3630 X Multiply box B 1.1 Variable box Numerical constraint nets (2)

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

  6. A B C D 1 Ratio X 1.2 Ratio Y 1.1 2 4 th nd rd 1 year 2 year 3 year 5 Income X 3000 = B1 * B5 = B1 * B5 = B1 * C5 = B1 * C5 3600 4320 6 Income Y 5000 = B2 * B6 = B2 * B6 = B2 * C6 = B2 * C6 5500 6050 7 Expenses 9000 = B7 = B7 = C7 = C7 9000 9000 8 Total = B5+B6 = B5+B6 = C5+C6 = C5+C6 =D5+D6 =D5+D6 - 1000 100 1370 -B7 -B7 - C7 - C7 -D7 -D7 In what if mode Spreadsheets (2)

  7. There is an associated constraint net: A 2000 OR 3000 • C D 3630 OR 4840 X 4000 X 4400 Multiply box B 1.1 Variable box Numerical constraint nets (3) ?

  8. Defining Constraint problems Definition File rouge

  9. a finite set of variables: z1, z2, … , zn Defining Constraint problems • Definition: A constraint problem (or consistent labeling problem) consists of: • for each variable: an associated finite domain of possible values di = {ai1,ai2, … , aini} • 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 aij from its domain di such that all c(zi, zj) are true.

  10. … is a solution Example: q-queens: • 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. 1 2 3 4 z1 z2 z3 z4 • The representation: • for queen on row i: a variable zi A ‘better’ formulation: • We agree that each queen is placed on a specific row: • for each zi, domain di = { 1, 2, … , q} • c(zi, zj) = zizj |zi-zj|  |i-j|

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

  15. Representing the search Or-tree representation Constraint Networks

  16. z1 . . . . . . . . . a11 a12 … … … … … … … a1n1 z2 . . . . . . . . . a21 a22 … … a2n2 z3 . . . . . a31 a32 … … … a3n3 The OR-tree representation: Select an order on the variables: z1, z2, …, zn c(z1,z2). . . . . . . x v … … v c(z1,z3). . . v v … … … x 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. {a11, a12, …, a1n1} z1 c(z1,z2) c(z1,z4) {a21, a22, …, a2n2} c(z1,z3) {a41, a42, …, a4n4} z2 z4 c(z2,z4) c(z2,z3) c(z3,z4) z3 {a31, a32, …, a3n3} Constraint Networks + relaxation = select an arc (constraint) and remove the inconsistent domain values

  19. {a11, a12, …, a1n1} z1 c(a1i, a41) is never true! c(z1,z2) c(z1,z4) {a21, a22, …, a2n2} c(z1,z3) {a41, a42, …, a4n4} z2 z4 c(z2,z4) c(z2,z3) c(z3,z4) a41 z3 {a31, a32, …, a3n3} Relaxation: c(z1,z4)

  20. Backtrack algorithms Chronological Backtracking Backjumping Backmarking Intelligent Backtracking Dynamic Search Rearrangement

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

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

  23. Backjumping Avoid trashing

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

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

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

  27. zi . . . . . . . . . . . . . . . . . . ai1 ai2 ... aini c(z1,zi). . . . . . . . . . . . . . . . . x v v x all fail ! c(z2,zi). . . . . . . . . . . . . . . . . . . x v … … c(zk,zi). . . . . . . . . . . . . . . . . . . . . . x deepest fail-level: k Backjumping (Gaschnig’78) • The principle: If all the assignments to zi fail, andc(zk, zi) is the deepest constraint causing the fail Thenbackjump to change the assignment of zk

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

  29. Backmarking Avoiding other redundancies

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

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

  32. Trashing: Only when a complete BLOCK of checks FAILS. z3 . . . . 1 x 1 x 1 x 2 v x 2 v v 2 v v 3 x 3 x 3 x 4 x 4 v x 4 v x c(z1,z3). . c(z2,z3). . . . z4 . . . c(z1,z4). . c(z2,z4). . . c(z3,z4) Trashing compared to Redundant Checks: • Redundant Checks: Also for SUCCESSFUL checks and for the checks performed on 1 ROW only.

  33. lemma generation (store results of checks in a table) Avoiding redundant checks: • 2 approaches: 1. TABULATION: + 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. zk . . . . ak1 x ak2 v x … v v … v aknk v x ak1 … aknk c(z1,zk). . c(z2,zk). . . . c(zk-1,zk). . . . . . . . checkdepthzk,1 = 1 checkdepthzk,nk = 2 checkdepthzk,…= k-1 checkdepthzk,2 = 2 • Checkdepth(k,l): = checkdepthzk, l. Backmarking (Gaschnig ‘77): • The 2 arrays: ak2

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

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

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

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

  39. BackM(depth, out: checkdepth, backup) For k= 1 to ndepthdo zdepth := adepth,k ; Ifcheckdepth(depth,k) backup(depth) Then Check all constraints c(zi, zdepth) with 1  i  depth until one fails; If none failed then … … … End-For The Backmark algorithm: *property 1 applied* backup(depth) 1 *property 2 applied*

  40. z1 . . . . . . . . . . . . . . . . . . 2 2 z2 . . . . . . . 1 c(z1,z2). . . . v v 1 x 1 x 2 v 2 v x 3 x 3 x 4 x 4 x z3 . . . . 1 x 2 v v 3 x 4 v x 3 x 4 v x c(z1,z3). . 1 x 2 v c(z2,z3). . . . z4 . . . c(z1,z4). . c(z2,z4). . . v v c(z3,z4). . . . . . . . . Backmarking at work: v

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

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

  43. 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. • Key idea: During the construction of the tree, infer and store“no-goods” , Use the “no-goods” to improve the backward behavior. • A no-good is: A set of assignments of values to variables that cannot occur simultaniously in any solution.

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

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

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

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

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

  50. Dynamic Search rearrangement

More Related