Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Constraint Satisfaction Problems Introduction Organization Introduction

Bernhard Nebel, Julien Hu´e, and Stefan W¨olfl

Albert-Ludwigs-Universit¨atFreiburg

April 23, 2012 Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Introduction Constraint Satisfaction Problems Real World Applications Introduction Solving Constraints Contents of the lecture Organization Constraints

Constraint What is a constraint? Satisfaction Problems 1 a: the act of constraining b: the state of being checked, Nebel, Hu´e restricted, or compelled to avoid or perform some action . . . and W¨olfl

c: a constraining condition, agency, or force . . . Introduction Constraint 2 a: repression of one’s own feelings, behavior, or actions Satisfaction Problems b: a sense of being constrained . . . Real World Applications Solving (from Merriam-Webster’s Online Dictionary) Constraints Contents of the lecture Usage Organization In programming languages, constraints are often used to restrict the domains of variables. In databases, constraints can be used to specify integrity conditions. In mathematics, a constraint is a requirement on solutions of optimization problems. Examples

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Examples: Introduction Constraint Satisfaction Latin squares Problems Real World Applications Eight queens problem Solving Constraints Contents of the lecture Map coloring problem Organization Boolean satisfiability

Constraint Satisfaction Problems Problem: Nebel, Hu´e How can one fill an n × n table with n different symbols and W¨olfl . . . such that each symbol occurs exactly once in each row Introduction Constraint Satisfaction and and each column? Problems Real World Applications Solving   Constraints 1 2 4 3 Contents of the 1 2 3 lecture 1 2 2 3 1 4 1 2 3 1   Organization 2 1   3 4 2 1 3 1 2   4 1 3 2 There are essentially 56 different Latin squares of size 5, 9408 squares of size 6, 16.942.080 squares of size 7, 535.281.401.856 squares of size 8, . . . Latin Square

Constraint Satisfaction Problems Problem: Nebel, Hu´e How can one fill an n × n table with n different symbols and W¨olfl . . . such that each symbol occurs exactly once in each row Introduction Constraint Satisfaction and and each column? Problems Real World Applications Solving   Constraints 1 2 3 4 Contents of the 1 2 3 lecture 1 2 2 3 4 1 1 2 3 1   Organization 2 1   3 4 1 2 3 1 2   4 1 2 3 There are essentially 56 different Latin squares of size 5, 9408 squares of size 6, 16.942.080 squares of size 7, 535.281.401.856 squares of size 8, . . . Eight Queens Puzzle

Constraint Satisfaction Problem: Problems How can one put 8 queens on a standard board Nebel, Hu´e (8 × 8-board) and W¨olfl Introduction . . . such that no can attack any other queen? Constraint Satisfaction Problems Real World Applications Solutions: Solving Constraints Contents of the The puzzle has 12 unique solutions (up to rotations and lecture reflections) Organization

Old problem proposed in 1848. Various variants knights (instead of queens) 3D n queens on an n × n-board A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0Z0Z0Z Introduction 7 Constraint Z0Z0Z0Z0 Satisfaction Problems 6 Real World 0Z0Z0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0Z0Z0Z0Z lecture Organization 3 Z0Z0Z0Z0 2 qZ0Z0Z0Z 1 Z0Z0Z0Z0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0Z0Z0Z Introduction 7 Constraint Z0Z0Z0Z0 Satisfaction Problems 6 Real World 0Z0Z0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0Z0Z0 2 qZ0Z0Z0Z 1 Z0Z0Z0Z0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0Z0Z0Z Introduction 7 Constraint Z0Z0Z0Z0 Satisfaction Problems 6 Real World 0ZqZ0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0Z0Z0 2 qZ0Z0Z0Z 1 Z0Z0Z0Z0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0l0Z0Z Introduction 7 Constraint Z0Z0Z0Z0 Satisfaction Problems 6 Real World 0ZqZ0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0Z0Z0 2 qZ0Z0Z0Z 1 Z0Z0Z0Z0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0l0Z0Z Introduction 7 Constraint Z0Z0Z0Z0 Satisfaction Problems 6 Real World 0ZqZ0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0l0Z0 2 qZ0Z0Z0Z 1 Z0Z0Z0Z0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0l0Z0Z Introduction 7 Constraint Z0Z0Z0Z0 Satisfaction Problems 6 Real World 0ZqZ0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0l0Z0 2 qZ0Z0Z0Z 1 Z0Z0ZqZ0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0l0Z0Z Introduction 7 Constraint Z0Z0Z0l0 Satisfaction Problems 6 Real World 0ZqZ0Z0Z Applications 5 Solving Z0Z0Z0Z0 Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0l0Z0 2 qZ0Z0Z0Z 1 Z0Z0ZqZ0 a b c d e f g h

Figure: A solution of the 8-queens problem A Solution . . .

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl 8 0Z0l0Z0Z Introduction 7 Constraint Z0Z0Z0l0 Satisfaction Problems 6 Real World 0ZqZ0Z0Z Applications 5 Solving Z0Z0Z0Zq Constraints Contents of the 4 0l0Z0Z0Z lecture Organization 3 Z0Z0l0Z0 2 qZ0Z0Z0Z 1 Z0Z0ZqZ0 a b c d e f g h

Figure: A solution of the 8-queens problem Sudoku

Constraint Problem: Satisfaction Problems Fill a partially completed 9 × 9 grid such that Nebel, Hu´e . . . each row, each column, and each of the nine 3 × 3 and W¨olfl boxes contains the numbers from 1 to 9. Introduction Constraint Satisfaction Problems 2 5 3 9 1 Real World Applications Solving 1 4 Constraints Contents of the 4 7 2 8 lecture Organization 5 2 9 8 1 4 3 3 6 7 2 7 3 9 3 6 4 Sudoku

Constraint Problem: Satisfaction Problems Fill a partially completed 9 × 9 grid such that Nebel, Hu´e . . . each row, each column, and each of the nine 3 × 3 and W¨olfl boxes contains the numbers from 1 to 9. Introduction Constraint Satisfaction Problems 2 5 8 7 3 6 9 4 1 Real World Applications Solving 6 1 9 8 2 4 3 5 7 Constraints Contents of the 4 3 7 9 1 5 2 6 8 lecture Organization 3 9 5 2 7 1 4 8 6 7 6 2 4 9 8 1 3 5 8 4 1 6 5 3 7 2 9 1 8 4 3 6 9 5 7 2 5 7 6 1 4 2 8 9 3 9 2 3 5 8 7 6 1 4 Constraint Satisfaction Problem

Constraint Satisfaction Problems

Nebel, Hu´e Definition and W¨olfl A constraint network is defined by: Introduction Constraint a finite set of variables Satisfaction Problems Real World a (finite) domain of values for each variable Applications Solving Constraints a finite set of constraints (i.e., binary, ternary, . . . relations Contents of the defined between the variables) lecture Organization

Problem Is there a solution of the network, i.e., an assignment of values to the variables such that all constraints are satisfied? k-Colorability

Constraint Satisfaction Problem: Problems Nebel, Hu´e Can one color the nodes of a given graph with k colors and W¨olfl . . . such that all nodes connected by an edge have Introduction different colors? Constraint Satisfaction Problems Real World Applications Reformulated as a constraint network: Solving Constraints Contents of the Variables: the nodes of the graph lecture Domains: “colors” {1, . . . , k} for each variable Organization Constraints: nodes connected by an edge must have different values This constraint network has a particular restricted form: only binary constraints domains are finite k-Colorability

Constraint Satisfaction Problem: Problems Nebel, Hu´e Can one color the nodes of a given graph with k colors and W¨olfl . . . such that all nodes connected by an edge have Introduction different colors? Constraint Satisfaction Problems Real World Applications Reformulated as a constraint network: Solving Constraints Contents of the Variables: the nodes of the graph lecture Domains: “colors” {1, . . . , k} for each variable Organization Constraints: nodes connected by an edge must have different values This constraint network has a particular restricted form: only binary constraints domains are finite k-Colorability

Constraint Satisfaction Problem: Problems Nebel, Hu´e Can one color the nodes of a given graph with k colors and W¨olfl . . . such that all nodes connected by an edge have Introduction different colors? Constraint Satisfaction Problems Real World Applications Reformulated as a constraint network: Solving Constraints Contents of the Variables: the nodes of the graph lecture Domains: “colors” {1, . . . , k} for each variable Organization Constraints: nodes connected by an edge must have different values This constraint network has a particular restricted form: only binary constraints domains are finite Crossword Puzzle

Constraint Problem instance: Satisfaction Problems

Variables: empty squares in a crossword puzzle; Nebel, Hu´e Domains: letters {A, B, C, . . . , Z} for each variable; and W¨olfl Introduction Constraints: relations defined by a given set of words that Constraint Satisfaction need (or are allowed) to occur in the completed puzzle. Problems Real World Applications Solving 1 2 3 4 5 6 7 8 Constraints Contents of the lecture 9 10 11 12 13 Organization

14 15 16 17 18 19 20

21 22 23 24 25

Fill-in words: EIER, HOLZ, IE, IM, IT, NZ, ON, RAM, RE, ROLLE, ROT, ZAR, ZUHOERER Crossword Puzzle

Constraint Problem instance: Satisfaction Problems

Variables: empty squares in a crossword puzzle; Nebel, Hu´e Domains: letters {A, B, C, . . . , Z} for each variable; and W¨olfl Introduction Constraints: relations defined by a given set of words that Constraint Satisfaction need (or are allowed) to occur in the completed puzzle. Problems Real World Applications Solving Constraints Contents of the Z U H O E R E R lecture A O I E A Organization R O L L E I M N Z R O T

Fill-in words: EIER, HOLZ, IE, IM, IT, NZ, ON, RAM, RE, ROLLE, ROT, ZAR, ZUHOERER Boolean Satisfiability

Constraint Satisfaction Problems Problem instance (Boolean constraint network): Nebel, Hu´e and W¨olfl Variables: (propositional) variables; Introduction Constraint Domains: truth values {0, 1} for each variable; Satisfaction Problems Real World Constraints: defined by a propositional formula in these Applications Solving variables. Constraints Contents of the lecture

Example: (x1 ∨ ¬x2 ∨ ¬x3) ∧ (x1 ∨ x2 ∨ x4) Organization

SAT as a constraint satisfaction problem: Given an arbitrary Boolean constraint network, is the network solvable? Real World Applications

Constraint Satisfaction Problems

Nebel, Hu´e In practice, not only constraint satisfaction, but constraint and W¨olfl optimization is required. Introduction Constraint Satisfaction Seminar topic assignment Problems Real World Applications Given n students who want to participate in a seminar; Solving Constraints Contents of the m topics are available to be worked on by students; lecture each topic can be worked on by at most one student, and Organization each student has preferences which topics s/he would like to work on; . . . how to assign topics to students? Real World Applications

Constraint Satisfaction Problems

Nebel, Hu´e CSP/COP techniques can be used in and W¨olfl

civil engineering (design of power plants, water and energy Introduction Constraint Satisfaction supply, transportation and traffic infrastructure) Problems Real World mechanical engineering (design of machines, robots, Applications Solving Constraints vehicles) Contents of the lecture digital circuit verification Organization automated timetabling air traffic control finance Computational Complexity

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Theorem Introduction Constraint Satisfaction It is NP-hard to decide solvability of CSPs. Problems Real World Applications Solving Constraints Contents of the Since k-colorability (SAT, 3SAT) is NP-complete, solvability of lecture CSPs in general must be NP-hard. Organization

Question: Is CSP solvability in NP? Computational Complexity

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Theorem Introduction Constraint Satisfaction It is NP-hard to decide solvability of CSPs. Problems Real World Applications Solving Constraints Contents of the Since k-colorability (SAT, 3SAT) is NP-complete, solvability of lecture CSPs in general must be NP-hard. Organization

Question: Is CSP solvability in NP? Computational Complexity

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Theorem Introduction Constraint Satisfaction It is NP-hard to decide solvability of CSPs. Problems Real World Applications Solving Constraints Contents of the Since k-colorability (SAT, 3SAT) is NP-complete, solvability of lecture CSPs in general must be NP-hard. Organization

Question: Is CSP solvability in NP? Solving CSPs

Constraint Satisfaction Problems

Enumeration of all assignments and testing Nebel, Hu´e and W¨olfl . . . too costly Introduction search Constraint Satisfaction Problems numerous different strategies, often “dead” search paths Real World Applications Solving are explored extensively Constraints Contents of the Constraint propagation: elimination of obviously lecture Organization impossible values Interleaving backtracking and constraint propagation: constraint propagation at each generated search node Many other search methods, e. g., local/stochastic search, etc. Solving CSPs

Constraint Satisfaction Problems

Enumeration of all assignments and testing Nebel, Hu´e and W¨olfl . . . too costly Introduction Backtracking search Constraint Satisfaction Problems numerous different strategies, often “dead” search paths Real World Applications Solving are explored extensively Constraints Contents of the Constraint propagation: elimination of obviously lecture Organization impossible values Interleaving backtracking and constraint propagation: constraint propagation at each generated search node Many other search methods, e. g., local/stochastic search, etc. Solving CSPs

Constraint Satisfaction Problems

Enumeration of all assignments and testing Nebel, Hu´e and W¨olfl . . . too costly Introduction Backtracking search Constraint Satisfaction Problems numerous different strategies, often “dead” search paths Real World Applications Solving are explored extensively Constraints Contents of the Constraint propagation: elimination of obviously lecture Organization impossible values Interleaving backtracking and constraint propagation: constraint propagation at each generated search node Many other search methods, e. g., local/stochastic search, etc. Solving CSPs

Constraint Satisfaction Problems

Enumeration of all assignments and testing Nebel, Hu´e and W¨olfl . . . too costly Introduction Backtracking search Constraint Satisfaction Problems numerous different strategies, often “dead” search paths Real World Applications Solving are explored extensively Constraints Contents of the Constraint propagation: elimination of obviously lecture Organization impossible values Interleaving backtracking and constraint propagation: constraint propagation at each generated search node Many other search methods, e. g., local/stochastic search, etc. Solving CSPs

Constraint Satisfaction Problems

Enumeration of all assignments and testing Nebel, Hu´e and W¨olfl . . . too costly Introduction Backtracking search Constraint Satisfaction Problems numerous different strategies, often “dead” search paths Real World Applications Solving are explored extensively Constraints Contents of the Constraint propagation: elimination of obviously lecture Organization impossible values Interleaving backtracking and constraint propagation: constraint propagation at each generated search node Many other search methods, e. g., local/stochastic search, etc. Contents I

Constraint Satisfaction Problems

Introduction and mathematical background Nebel, Hu´e Sets, relations, graphs and W¨olfl

Constraint networks and satisfiability Introduction Constraint Binary constraint networks Satisfaction Problems Simple solution methods (backtracking, etc.) Real World Applications Solving Inference-based methods Constraints Contents of the Arc and path consistency lecture k-consistency and global consistency Organization Search methods Backtracking Backjumping Comparing different methods Stochastic local search Contents I

Constraint Satisfaction Problems

Introduction and mathematical background Nebel, Hu´e Sets, relations, graphs and W¨olfl

Constraint networks and satisfiability Introduction Constraint Binary constraint networks Satisfaction Problems Simple solution methods (backtracking, etc.) Real World Applications Solving Inference-based methods Constraints Contents of the Arc and path consistency lecture k-consistency and global consistency Organization Search methods Backtracking Backjumping Comparing different methods Stochastic local search Contents I

Constraint Satisfaction Problems

Introduction and mathematical background Nebel, Hu´e Sets, relations, graphs and W¨olfl

Constraint networks and satisfiability Introduction Constraint Binary constraint networks Satisfaction Problems Simple solution methods (backtracking, etc.) Real World Applications Solving Inference-based methods Constraints Contents of the Arc and path consistency lecture k-consistency and global consistency Organization Search methods Backtracking Backjumping Comparing different methods Stochastic local search Contents II

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Introduction Constraint Global constraints Satisfaction Problems Real World Constraint optimization Applications Solving Selected advanced topics Constraints Contents of the Expressiveness vs complexity of constraint formalisms lecture Qualitative constraint networks Organization Contents II

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Introduction Constraint Global constraints Satisfaction Problems Real World Constraint optimization Applications Solving Selected advanced topics Constraints Contents of the Expressiveness vs complexity of constraint formalisms lecture Qualitative constraint networks Organization Contents II

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Introduction Constraint Global constraints Satisfaction Problems Real World Constraint optimization Applications Solving Selected advanced topics Constraints Contents of the Expressiveness vs complexity of constraint formalisms lecture Qualitative constraint networks Organization Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Introduction

Organization Time, Location, Web Organization Lecturers Exercises Course goals Literature Lectures: Where, When, Web Page

Constraint Where Satisfaction Problems Bld. 101, Room 00-036 Nebel, Hu´e and W¨olfl When Introduction

Monday, 16:15–18:00 Organization Time, Location, Wednesday, 16:15–17:00 (+ exercises: 17:15–18:00) Web Lecturers Exercises Course goals No lectures Literature 14-05-2012 16-05-2012 28-05-2012 (Pentecost break) 30-05-2012 (Pentecost break)

Web Page http://www.informatik.uni-freiburg.de/~ki/teaching/ss12/csp/ Lecturers

Constraint Prof. Bernhard Nebel Satisfaction Problems Bld. 52, Room 00-029 Nebel, Hu´e Consultation: Wednesday, 14-15 and W¨olfl Phone: 0761/203-8221 Introduction Email: [email protected] Organization Time, Location, Web Lecturers Exercises Dr. Julien Hu´e Course goals Bld. 52, Room 00-041 Literature Phone: 0761/203-8234 Email: [email protected]

Dr. Stefan W¨olfl Bld. 52, Room 00-043 Phone: 0761/203-8228 Email: woelfl@informatik.uni-freiburg.de Exercises

Constraint Satisfaction Where Problems Nebel, Hu´e Bld. 101, Room 00-036 and W¨olfl

Introduction

Organization When Time, Location, Web Wednesday, 17:15–18:00 Lecturers Exercises Course goals Literature

Who Matthias Westphal Bld. 52, Room 00-041 Phone: 0761/203-8227 Email: [email protected] Course Prerequisites & Goals

Constraint Satisfaction Goals Problems Acquiring skills in constraint processing Nebel, Hu´e and W¨olfl Understanding the principles behind different solving Introduction techniques Organization Time, Location, Being able to read and understand research literature in Web Lecturers the area of constraint satisfaction Exercises Course goals Being able to complete a project (thesis) in this research Literature area

Prerequisites Basic knowledge in the area of AI Basic knowledge in formal logic Basic knowledge in theoretical computer science Exercises

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Exercise assignments Introduction Organization handed out on Wednesdays Time, Location, Web due on Wednesday in the following week (before the Lecturers Exercises Course goals lecture) Literature may be solved in groups of two students 50 % of reachable points are required for exam admission Programming project

Constraint Satisfaction Problems

Implement a CSP solver . . . Nebel, Hu´e Implementation tasks are specified on a regular basis and W¨olfl (depending on the progress of the lecture) Introduction Organization May be worked on in groups of two students Time, Location, Web Lecturers Programming language Exercises Course goals Implementation should compile on a standard Linux Literature computer (Ubuntu 11.10) We provide git repositories for source code Working solver is prerequisite for exam admission Will do a competition between solvers at the end of the lecture Examination

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Introduction Credit points Organization Time, Location, 6 ECTS points Web Lecturers Exercises Course goals Exams Literature (Oral or written) exam in September 2012 Acknowledgements

Constraint Satisfaction Problems

Nebel, Hu´e and W¨olfl

Lecture is based on slidesets of previous CSP lectures: Introduction Organization Time, Location, Malte Helmert and Stefan W¨olfl(summer term 2007) Web Lecturers Bernhard Nebel and Stefan W¨olfl(winter term 2009/10) Exercises Course goals Literature Special thanks to: Matthias Westphal, Robert Mattm¨uller, Gabriele R¨oger,Manuel Bodirsky Literature

Constraint Rina Dechter: Satisfaction Problems

Constraint Processing, Nebel, Hu´e Morgan Kaufmann, 2003. and W¨olfl Francesca Rossi, Peter van Beek, and Toby Walsh: Introduction Organization Handbook of Constraint Programming, Time, Location, Web Elsevier, 2006. Lecturers Exercises Course goals Wikipedia contributors: Literature Wikipedia, The Free Encyclopedia, http://en.wikipedia.org/ Wolfram Research: Wolfram MathWorld, http://mathworld.wolfram.com/

Further readings will be given during the lecture.