1 / 65

Constraint Programming

Constraint Programming. Peter van Beek Computer Science. Outline. Introduction Constraint propagation Backtracking search Global constraints Symmetry Modeling. Some additional resources. “Handbook of Constraint Programming,” edited by F. Rossi, P. van Beek, T. Walsh.

yazid
Télécharger la présentation

Constraint Programming

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 Programming • Peter van Beek • Computer Science

  2. Outline • Introduction • Constraint propagation • Backtracking search • Global constraints • Symmetry • Modeling

  3. Some additional resources “Handbook of Constraint Programming,” edited by F. Rossi, P. van Beek, T. Walsh “Constraint-Based Local Search”, by Pascal Van Hentenryck and Laurent Michel Integrated Methods for Optimization, by John N. Hooker “Programming with Constraints,” by Kim Marriott, Peter J. Stuckey “Principles of Constraint Programming,” by Krzysztof Apt “Constraint Processing,” by Rina Dechter

  4. Outline • Introduction • Constraint propagation • Backtracking search • Global constraints • Symmetry • Modeling

  5. What is constraint programming? • Idea: Solve a problem by stating constraints on acceptable solutions • Advantages: • constraints often a natural part of problems • especially true of difficult combinatorial problems • once problem is modeled using constraints, wide selection of solution techniques available

  6. What is constraint programming? • Constraint programming is similar to mathematical programming • declarative • user states the constraints • general purpose constraint solver, often based on backtracking search, is used to solve the constraints • Constraint programming is similar to computer programming • extensible • user-defined constraints • allows user to program a strategy to search for a solution

  7. What is constraint programming? • Constraint programming is a collection of core techniques • Modeling • deciding on variables/domains/constraints • improving the efficiency of a model • Solving • local consistency • constraint propagation • global constraints • search • backtracking search • hybrid methods

  8. Constraint programming methodology • Constraint programming is a problem-solving methodology • Model problem • Solve model • specify in terms of constraints on acceptable solutions • define/choose constraint model: • variables, domains, constraints • define/choose search algorithm • define/choose heuristics Constraint Satisfaction Problem

  9. Constraint satisfaction problem (CSP) • A CSP is defined by: • a set of variables {x1, …, xn} • a set of values for each variable dom(x1), …, dom(xn) • a set of constraints {C1, …, Cm} • A solution to a CSP is a complete assignment to all the variables that satisfies the constraints • Given a CSP: • determine whether it has a solution or not • find one solution • find all solutions • find an optimal solution, given some cost function

  10. Example domains and constraints • Reals, linear constraints • 3x + 4y ≤ 7, 5x – 3y + z = 2 • Guassian elimination, linear programming • Integers, linear constraints • integer linear programming, branch-and-bound • Boolean values, clauses • Here: • finite domains • rich constraint languages • user-defined constraints • global constraints

  11. Constraint languages • Usual arithmetic operators: • =, , , < , > ,  , + , , *, /, absolute value, exponentiation • e.g., 3x + 4y  7, 5x3 – x*y = 9 • Usual logical operators: • , , ,  • e.g., (x = 1)  (y = 2), x  y  z,(3x + 4y  7)  (x*y = z) • Global constraints: • e.g., alldifferent(x1, …, xn) • Table constraints

  12. Alldifferent constraint • Consists of: • set of variables {x1, …, xn} • Satisfied iff: • each of the variables is assigned a different value

  13. Sudoku Each Sudoku has a unique solution that can be reached logically without guessing. Enter digits from 1 to 9 into the blank spaces. Every row must contain one of each digit. So must every column, as must every 3x3 square.

  14. Sudoku

  15. dom(xi) = {1, …, 9}, for all i = 1, …, 81 alldifferent(x1, x2, x3, x4, x5, x6, x7, x8, x9) … alldifferent(x1, x10, x19, x28, x37, x46, x55, x64, x73) … alldifferent(x1, x2, x3, x10, x11, x12, x19, x20, x21) … x1 = 5, x2 = 3, x5 = 7, …, x81 = 9 Sudoku

  16. Example: Boolean satisfiability (x1  x2  x4)  (x2  x4  x5)  (x3  x4  x5) Given a Boolean formula, does there exist a satisfying assignment

  17. Constraint model variables: x1, x2 , x3 , x4 , x5 domains: {true, false} constraints: (x1  x2  x4) (x2  x4  x5) (x3  x4  x5) (x1  x2  x4)  (x2  x4  x5)  (x3  x4  x5)

  18. Example: n-queens Place n-queens on an n  n board so that no pair of queens attacks each other

  19. Constraint model variables: x1, x2 , x3 , x4 domains: {1, 2, 3, 4} constraints: x1  x2 | x1 – x2 | 1 x1  x3 | x1 – x3 | 2 x1  x4 | x1 – x4 | 3 x2  x3 | x2 – x3 | 1 x2  x4 | x2 – x4 | 2 x3  x4 | x3 – x4 | 1 x1 x2 x3 x4 1 2 3 4

  20. Example: 4-queens x1 x2 x3 x4 A solution x1 = 2 x2 = 4 x3 = 1 x4 = 3 Q 1 Q 2 Q 3 Q 4

  21. Some commercial applications

  22. Outline • Introduction • Constraint propagation • Backtracking search • Global constraints • Soft constraints • Symmetry • Modeling

  23. Fundamental insight: Local consistency • A local inconsistency is an instantiation of some of the variables that satisfies the relevant constraints but: • cannot be extended to one or more additional variables • so cannot be part of any solution • Has led to: • definitions of conditions that characterize the level of local consistency of a CSP • algorithms which enforce these levels of local consistency by removing inconsistencies from the CSP • effective backtracking algorithms for finding solutions to a CSP that maintain a level of local consistency during the search

  24. Enforcing local consistency: constraint propagation • Here, focus on: Given a constraint, remove a value from the domain of a variable if it cannot be part of a solution according to that constraint • Example of local consistency: arc consistency

  25. Generic arc consistency algorithm ac() : boolean • Q all variable/constraint pairs (x, C) • while Q {} do • select and remove a pair (x, C) from Q • if revise(x, C) then • if dom(x) = {} • return false • else • add pairs to Q • return true revise(x, C) : boolean change  false for each vdom(x) do if  tC s.t. t[x] = v then remove v from dom(x) change  true return change

  26. Generic arc consistency algorithm ac() : boolean • Q all variable/constraint pairs (x, C) • while Q {} do • select and remove a pair (x, C) from Q • if revise(x, C) then • if dom(x) = {} • return false • else • add pairs to Q • return true variable x y z domain {1, 2, 3} {1, 2, 3} {1, 2, 3} constraints C1: x< y C2: y < z revise(x, C) : boolean change  false for each vdom(x) do if  tC s.t. t[x] = v then remove v from dom(x) change  true return change

  27. Improvements • Much work on efficient algorithms for table constraints • Special purpose algorithms for global constraints (coming later)

  28. Outline • Introduction • Constraint propagation • Backtracking search • Global constraints • Symmetry • Modeling

  29. Constraint programming methodology • Model problem • Solve model • specify in terms of constraints on acceptable solutions • define/choose constraint model: • variables, domains, constraints • define/choose search algorithm • define/choose heuristics

  30. Backtracking search • CSPs often solved using backtracking search • Many techniques for improving efficiency of a backtracking search algorithm • branching strategies, constraint propagation, nogood recording, non-chronological backtracking (backjumping), heuristics for variable and value ordering, portfolios and restart strategies

  31. Backtracking search • A backtracking search is a depth-first traversal of a search tree • search tree is generated as the search progresses • search tree represents alternative choices that may have to be examined in order to find a solution • method of extending a node in the search tree is often called a branching strategy

  32. Generic backtracking algorithm treeSearch( i : integer ) : integer • if all variables assigned a value then • return 0 // solution found • x getNextVariable( ) • backtrackLevel i • for each branching constraintbi do • post( bi ) • if propagate( bi ) then • backtrackLevel treeSearch( i + 1 ) • undo( bi ) • if backtrackLevel < i then • return backtrackLevel • backtrackLevel getBacktrackLevel() • setNogoods() • return backtrackLevel

  33. Outline • Introduction • Constraint propagation • Backtracking search • branching strategies • constraint propagation • non-chronological backtracking • nogood recording • heuristics for variable and value ordering • portfolios and restart strategies • Global constraints • Symmetry • Modeling

  34. p = {b1, …, bj} … p {bj+1} p {bj+1} 1 k Branching strategies • A node p = {b1, …, bj} in the search tree is a set of branching constraints, where bi, 1 ≤ i ≤ j, is the branching constraint posted at level i in search tree • A node p is extended by posting a branching constraint • to ensure completeness, the constraints posted on all the branches from a node must be mutually exclusive and exhaustive

  35. Popular branching strategies • Running example: let x be the variable branched on, let dom(x) = {1, …, 6} • Enumeration (or d-way branching) • variable x is instantiated in turn to each value in its domain • e.g., x = 1 is posted along the first branch, x = 2 along second branch, … • Binary choice points (or 2-way branching) • variable x is instantiated to some value in its domain • e.g., x = 1 is posted along the first branch, x 1 along second branch, respectively • Domain splitting • constraint posted splits the domain of the variable • e.g., x 3 is posted along the first branch, x> 3 along second branch, respectively

  36. Outline • Introduction • Constraint propagation • Backtracking search • branching strategies • constraint propagation • non-chronological backtracking • nogood recording • heuristics for variable and value ordering • portfolios and restart strategies • Global constraints • Symmetry • Modeling

  37. Constraint propagation • Effective backtracking algorithms for constraint programming maintain a level of local consistency during the search; i.e., perform constraint propagation • A generic scheme to maintain a level of local consistency in a backtracking search is to perform constraint propagation at each node in the search tree • if any domain of a variable becomes empty, inconsistent so backtrack

  38. Constraint propagation • Backtracking search integrated with constraint propagation has two important benefits 1. removing inconsistencies during search can dramatically prune the search tree by removing deadends and by simplifying the remaining sub-problem 2. some of the most important variable ordering heuristics make use of the information gathered by constraint propagation

  39. Maintaining a level of local consistency • Definitions of local consistency can be categorized by whether: • only unary constraints need to be posted during constraint propagation; sometimes called domain filtering • higher arity constraints may need to be posted • In implementations of backtracking • domains represented extensionally • posting and retracting unary constraints can be done very efficiently • important that algorithms for enforcing a level of local consistency be incremental

  40. Some backtracking algorithms • Chronological backtracking (BT) • naïve backtracking: performs no constraint propagation, only checks a constraint if all of its variables have been instantiated; chronologically backtracks • Forward checking (FC) • maintains arc consistency on all constraints with exactly one uninstantiated variable; chronologically backtracks • Maintaining arc consistency (MAC) • maintains arc consistency on all constraints with at least one uninstantiated variable; chronologically backtracks • Conflict-directed backjumping (CBJ) • backjumps; no constraint propagation

  41. Constraint model for 4-queens variables: x1, x2 , x3 , x4 domains: {1, 2, 3, 4} constraints: x1  x2 | x1 – x2 | 1 x1  x3 | x1 – x3 | 2 x1  x4 | x1 – x4 | 3 x2  x3 | x2 – x3 | 1 x2  x4 | x2 – x4 | 2 x3  x4 | x3 – x4 | 1 x1 x2 x3 x4 1 2 3 4

  42. Search tree for 4-queens x1=1 x1= 4 x1 x2 x3 x4 (1,1,1,1) (2,4,1,3) (3,1,4,2) (4,4,4,4)

  43. Q Q Q Q Q Q … Q Q Q Q Chronological backtracking (BT)

  44. Enforce arc consistency on constraints with exactly one variable uninstantiated x1 x2 x3 x4 1 2 3 4 Forward checking (FC) Q { x1 = 1} constraints: x1 x2  |x1 – x2|  1 x1x3  |x1– x3|  2 x1x4  |x1– x4|  3

  45. Q Q Q Q … Q Forward checking (FC) on 4-queens Q

  46. Enforce arc consistency on constraints with at least one variable uninstantiated x1 x2 x3 x4 1 2 3 4 Maintaining arc consistency (MAC) { x1 = 1} Q constraints: x1  x2 | x1 – x2 | 1 x1  x3 | x1 – x3 | 2 x1  x4 | x1 – x4 | 3 x2  x3 | x2 – x3 | 1 x2  x4 | x2 – x4 | 2 x3  x4 | x3 – x4 | 1 ?

  47. Q Maintaining arc consistency (MAC) on 4-queens Q Q Q Q √

  48. Outline • Introduction • Constraint propagation • Backtracking search • branching strategies • constraint propagation • non-chronological backtracking • nogood recording • heuristics for variable and value ordering • portfolios and restart strategies • Global constraints • Symmetry • Modeling

  49. Non-chronological backtracking • Upon discovering a deadend, a backtracking algorithm must retract some previously posted branching constraint • chronological backtracking: only the most recently posted branching constraint is retracted • non-chronological backtracking: algorithm backtracks to and retracts the closest branching constraint which bears some responsibility for the deadend

  50. x1 x2 x3 x4 1 {x1 = 1} 2 3 {x1 = 1,x2 = 3} 4 {x1 = 1,x2 = 3 ,x4 = 2} Conflict-directed backjumping (CBJ) Q x1 Q x2 Q x1 x2 x3

More Related