Solving Constraint Problems using Constraint Programming

Zeynep KIZILTAN Department of Computer Science and Engineering (DISI) University of Bologna Email: [email protected] What is it about? l Theory – The core of solving constraint problems using Constraint Programming (CP). – Brief overview of advanced topics. l Practice – Programming examples. – Project work. What is it about? l Theory – The core of solving constraint problems using Constraint Programming (CP). – Brief overview of advanced topics. l Practice – Programming examples. – Project work. PART 0:Introduction

• Overview of constraint problems & CP • Resources Constraints are everywhere!

l No meetings before 9am. l No registration of marks before the end of the term. l The lecture rooms have a capacity. l Two lectures of a student cannot overlap. l No two trains on the same track at the same time. l Salary > 45k Euros J … Constraint Problems

l A constraint is a restriction. l Problems that require to give a decision in the presence of constraints: – flight / train scheduling; – scheduling of events in an operating system; – staff rostering at a company; – course time tabling at a university … l Better called Problems (CSPs). A Puzzle

You have 3 minutes!

Put a different number in each node (1 to 8) such that adjacent nodes cannot take consecutive numbers Constraint Programming l User models her problem as a CSP. – Formalization of the unknowns of the decision, possible values for unknowns, and relations between the unknowns. l A constraint program solves it by guessing a value for each unknown using systematic search. – Backtracking – Consistency & constraint propagation – Heuristics to guide the search The Puzzle

l Place numbers 1 through 8 on nodes, s.t.: – each number appears exactly once; – no connected nodes have consecutive numbers.

? ?

? ? ? ?

? ? Modelling

l Unknowns: N1..N8 represent the nodes l Values that N1..N8 can take: {1,2,3,4,5,6,7,8} l Relations: for all i < j s.t. Ni and Nj are adjacent |Ni - Nj| > 1

for all i < j Ni ≠ Nj

N2 N5

N1 N3 N6 N8

N4 N7 Solving by Backtracking Heuristic Search

l Guess a value on a node! – Which nodes are hardest to number? ? ?

? ? ? ?

? ? Backtracking Heuristic Search

l Guess a value on a node! – Which nodes are hardest to number? ? ?

? ? ? ?

? ? Backtracking Heuristic Search

l Guess a value on a node! – Which are the least constraining values to use? ? ?

? ? ? ?

? ? Backtracking Heuristic Search

l Guess a value on a node! – Which are the least constraining values to use? ? ?

? 1 8 ?

? ? Backtracking Heuristic Search

Symmetry means we don’t need to consider: 8 1

? ?

? 1 8 ?

? ? Consistency & Propagation

l We can now eliminate many values for other nodes! – Check the current assignment Ni= vj and see the values that cannot be assigned to the future nodes. ? ?

? 1 8 ?

? ? Consistency & Propagation

{1,2,3,4,5,6,7,8} ? ?

? 1 8 ?

? ? Consistency & Propagation

{1,2,3,4,5,6,7,8} ? ?

? 1 8 ?

? ? Consistency & Propagation

{1,2,3,4,5,6,7,8} ? ?

? 1 8 ?

? ? Consistency & Propagation

{1,2,3,4,5,6,7,8} ? ?

? 1 8 ?

? ? Consistency & Propagation

{1,2,3,4,5,6,7,8} ? ?

? 1 8 ?

? ? Consistency & Propagation

{3,4,5,6} ? ?

? 1 8 ?

? ? Consistency & Propagation

By symmetry {3,4,5,6} {3,4,5,6} ? ?

? 1 8 ?

? ?

{3,4,5,6} {3,4,5,6} Consistency & Propagation

{3,4,5,6} {3,4,5,6} ? ? {3,4,5,6,7} {2,3,4,5,6} ? 1 8 ?

? ?

{3,4,5,6} {3,4,5,6} Backtracking Heuristic Search

l Guess a value on a node! {3,4,5,6} {3,4,5,6} ? ? {3,4,5,6,7} {2,3,4,5,6} ? 1 8 ?

? ?

{3,4,5,6} {3,4,5,6} Backtracking Heuristic Search

{3,4,5,6} {3,4,5,6} ? ?

7 1 8 2

? ?

{3,4,5,6} {3,4,5,6} Consistency & Propagation

{3,4,5,6} {3,4,5,6} ? ?

7 1 8 2

? ?

{3,4,5,6} {3,4,5,6} Consistency & Propagation

{3,4,5,6} {3,4,5,6} ? ?

7 1 8 2

? ?

{3,4,5,6} {3,4,5,6} Backtracking Heuristic Search

l Guess a value on a node! {3,4,5} {4,5,6} ? ?

7 1 8 2

? ?

{3,4,5} {4,5,6} Backtracking Heuristic Search

{4,5,6} 3 ?

7 1 8 2

? ?

{3,4,5} {4,5,6} Consistency & Propagation

{4,5,6} 3 ?

7 1 8 2

? ?

{3,4,5} {4,5,6} Backtracking Heuristic Search

l Guess a value on a node! {5,6} 3 ?

7 1 8 2

? ?

{4,5} {4,5,6} Backtracking Heuristic Search

{5,6} 3 ?

7 1 8 2

4 ?

{4,5,6} Consistency & Propagation

{5,6} 3 ?

7 1 8 2

4 ?

{4,5,6} Backtracking Heuristic Search

l One node has a single value left! {5,6} 3 ?

7 1 8 2

4 ?

{6} Backtracking Heuristic Search

{5,6} 3 ?

7 1 8 2

4 6 Consistency & Propagation

{5,6} 3 ?

7 1 8 2

4 6 Solution

l 8 guesses, without any backtracking!

3 5

7 1 8 2

4 6 Backtracking Search without Heuristics

l Guess a value on a node!

? ?

? ? ? ?

? ? Backtracking Search

? ?

1 ? ? ?

? ? Consistency & Propagation

{1,2,3,4,5,6,7,8} ? ?

1 ? ? ?

? ? Consistency & Propagation

3--8 2--8 ? ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Backtracking Search

2--8 3 ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Consistency & Propagation

5--8 3 ? 5--8 5--8 1 ? ? ? 2,4--8

? ?

4--8 2,4--8 Backtracking Search

5--8 3 ? 5--8 1 5 ? ? 2,4--8

? ?

4--8 2,4--8 Consistency & Propagation

7--8 3 ? 7--8 1 5 ? ? 2,4,6--8

? ?

7--8 2,7--8 Backtracking Search

7--8 3 ? 7--8 1 5 ? ? 2,4,6--8

7 ?

2,7--8 Consistency & Propagation

8 3 ? ? 1 5 ? ? 4,6

7 ?

2 Deadend

8 #backtracks =1 3 ? ? 1 5 ? ? 4,6

7 ?

2 Backtracking

7--8 3 ? 7--8 1 5 ? ? 2,4,6--8

? ?

7--8 2,7--8 Backtracking Search

7--8 3 ? 7--8 1 5 ? ? 2,4,6--8

8 ?

2,7--8 Consistency & Propagation

7 3 ? ? 1 5 ? ? 4

8 ?

2 Deadend

7 #backtracks =2 3 ? ? 1 5 ? ? 4

8 ?

2 Backtracking

5--8 3 ? 5--8 5--8 1 ? ? ? 2,4--8

? ?

4--8 2,4--8 Backtracking Search

5--8 3 ? 5--8 1 6 ? ? 2,4--8

? ?

4--8 2,4--8 Consistency & Propagation

8 3 ? ? 1 6 ? ? 5

? ?

4 2 Deadend

8 #backtracks =3 3 ? ? 1 6 ? ? 5

? ?

4 2 Backtracking

5--8 3 ? 5--8 5--8 1 ? ? ? 2,4--8

? ?

4--8 2,4--8 Backtracking Search

5--8 3 ? 5--8 1 7 ? ? 2,4--8

? ?

4--8 2,4--8 Consistency & Propagation

5 3 ? ? 1 7 ? ? 8

? ?

4 2 Deadend

5 #backtracks =4 3 ? ? 1 7 ? ? 8

? ?

4 2 Backtracking

5--8 3 ? 5--8 5--8 1 ? ? ? 2,4--8

? ?

4--8 2,4--8 Backtracking Search

5--8 3 ? 5--8 1 8 ? ? 2,4--8

? ?

4--8 2,4--8 Consistency & Propagation

5--6 3 ? 5--6 1 8 ? ? 2,4--7

? ?

4--6 2,4--6 Backtracking Search

5--6 3 ? 5--6 1 8 ? ? 2,4--7

4 ?

2,4--6 Consistency & Propagation

? 3 ? 6 1 8 ? ? ?

4 ?

2 Deadend

? #backtracks =5 3 ? 6 1 8 ? ? ?

4 ?

2 Backtracking

5--6 3 ? 5--6 1 8 ? ? 2,4--7

? ?

4--6 2,4--6 Backtracking Search

5--6 3 ? 5--6 1 8 ? ? 2,4--7

5 ?

2,4--6 Consistency & Propagation

6 3 ? ? 1 8 ? ? 4

5 ?

2 Deadend

6 #backtracks =6 3 ? ? 1 8 ? ? 4

5 ?

2 Backtracking

5--6 3 ? 5--6 1 8 ? ? 2,4--7

? ?

4--6 2,4--6 Backtracking Search

5--6 3 ? 5--6 1 8 ? ? 2,4--7

6 ?

2,4--6 Consistency & Propagation

5 3 ? ? 1 8 ? ? 2,7

6 ?

2,4 Deadend

5 #backtracks =7 3 ? ? 1 8 ? ? 2,7

6 ?

2,4 Backtracking

3--8 2--8 ? ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Backtracking Search

2--8 4 ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Consistency & Propagation

2,6--8 4 ? 6--8 2,6--8 1 ? ? ? 2,3,5--8

? ?

3,5--8 2,3,5--8 Backtracking Search

2,6--8 4 ? 2,6--8 1 6 ? ? 2,3,5--8

? ?

3,5--8 2,3,5--8 Consistency & Propagation

2,8 4 ? 2,8 1 6 ? ? 2,3,5,7,8

? ?

3,8 2,3,8 Backtracking Search

2,8 4 ? 2,8 1 6 ? ? 2,3,5,7,8

3 ?

2,3,8 Deadend

2 #backtracks =8 4 ? ? 1 6 ? ? 5

3 ?

8 Backtracking

2,8 4 ? 2,8 1 6 ? ? 2,3,5,7,8

? ?

3,8 2,3,8 Backtracking

2,8 4 ? 2,8 1 6 ? ? 2,3,5,7,8

8 ?

2,3,8 Consistency & Propagation

2 4 ? ? 1 6 ? ? 5,7

8 ?

3 Deadend

2 #backtracks =9 4 ? ? 1 6 ? ? 5,7

8 ?

3 Backtracking

2,6--8 4 ? 6--8 2,6--8 1 ? ? ? 2,3,5--8

? ?

3,5--8 2,3,5--8 Backtracking

2,6--8 4 ? 2,6--8 1 7 ? ? 2,3,5--8

? ?

3,5--8 2,3,5--8 Consistency & Propagation

2 4 ? ? 1 7 ? ? 5,6,8

? ?

3,5 3,5 Deadend

2 #backtracks =10 4 ? ? 1 7 ? ? 5,6,8

? ?

3,5 3,5 Backtracking

2,6--8 4 ? 6--8 2,6--8 1 ? ? ? 2,3,5--8

? ?

3,5--8 2,3,5--8 Backtracking Search

2,6--8 4 ? 2,6--8 1 8 ? ? 2,3,5--8

? ?

3,5--8 2,3,5--8 Consistency & Propagation

2,6 4 ? 2,6 1 8 ? ? 2,3,5--7

? ?

3,5,6 2,3,5,6 Backtracking Search

2,6 4 ? 2,6 1 8 ? ? 2,3,5--7

3 ?

2,3,5,6 Consistency & Propagation

2 4 ? 6 1 8 ? ? ?

3 ?

? Deadend

2 #backtracks =11 4 ? 6 1 8 ? ? ?

3 ?

? Backtracking

2,6 4 ? 2,6 1 8 ? ? 2,3,5--7

? ?

3,5,6 2,3,5,6 Backtracking Search

2,6 4 ? 2,6 1 8 ? ? 2,3,5--7

5 ?

2,3,5,6 Consistency & Propagation

6 4 ? 2 1 8 ? ? ?

5 ?

? Deadend

6 #backtracks =12 4 ? 2 1 8 ? ? ?

5 ?

? Backtracking

2,6 4 ? 2,6 1 8 ? ? 2,3,5--7

? ?

3,5,6 2,3,5,6 Backtracking Search

2,6 4 ? 2,6 1 8 ? ? 2,3,5--7

6 ?

2,3,5,6 Consistency & Propagation

? 4 ? 2 1 8 ? ? 5,7

6 ?

? Deadend

? #backtracks =13 4 ? 2 1 8 ? ? 5,7

6 ?

? Backtracking

3--8 2--8 ? ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Backtracking Search

2--8 5 ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Consistency & Propagation

2,3,7,8 5 ? 3,7,8 2,3,7,8 1 ? ? ? 2--4,6--8

? ?

3,4,6--8 2--4,6--8 Backtracking Search

2,3,7,8 5 ? 2,3,7,8 1 3 ? ? 2--4,6--8

? ?

3,4,6--8 2--4,6--8 Consistency & Propagation

7,8 5 ? 7,8 1 3 ? ? 2,4,6--8

? ?

6--8 6--8 Backtracking Search

7,8 5 ? 7,8 1 3 ? ? 2,4,6--8

6 ?

6--8 Consistency & Propagation

? 5 ? 8 1 3 ? ? 2,4

6 ?

? Deadend

? #backtracks =14 5 ? 8 1 3 ? ? 2,4

6 ?

? Backtracking

7,8 5 ? 7,8 1 3 ? ? 2,4,6--8

? ?

6--8 6--8 Backtracking Search

7,8 5 ? 7,8 1 3 ? ? 2,4,6--8

7 ?

6--8 Consistency & Propagation

8 5 ? ? 1 3 ? ? 2,4,6

7 ?

? Deadend

8 #backtracks =15 5 ? ? 1 3 ? ? 2,4,6

7 ?

? Backtracking

7,8 5 ? 7,8 1 3 ? ? 2,4,6--8

? ?

6--8 6--8 Backtracking Search

7,8 5 ? 7,8 1 3 ? ? 2,4,6--8

8 ?

6--8 Consistency & Propagation

7 5 ? ? 1 3 ? ? 2,4

8 ?

6 Deadend

7 #backtracks =16 5 ? ? 1 3 ? ? 2,4

8 ?

6 Backtracking

2,3,7,8 5 ? 3,7,8 2,3,7,8 1 ? ? ? 2--4,6--8

? ?

3,4,6--8 2--4,6--8 Backtracking Search

2,3,7,8 5 ? 2,3,7,8 1 7 ? ? 2--4,6--8

? ?

3,4,6--8 2--4,6--8 Consistency & Propagation

2,3 5 ? 2,3 1 7 ? ? 2--4,6,8

? ?

3,4 2--4 Backtracking Search

2,3 5 ? 2,3 1 7 ? ? 2--4,6,8

3 ?

2--4 Consistency & Propagation

2 5 ? ? 1 7 ? ? 4,6,8

3 ?

? Deadend

2 #backtracks =17 5 ? ? 1 7 ? ? 4,6,8

3 ?

? Backtracking

2,3 5 ? 2,3 1 7 ? ? 2--4,6,8

? ?

3,4 2--4 Backtracking Search

2,3 5 ? 2,3 1 7 ? ? 2--4,6,8

4 ?

2--4 Consistency & Propagation

3 5 ? ? 1 7 ? ? 6,8

4 ?

2 Deadend

3 #backtracks =18 5 ? ? 1 7 ? ? 6,8

4 ?

2 Backtracking

2,3,7,8 5 ? 3,7,8 2,3,7,8 1 ? ? ? 2--4,6--8

? ?

3,4,6--8 2--4,6--8 Backtracking

2,3,7,8 5 ? 2,3,7,8 1 8 ? ? 2--4,6--8

? ?

3,4,6--8 2--4,6--8 Consistency & Propagation

2,3 5 ? 2,3 1 8 ? ? 2--4,6,7

? ?

3,4,6 2--4,6 Backtracking Search

2,3 5 ? 2,3 1 8 ? ? 2--4,6,7

3 ?

2--4,6 Consistency & Propagation

2 5 ? ? 1 8 ? ? 4

3 ?

6 Deadend

2 #backtracks =19 5 ? ? 1 8 ? ? 4

3 ?

6 Backtracking

2,3 5 ? 2,3 1 8 ? ? 2--4,6,7

? ?

3,4,6 2--4,6 Backtracking

2,3 5 ? 2,3 1 8 ? ? 2--4,6,7

4 ?

2--4,6 Consistency & Propagation

? 5 ?

2 1 8 ? ? ?

4 ?

6 Deadend

? #backtracks =20 5 ?

2 1 8 ? ? ?

4 ?

6 Backtracking

2,3 5 ? 2,3 1 8 ? ? 2--4,6,7

? ?

3,4,6 2--4,6 Backtracking Search

2,3 5 ? 2,3 1 8 ? ? 2--4,6,7

6 ?

2--4,6 Consistency & Propagation

2,3 5 ? 2,3 1 8 ? ? 2--4,7

6 ?

2--4 Backtracking Search

5 2 2,3 1 8 ? ? 2--4,7

6 ?

2--4 Consistency & Propagation

5 2

? 1 8 ? ? 4,7

6 ?

3,4 Deadend

#backtracks =21

5 2 ? 1 8 ? ? 4,7

6 ?

3,4 Backtracking

2,3 5 ? 2,3 1 8 ? ? 2--4,7

6 ?

2--4 Backtracking Search

5 3 2,3 1 8 ? ? 2--4,7

6 ?

2--4 Consistency & Propagation

5 3 ? 1 8 ? ? 7

6 ?

2,4 Deadend

#backtracks =22

5 3 ? 1 8 ? ? 7

6 ?

2,4 Backtracking

3--8 2--8 ? ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Backtracking Search

2--8 6 ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Consistency & Propagation

2--4,8 6 ? 3,4,8 2--4,8 1 ? ? ? 2--5,7,8

? ?

3--5,7,8 2--5,7,8 Backtracking Search

2--4,8 6 ? 2--4,8 1 3 ? ? 2--5,7,8

? ?

3--5,7,8 2--5,7,8 Consistency & Propagation

? 6 ?

8 1 3 ? ? 2,4,5

? ?

5,7 5,7 Deadend

? #backtracks =23 6 ?

8 1 3 ? ? 2,4,5

? ?

5,7 5,7 Backtracking

2--4,8 6 ? 3,4,8 2--4,8 1 ? ? ? 2--5,7,8

? ?

3--5,7,8 2--5,7,8 Backtracking Search

2--4,8 6 ? 2--4,8 1 4 ? ? 2--5,7,8

? ?

3--5,7,8 2--5,7,8 Consistency & Propagation

2,8 6 ? 2,8 1 4 ? ? 2,3,5,7,8

? ?

7,8 2,7,8 Backtracking Search

2,8 6 ? 2,8 1 4 ? ? 2,3,5,7,8

7 ?

2,7,8 Consistency & Propagation

8 6 ? 2 1 4 ? ? 5

7 ?

? Deadend

8 #backtracks =24 6 ? 2 1 4 ? ? 5

7 ?

? Backtracking

2,8 6 ? 2,8 1 4 ? ? 2,3,5,7,8

? ?

7,8 2,7,8 Backtracking Search

2,8 6 ? 2,8 1 4 ? ? 2,3,5,7,8

8 ?

2,7,8 Consistency & Propagation

? 6 ? 2 1 4 ? ? 5,7

8 ?

? Deadend

? #backtracks =25 6 ? 2 1 4 ? ? 5,7

8 ?

? Backtracking

2--4,8 6 ? 3,4,8 2--4,8 1 ? ? ? 2--5,7,8

? ?

3--5,7,8 2--5,7,8 Backtracking Search

2--4,8 6 ? 2--4,8 1 8 ? ? 2--5,7,8

? ?

3--5,7,8 2--5,7,8 Consistency & Propagation

2--4 6 ? 2--4 1 8 ? ? 2--5,7

? ?

3--5 2--5 Backtracking Search

2--4 6 ? 2--4 1 8 ? ? 2--5,7

3 ?

2--5 Consistency & Propagation

2,4 6 ? ? 1 8 ? ? 2,7

3 ?

5 Deadend

2,4 #backtracks =26 6 ? ? 1 8 ? ? 2,7

3 ?

5 Backtracking

2--4 6 ? 2--4 1 8 ? ? 2--5,7

? ?

3--5 2--5 Backtracking Search

2--4 6 ? 2--4 1 8 ? ? 2--5,7

4 ?

2--5 Consistency & Propagation

? 6 ? 2 1 8 ? ? 5,7

4 ?

? Deadend

? #backtracks =27 6 ? 2 1 8 ? ? 5,7

4 ?

? Backtracking

2--4 6 ? 2--4 1 8 ? ? 2--5,7

? ?

3--5 2--5 Backtracking Search

2--4 6 ? 2--4 1 8 ? ? 2--5,7

5 ?

2--5 Consistency & Propagation

2--4 6 ? 2,3 1 8 ? ? 2--4,7

5 ?

2,3 Backtracking Search

6 2 2,3 1 8 ? ? 2--4,7

5 ?

2,3 Consistency & Propagation

6 2 ? 1 8 ? ? 7

5 ?

3 Deadend

#backtracks =28

6 2 ? 1 8 ? ? 7

5 ?

3 Backtracking

2--4 6 ? 2,3 1 8 ? ? 2--4,7

5 ?

2,3 Consistency & Propagation

6 3 ? 1 8 ? ? 7

5 ?

2 Deadend

#backtracks =29

6 3 ? 1 8 ? ? 7

5 ?

2 Backtracking

2--4 6 ? 2,3 1 8 ? ? 2--4,7

5 ?

2,3 Backtracking Search

6 4

2 1 8 ? ? 7

5 ?

? Deadend

#backtracks =30

6 4

2 1 8 ? ? 7

5 ?

? Backtracking

3--8 2--8 ? ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Backtracking Search

2--8 7 ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Consistency & Propagation

2--5 7 ? 3--5 2--5 1 ? ? ? 2--6,8

? ?

3--6,8 2--6,8 Backtracking Search

2--5 7 ? 2--5 1 3 ? ? 2--6,8

? ?

3--6,8 2--6,8 Consistency & Propagatipon

5 7 ? ? 1 3 ? ? 2,8

? ?

6,8 6,8 Deadend

5 #backtracks =31 7 ? ? 1 3 ? ? 2,8

? ?

6,8 6,8 Backtracking

2--5 7 ? 3--5 2--5 1 ? ? ? 2--6,8

? ?

3--6,8 2--6,8 Backtracking Search

2--5 7 ? 2--5 1 4 ? ? 2--6,8

? ?

3--6,8 2--6,8 Consistency & Propagation

2 7 ? ? 1 4 ? ? 5,6,8

? ?

6,8 6,8 Deadend

2 #backtracks =32 7 ? ? 1 4 ? ? 5,6,8

? ?

6,8 6,8 Backtracking

2--5 7 ? 3--5 2--5 1 ? ? ? 2--6,8

? ?

3--6,8 2--6,8 Backtracking Search

2--5 7 ? 2--5 1 5 ? ? 2--6,8

? ?

3--6,8 2--6,8 Consistency & Propagation

2,3 7 ? 2,3 1 5 ? ? 2--4,6,8

? ?

3,8 2,3,8 Backtracking Search

2,3 7 ? 2,3 1 5 ? ? 2--4,6,8

3 ?

2,3,8 Consistency & Propagation

2 7 ? ? 1 5 ? ? 4,6

3 ?

8 Deadend

2 #backtracks =33 7 ? ? 1 5 ? ? 4,6

3 ?

8 Backtracking

2,3 7 ? 2,3 1 5 ? ? 2--4,6,8

? ?

3,8 2,3,8 Consistency & Propagation

2,3 7 ? 2,3 1 5 ? ? 2--4,6

8 ?

2,3 Backtracking Search

7 2 2,3 1 5 ? ? 2--4,6

8 ?

2,3 Consistency & Propagation

7 2 ? 1 5 ? ? 6

8 ?

3 Deadend

#backtracks =34

7 2

? 1 5 ? ? 6

8 ?

3 Backtracking

2,3 7 ? 2,3 1 5 ? ? 2--4,6

8 ?

2,3 Backtracking Search

7 3 ? 1 5 ? ? 6

8 ?

2 Deadend

#backtracks =35

7 3 ? 1 5 ? ? 6

8 ?

2 Backtracking

3--8 2--8 ? ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Backtracking Search

2--8 8 ? 3--8 2--8 1 ? ? ? 2--8

? ?

3--8 2--8 Consistency & Propagation

2--6 8 ? 3--6 2--6 1 ? ? ? 2--7

? ?

3--7 2--7 Backtracking Search

2--6 8 ? 2--6 1 3 ? ? 2--7

? ?

3--7 2--7 Consistency & Propagation

5,6 8 ? 5,6 1 3 ? ? 2,4--7

? ?

5--7 5--7 Backtracking Search

5,6 8 ? 5,6 1 3 ? ? 2,4--7

5 ?

5--7 Consistency & Propagation

6 8 ? ? 1 3 ? ? 2,4

5 ?

7 Deadend

6 #backtracks =36 8 ? ? 1 3 ? ? 2,4

5 ?

7 Backtracking

5,6 8 ? 5,6 1 3 ? ? 2,4--7

? ?

5--7 5--7 Backtracking Search

5,6 8 ? 5,6 1 3 ? ? 2,4--7

6 ?

5--7 Consistency & Propagation

5 8 ? ? 1 3 ? ? 2,7

6 ?

? Deadend

5 #backtracks =37 8 ? ? 1 3 ? ? 2,7

6 ?

? Backtracking

5,6 8 ? 5,6 1 3 ? ? 2,4--7

? ?

5--7 5--7 Backtracking Search

5,6 8 ? 5,6 1 3 ? ? 2,4--7

7 ?

5--7 Consistency & Propagation

6 8 ? ? 2 1 3 ? ?

7 ?

5 Deadend

6 #backtracks =38 8 ? ? 2 1 3 ? ?

7 ?

5 Backtracking

2--6 8 ? 3--6 2--6 1 ? ? ? 2--7

? ?

3--7 2--7 Backtracking Search

2--6 8 ? 2--6 1 4 ? ? 2--7

? ?

3--7 2--7 Consistency & Propagation

2,6 8 ? 2,6 1 4 ? ? 2,3,5--7

? ?

6,7 2,6,7 Backtracking Search

2,6 8 ? 2,6 1 4 ? ? 2,3,5--7

6 ?

2,6,7 Consistency & Propagation

? 8 ? ? 1 4 ? ? 5,7

6 ?

2 Deadend

? #backtracks =39 8 ? ? 1 4 ? ? 5,7

6 ?

2 Backtracking

2,6 8 ? 2,6 1 4 ? ? 2,3,5--7

? ?

6,7 2,6,7 Backtracking Search

2,6 8 ? 2,6 1 4 ? ? 2,3,5--7

7 ?

2,6,7 Consistency & Propagation

6 8 ? ? 1 4 ? ? ?

7 ?

2 Deadend

6 #backtracks =40 8 ? ? 1 4 ? ? ?

7 ?

2 Backtracking

2--6 8 ? 3--6 2--6 1 ? ? ? 2--7

? ?

3--7 2--7 Backtracking Search

2--6 8 ? 2--6 1 5 ? ? 2--7

? ?

3--7 2--7 Consistency & Propagation

2,3 8 ? 2,3 1 5 ? ? 2--4,6,7

? ?

3,7 2,3,7 Backtracking Search

2,3 8 ? 2,3 1 5 ? ? 2--4,6,7

3 ?

2,3,7 Consistency & Propagation

2 8 ? ? 1 5 ? ? 4

3 ?

7 Deadend

2 #backtracks =41 8 ? ? 1 5 ? ? 4

3 ?

7 Backtracking

2,3 8 ? 2,3 1 5 ? ? 2--4,6,7

? ?

3,7 2,3,7 Backtracking Search

2,3 8 ? 2,3 1 5 ? ? 2--4,6,7

7 ?

2,3,7 Consistency & Propagation

2,3 8 ? 2,3 1 5 ? ? 2--4,6

7 ?

2,3 Backtracking Search

8 2 2,3 1 5 ? ? 2--4,6

7 ?

2,3 Consistency & Propagation

8 2 ? 1 5 ? ? 6

7 ?

3 Deadend

#backtracks =42

8 2 ? 1 5 ? ? 6

7 ?

3 Backtracking

2,3 8 ? 2,3 1 5 ? ? 2--4,6

7 ?

2,3 Backtracking Search

8 3 2,3 1 5 ? ? 2--4,6

7 ?

2,3 Consistency & Propagation

8 3 ? 1 5 ? ? 6

7 ?

2 Deadend

#backtracks =43

8 3 ? 1 5 ? ? 6

7 ?

2 Backtracking

2--6 8 ? 3--6 2--6 1 ? ? ? 2--7

? ?

3--7 2--7 Backtracking Search

2--6 8 ? 2--6 1 6 ? ? 2--7

? ?

3--7 2--7 Consistency & Propagation

2--4 8 ? 2--4 1 6 ? ? 2--5,7

? ?

3,4 2--4 Backtracking Search

2--4 8 ? 2--4 1 6 ? ? 2--5,7

3 ?

2--4 Consistency & Propagation

2,4 8 ? ? 1 6 ? ? 2,4,5,7

3 ?

? Deadend

2,4 #backtracks =44 8 ? ? 1 6 ? ? 2,4,5,7

3 ?

? Backtracking

2--4 8 ? 2--4 1 6 ? ? 2--5,7

? ?

3,4 2--4 Backtracking Search

2--4 8 ? 2--4 1 6 ? ? 2--5,7

4 ?

2--4 Consistency & Propagation

3 8 ? ? 1 6 ? ? 5,7

4 ?

2 Deadend

3 #backtracks =45 8 ? ? 1 6 ? ? 5,7

4 ?

2 Backtracking

l Back to the beginning after 45 backtracks without any solution at hand L 1--8 1--8

? ? 1--8 1--8 1--8 ? ? ? ? 1--8

? ? 1--8 1--8 What’s going on?

l Bad choice of nodes, bad assignment of values => Good heuristic choice is very important! l Good heuristics are always possible? – No! l What can we do then? – Good consistency and propagation! l Didn’t we do that already? – Not as strong as we could!

A State During Search

l 2 deadends after this state. l Can be detected immediately! 7--8

3 ? 7--8 1 5 ? ? 2,4,6--8

? ?

7--8 2,7--8 A State During Search

l Check the relations between the future nodes. 7--8 3 ? 7--8 1 5 ? ? 2,4,6--8

? ?

7--8 2,7--8 What’s going on?

l Bad choice of nodes, bad assignment of values => Good heuristic choice is very important! l Good heuristics are always possible? – No! l What can we do then? – Good consistency and propagation! l Didn’t we do that already? – Not as strong as we could! l Is that all? – Nope! – Good modelling can result in even stronger consistency & propagation

Modelling

l Unknowns: N1..N8 represent the nodes l Values that N1..N8 can take: {1,2,3,4,5,6,7,8} l Relations: for all i < j s.t. Ni and Nj are adjacent. |Ni - Nj | > 1

for all i < j Ni ≠ Nj

N2 N5

N1 N3 N6 N8

N4 N7 Another State

l Cannot detect the inconsistency of N3= 6. – Future nodes are fine wrt the relations. 2,8 4 ?

2,8 1 6 ? ? 2,3,5,7,8

? ?

3,8 2,3,8 Stronger Model

l Relations:

- for all i < j s.t. Ni and Nj are adjacent |Ni - Nj| > 1

- for all i < j Ni ≠ Nj alldifferent([N1, N2, N3, N4, N5, N6, N7,])

N2 N5

N1 N3 N6 N8

N4 N7 Another State

l Check the difference relations between the future nodes. 2,8

4 ? 2,8 1 6 ? ? 2,3,5,7,8

? ?

3,8 2,3,8 Another State

l Consistency & propagation 2,8

4 ? 2,8 1 6 ? ? 2,3,5,7,8

? ?

3,8 2,3,8 Homework

l How many backtracks when we explore the nodes and their values in order using all these stronger reasoning without any heuristics? - Alldifferent relation - Consistency between the future nodes

N2 N5

N1 N3 N6 N8

N4 N7 Constraint Programming

l The efficiency of a CP program highly depends on: – strong modelling; – strong consistency & propagation; – intelligent heuristics to guide the search. l Attention! – Stronger/intelligent reasoning comes with a cost. – Need a good balance between the cost and the strength.

What is it about? l Theory – The core of solving constraint problems using Constraint Programming (CP), with emphasis on: l Modeling l Solving: – Local consistency and propagation; – Backtracking search + heuristics. – Brief overview of advanced topics. l Practice – Programming examples. – Project work. Constraint Programming

l Programming, in the sense of mathematical programming: – the user models the problem; – an underlying solver returns a solution. l Programming, in the sense of computer programming: – the user needs to program a strategy to search for a solution l search algorithm, heuristics, … – otherwise, solving process can be inefficient. Constraint Programming

Artificial Discrete Computer Operations Algorithms Intelligence Mathematics Science Research … Complexity Theory CP

Planning Networks Vehicle Configuration Bio- Routing informatics … & Scheduling CP Tools l Solvers – Dedicated – Embedded in an existing programming language via a library: l logic programming (Prolog-based) l functional programming (Objective Caml) l imperative programming (C, C++, Java, Python) l Modelling (&search) languages with backend to (CP) solvers

Sport Scheduling

Week1 Week 2 Week 3 Week4 Week 5 Week6 Week 7

Period 1 0 vs 1 0 vs 2 4 vs 7 3 vs 6 3 vs 7 1 vs 5 2 vs 4

Period 2 2 vs 3 1 vs 7 0 vs 3 5 vs 7 1 vs 4 0 vs 6 5 vs 6

Period 3 4 vs 5 3 vs 5 1 vs 6 0 vs 4 2 vs 6 2 vs 7 0 vs 7

Period 4 6 vs 7 4 vs 6 2 vs 5 1 vs 2 0 vs 5 3 vs 4 1 vs 3 Schedule of 1997/1998 ACC basketball league (9 teams). • Various complicated side constraints. • All 179 soluons were found in 24h using enumeraon and integer [Nemhauser & Trick, 1998].

• All 179 soluons were found in less than a minute CP [Henz, 1999, 2001]. Hong Kong Airport

l Gate allocation at the Hong Kong International Airport. l System was implemented in only four months, and includes CP technology (ILOG). l Schedules ~900 flights a day. Port of Singapore

l One of the world’s largest container transshipment hubs. l Links shippers to a network of 200 shipping lines with connections to 600 ports in 123 countries. l Problem: Assign yard locations and loading plans under various operational and safety requirements. l Solution: Yard planning system, based on CP (ILOG).

283 Railroad Optimization

l Netherlands Railways has among the densest rail networks in the world, with 5,500 trains per day. l CP is one of the components in their railway planning software, which was used to design a new timetable from scratch (2009). l Much more robust and effective schedule, and $75M additional annual profit. Resources l Books – Handbook of Constraint Programming F. Rossi, P. van Beek, T. Walsh (eds), Elsevier Science, 2006.

Some chapters are available in pdf: Chapter 1 - Introduction Chapter 3 - Constraint Propagation Chapter 4 - Backtracking Search Algorithms Chapter 7 - Global Constraints Chapter 10 - Symmetry in CP Chapter 11 - Modelling Resources l Books – Integrated Methods for Optimization J. N. Hooker, Springer, 2nd edition, 2012. – Hybrid Optimization M. Milano and P. van Hentenryck (eds), Springer, 2011. – Constraint Logic Programming Using Eclipse K. Apt and M. Wallace, Cambridge University Press, 2006. – Constraint-based Local Search P. van Hentenryck and L. Michel, MIT Press, 2005. – Principles of Constraint Programming K. Apt, Cambridge University Press, 2003. – Constraint Processing R. Dechter, Morgan Kaufmann, 2003. – Constraint-based Scheduling P. Baptiste, C. Le pape and W. Nuijten, Springer-Verlag, 2001. – The OPL Optimization Programming Languages Pascal Van Hentenryck, MIT Press, 1999.

Resources

l People – Barbara Smith l Modelling, symmetry breaking, search heuristics l Tutorials and book chapter – Christian Bessiere l Constraint propagation l Global constraints l Book chapter – Jean-Charles Regin l Global constraints – alldifferent, global cardinality, cardinality matrix l Book chapter – Toby Walsh l Modelling, symmetry breaking, global constraints l Various tutorials – John Hooker l Hybrid optimization l Tutorials and book chapter Resources

l Journals – Constraints – Artificial Intelligence – Journal of Artificial Intelligence Research – Journal of Heuristics – Intelligenza Artificiale (AI*IA) – Informs Journal on Computing – Annals of Mathematics and Artificial Intelligence Resources l Conferences – Principles and Practice of Constraint Programming (CP) http://4c110.ucc.ie/a4cp/events/cp-conference-series – Integration of AI and OR Techniques in CP (CP-AI-OR) http://www.andrew.cmu.edu/user/vanhoeve/cpaior/ – Conference of the Association for the Advancement of AI (AAAI) http://www.aaai.org – International Joint Conference on Artificial Intelligence (IJCAI) http://www.ijcai.org – European Conference on Artificial Intelligence (ECAI) http://www.eccai.org – International Symposium on Practical Aspects of Declarative Languages (PADL) http://www.informatik.uni-trier.de/~ley/db/conf/padl/index.html Resources

l Courses and Schools – Discrete Optimization at Coursera (https://www.coursera.org/course/optimization) – ACP summer schools: (http://4c110.ucc.ie/a4cp/events/summer-schools) 2005: http://www.math.unipd.it/~frossi/cp-school/ 2006: http://www.cse.unsw.edu.au/~tw/school.html 2007: http://www.iiia.csic.es/summerschools/sscp2007/ 2008: http://www-circa.mcs.st-and.ac.uk/cpss2008/ 2009: http://www.cs.ucc.ie/~osullb/ACPSS2009/Welcome.html 2010: http://becool.info.ucl.ac.be/summerschool2010/ 2011: http://www.gecode.org/events/acp-summerschool-2011/ 2012: http://acpss2012.ii.uni.wroc.pl/ 2013: http://www.cse.unsw.edu.au/~tw/school2013/ Resources

l Tutorials – AI conference tutorials (IJCAI, AAAI, ECAI). – CP conference tutorials. – CP-AI-OR master classes. Resources l Solvers, Languages, Libraries – Live catalogue of CP Solvers (http://openjvm.jvmhost.net/CPSolvers/) – AIMMS l Modelling language with interfaces to CP and MIP solvers (http://www.aimms.com/cp) – Choco l Java library (http://www.emn.fr/z-info/choco-solver/) – CPX Discrete Optimizer l Solver (http://opturion.com/cpx.html) – Eclipse l Prolog-based library (http://eclipseclp.org/) – FaCiLe l Functional programming library (http://www.recherche.enac.fr/opti/facile/) Resources

– Gecode l C++ library (http://www.gecode.org/) – Google CP Solver (OR-tools) l C++ library (http://code.google.com/p/or-tools/) – IBM ILOG CP Optimizer l C++ library ( http://www-01.ibm.com/software/commerce/optimization/cplex- cp-optimizer/index.html) – IBM ILOG OPL l Modelling language with interfaces to CP and MIP solvers ( http://www-03.ibm.com/software/products/us/en/ ibmilogcpleoptistud/) – JaCoP l Java library (http://www.jacop.eu/) Resources

– Minizinc l Modelling language with interfaces to CP, MIP, SAT solvers (http://www.minizinc.org/) – Numberjack l Phyton library l Modelling package with interfaces to CP, MIP, SAT solvers (http://4c110.ucc.ie/numberjack/) – SCIP l C library (http://scip.zib.de/) – Sicstus Prolog l Prolog-based library (http://sicstus.sics.se/) Resources l Blogs – CP Applications by Helmut Simonis (http://hsimonis.wordpress.com/) – My CP Blog by Håkan Kjellerstrand (http://www.hakank.org/constraint_programming_blog/) – CP-is-Fun by Jean-Charles Régin (http://cp-is-fun.blogspot.it/) – Michael Trick’s OR blog (http://mat.gsia.cmu.edu/blog/)

Practice l For programming examples and project work, we will use AIMMS: – modeling language with an interface to CP and MIP solvers (http://www.aimms.com/cp) l Free academic license available Why AIMMS? l Based on algebraic notation to naturally capture the frequently used CP structures. l Supports scheduling constructs to allow schedulers to formulate their problem in a natural way. l Provides GUI support to visualize and interact with resulting schedules and solutions.

AIMMS l Install and start playing with it! l Student license l GUI support available only in the Windows version – Create a virtual machine with Windows OS in your computer – Virtual Box (https://www.virtualbox.org/)

AIMMS l Extensive documentation – One-hour tutorial (general introduction) – Chapter 21 – “Constraint Programming” of Language Reference l Example CP models – Follow the course web site