© J. Christopher Beck Lecture 13: Modeling in Constraint Programming
© J. Christopher Beck Outline Introduction The Crystal Maze Heuristics, Propagation, B&B Modeling in CP
© J. Christopher Beck Readings P Ch 5.5, D.2, D.3 B.M. Smith, “Modelling”, The Handbook of Constraint Programming, 2006.
© J. Christopher Beck Crystal Maze Place the numbers 1 through 8 in the nodes such that: Each number appears exactly once ? ? ? ? ? ? ?? –No connected nodes have consecutive numbers You have 5 minutes!
© J. Christopher Beck Modeling Each node a variable {1, …, 8} values in the domain of each variable No consecutive numbers a constraint (v i, v j ) |v i – v j | > 1 All values used all-different constraint
© J. Christopher Beck Heuristic Search ? ? ? ? ? ? ?? 18 {1, 2, 3, 4, 5, 6, 7, 8}
© J. Christopher Beck Inference/Propagation ? ? ? ? ? ? ?? 18 {1, 2, 3, 4, 5, 6, 7, 8}
© J. Christopher Beck Inference/Propagation ? ? ? ? ? ? ?? 18 {3, 4, 5, 6} {3, 4, 5, 6, 7}{2, 3, 4, 5, 6}
© J. Christopher Beck Generic CP Algorithm Assert Commitment Propagators Start Success Solution? Make Heuristic Decision Backtrack Technique Failure Nothing to retract? Dead-end?
© J. Christopher Beck With CP You Are … Guaranteed to find a solution if one exists If not you can prove there is no solution But you need to have enough time! On average, for many problems you can find a solution within a reasonable time CP is commercially successful for scheduling and other applications Questions?
© J. Christopher Beck The Core of CP Modeling How to represent the problem Heuristic search How to branch How much effort to find a good branch Inference/propagation How much effort Backtracking
© J. Christopher Beck Constraint Satisfaction Problem (CSP) Given: V, a set of variables {v 0, v 1, …, v n } D, a set of domains {D 0, D 1, …, D n } C, a set of constraints {c 0, c 1, …, c m } Each constraint, c i, has a scope c i (v 0, v 2, v 4, v 117, …), the variables that it constrains
© J. Christopher Beck Constraint Satisfaction Problem (CSP) A constraint, c i, is a mapping from the elements of the Cartesian product of the domains of the variables in its scope to {T,F} c i (v 0, v 2, v 4, v 117, …) maps: (D 0 X D 2 X D 4 X D 117 X … ) {T,F} A constraint is satisfied if the assignment of the variables in its scope are mapped to T
© J. Christopher Beck Constraint Satisfaction Problem (CSP) In a solution to a CSP: each variable is assigned a value from its domain: v i = d i, d i є D i each constraint is satisfied
© J. Christopher Beck Variables & Constraints Variables can be anything colors, times, countries, … often integer valued or binary Constraints are not limited to linear constraints extensional: list of acceptable value- combinations intensional: mathematical expression
© J. Christopher Beck Examples V = {v 1, v 2, …, v n } D i = {1, 2, …, n} all-diff(v 1, v 2, …, v n ) each of the variables must take on a different value v 1 > v 2 + v 4 v 4 = v 5 * v 6
© J. Christopher Beck n-Queens Problem Place n-Queens on the chess board so that no queen can attack any other
© J. Christopher Beck n-Queens Problem Variables? Constraints?
© J. Christopher Beck n-Queens Problem How about a different model?
© J. Christopher Beck Why Do We Care About Modeling? You need to represent your problem! As in MIP/LP different models are harder or easier to solve We will return to CP modeling (in a big way) in a few weeks