Download presentation
Presentation is loading. Please wait.
Published byBrittany Byrd Modified over 9 years ago
1
Constraint processing An efficient alternative for search
2
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
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
4
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
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
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
7
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
8
Defining Constraint problems Definition File rouge
9
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
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
11 Confused q-queens: = identical, BUT each 2 queens MUST attack each other. C-4-queens:
12
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
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
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
15
Representing the search Or-tree representation Constraint Networks
16
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
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
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
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!
20
Backtrack algorithms Chronological Backtracking BackjumpingBackmarking Intelligent Backtracking Dynamic Search Rearrangement
21
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
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
23
Backjumping Avoid trashing
24
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
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
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
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
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 )
29
Backmarking Avoiding other redundancies
30
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
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
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
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
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
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
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
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
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
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
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
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
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.
43
Dynamic Search rearrangement
44
44 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:
45
45 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
46
46 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
47
47 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.