2.06k likes | 2.07k Views
Introduction to Constraint Programming. Peter van Beek University of Waterloo. Patrick Prosser Christian Schulte. Michael Chase Abid Malik Tyrel Russell. Acknowledgements. Outline. Introduction Constraint propagation Backtracking search Global constraints Symmetry Modeling.
E N D
Introduction to Constraint Programming • Peter van Beek • University of Waterloo
Patrick Prosser Christian Schulte Michael Chase Abid Malik Tyrel Russell Acknowledgements
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 “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
Outline • Introduction • Constraint propagation • Backtracking search • Global constraints • Symmetry • Modeling
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 • Constraint programming is an active area of research • draws on techniques from artificial intelligence, algorithms, databases, programming languages, and operations research
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
What is constraint programming? • 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
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
? ? ? ? ? ? ? ? Place numbers 1 through 8 on nodes, where each number appears exactly once and no connected nodes have consecutive numbers Acknowledgement: Patrick Prosser
? ? ? ? ? ? ? ? Backtracking search Guess a value, but be prepared to backtrack Which nodes are hardest to number?
? ? ? ? ? ? ? ? Backtracking search Which nodes are hardest to number?
? ? ? ? ? ? ? ? Backtracking search Which are the least constraining values to use? Values 1 & 8
? ? ? 1 8 ? ? ? Backtracking search Symmetry means we don’t need to consider: 8 1
? ? ? 1 8 ? ? ? Inference/propagation {1,2,3,4,5,6,7,8} We can now eliminate many values for other nodes
? ? ? 1 8 ? ? ? Inference/propagation {3,4,5,6} {3,4,5,6} By symmetry
? ? ? 1 8 ? ? ? Inference/propagation {3,4,5,6} {1,2,3,4,5,6,7,8} {3,4,5,6}
? ? ? 1 8 ? ? ? Inference/propagation {3,4,5,6} {3,4,5,6} {3,4,5,6} {3,4,5,6} By symmetry
? ? ? 1 8 ? ? ? Inference/propagation {3,4,5,6} {3,4,5,6} {3,4,5,6,7} {2,3,4,5,6} {3,4,5,6} {3,4,5,6}
? ? 7 1 8 2 ? ? Inference/propagation {3,4,5,6} {3,4,5,6} {3,4,5,6} {3,4,5,6}
? ? 7 1 8 2 ? ? Inference/propagation {3,4,5,6} {3,4,5,6} {3,4,5,6} {3,4,5,6} And propagate
? ? 7 1 8 2 ? ? Inference/propagation {3,4,5} {4,5,6} {3,4,5} {4,5,6} Guess a value, but be prepared to backtrack
3 ? 7 1 8 2 ? ? Inference/propagation {4,5,6} {3,4,5} {4,5,6} Guess a value, but be prepared to backtrack
3 ? 7 1 8 2 ? ? Inference/propagation {4,5,6} {3,4,5} {4,5,6} And propagate
3 ? 7 1 8 2 ? ? Inference/propagation {5,6} {4,5} {4,5,6} More propagation?
3 5 7 1 8 2 4 6 Inference/propagation A solution
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 Constraint Satisfaction Problem
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
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
Constraint languages • Usual arithmetic operators: • =, , , < , > , , + , , *, /, absolute value, exponentiation • e.g., 3x + 4y 7, 5x3 – x*y = 9 • Usual logical operators: • , , , (or “if … then”) • e.g., if x = 1 then y = 2, x y z,(3x + 4y 7) (x*y = z) • Global constraints: • alldifferent(x1, …, xn) pairwise different • cardinality(x1, …, xn, l, u) each value must be assigned to at least l variables and at most u variables • Table constraints
? ? ? ? ? ? ? ? Constraint model for puzzle variables v1, …, v8 domains {1, …, 8} constraints | v1 – v2 | 1 | v1 – v3 | 1 … | v7 – v8 | 1 alldifferent(v1, …, v8)
Example: Instruction scheduling Given a basic-block of code and a multiple-issue pipelined processor, find the minimum length schedule (a + b) + c
dependency DAG 3 3 A B 3 D C 1 E Example: evaluate (a + b) + c instructions A r1 a B r2 b C r3 c D r1 r1 + r2 E r1 r1 + r3
dependency DAG 3 3 A B 3 D C 1 E Example: evaluate (a + b) + c non-optimal schedule A r1 a B r2 b nop nop D r1 r1 + r2 C r3 c nop nop E r1 r1 + r3
dependency DAG 3 3 A B 3 D C 1 E Example: evaluate (a + b) + c optimal schedule A r1 a B r2 b C r3 c nop D r1 r1 + r2 E r1 r1 + r3
dependency DAG 3 3 A B 3 D C 1 E Constraint model variables A, B, C, D, E domains {1, …, m} constraints D A + 3 D B + 3 E C + 3 E D + 1 cardinality(A, B, C, D, E, 0, width)
Example: Boolean satisfiability (x1 x2 x4) (x2 x4 x5) (x3 x4 x5) Given a Boolean formula, does there exist a satisfying assignment
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)
Example: 3-SAT A solution x1 = false x2 = false x3 = false x4 = true x5 = false (x1 x2 x4) (x2 x4 x5) (x3 x4 x5)
Example: Graph coloring Given k colors, does there exist a coloring of the nodes such that adjacent nodes are assigned different colors
Example: 3-coloring variables: v1, v2 , v3 , v4 , v5 domains: {1, 2, 3} constraints: vi vjif vi and vj are adjacent v1 v2 v3 v4 v5
Example: 3-coloring A solution v1 = 1 v2 = 2 v3 = 2 v4 = 1 v5 = 3 v1 v2 v3 v4 v5
Example: n-queens Place n-queens on an n n board so that no pair of queens attacks each other
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
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
A closer look at constraints • An assignment (also called an instantiation) • x = a, where adom(x), • A tuplet over an ordered set of variables {x1, …, xk} is an ordered list of values (a1, …, ak) such that aidom(xi), i = 1, …, k • can be viewed as a set of assignments {x1 = a1, …, xk = ak} • Given a tuple t, notation t[xi] selects out the value for variable xi; i.e.,t[xi] = ai
A closer look at constraints • Each constraint C is a relation • a set of tuples over some ordered subset of the variables, denoted by vars(C) • specifies the allowed combinations of values for the variables in vars(C) • The size of vars(C) is known as the arity of the constraint • a unary constraint has an arity of 1 • a binary constraint has an arity of 2 • a non-binary constraint has arity greater than 2
intensional extensional Example • Let • dom(x1) = {1, 2, 3, 4}, • dom(x2) = {1, 2, 3, 4} • C be the constraint x1 x2 | x1– x2 | 1 • Then • vars(C) = {x1,x2} • tuples in C = {(1,3), (1,4), (2,4), (3,1), (4,1), (4,2)} • C is a binary constraint (table constraint)
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 Constraint Satisfaction Problem