470 likes | 683 Views
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
E N D
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 • Consistency and relaxation • Hybrid constraint techniques • Some applications: • Understanding line drawings • Disambiguating natural language
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:
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)
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
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)
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) ?
Defining Constraint problems Definition File rouge
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, ij, a constraint c(zi, zj) • ex.: zizjzi+izj-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.
… 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:
Confused q-queens: = identical, BUT each 2 queens MUST attack each other. • C-4-queens:
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)|
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) = zizj |zi-zj| |i-j|
… 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:
Representing the search Or-tree representation Constraint Networks
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 … … …
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),ji • 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)
{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
{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)
Backtrack algorithms Chronological Backtracking Backjumping Backmarking Intelligent Backtracking Dynamic Search Rearrangement
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
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 )
Backjumping Avoid trashing
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!
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
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
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
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; Ifjumpbackdepththenreturn; The backjump algorithm: checkdepthk:= deepest i checked; checkdepthk = 2 checkdepthl = m jumpback:= max(checkdepthk)
Backmarking Avoiding other redundancies
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
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:
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.
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)
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
? ? 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.
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.
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):
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):
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*
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
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:
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.
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” • The first-fail principle: If assigning a value to zi is more likely to fail than assigning to zj: Then: assing to zi first.
zi . . . zj . . . ai1 v x … aik x aj1 v v aj2 x ajl v v c(z1,zi). . c(z1,zj). . c(z2,zi). . x c(z2,zj) . . zi . . . ai1 v x … aik x ai1 v x … aik x c(z1,zi). . c(z2,zi). Effect? • 1. Suppose our guess was right… Done • … then we gain in any case Smaller search tree !
zj . . . zj . . . aj1 v v aj1 v v aj2 x aj2 x ajl v v ajl v v c(z1,zj). . c(z1,zj). . c(z2,zj) . . c(z2,zj) . . zi . . . ai1 v v … aik x c(z1,zi). . c(z2,zi). . x zi . . . ai1 v v … aik x ai1 v v … aik x c(z1,zi). . c(z2,zi). Effect (2)? • 2. Suppose our guess was only partly right … Assingment to zi does not fail, but has less successes ! • … we still gain: Less checks are redone !
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: • + Heuristics based on problem at hand. z1 c(z1,z2) c(z1,z4) = “true” c(z1,z3) z4 z2 c(z2,z4) “true” = c(z2,z3) c(z3,z4) = “true” z3 Some general first-fail heuristics: