110
THE CONSTRAINT PROGRAMMER’S TOOLBOX Christian Schulte, SCALE, KTH & SICS

THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

THE CONSTRAINT

PROGRAMMER’S

TOOLBOX

Christian Schulte, SCALE, KTH & SICS

Page 2: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Programming

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

2

What is constraint programming?

Sudoku is constraint programming

2/3/2014

Page 3: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

...is constraint programming!

Sudoku 3

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 4: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Sudoku

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

4

Assign blank fields digits such that:

digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

2/3/2014

Page 5: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Sudoku

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

5

Assign blank fields digits such that:

digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

2/3/2014

Page 6: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Sudoku

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

6

Assign blank fields digits such that:

digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

2/3/2014

Page 7: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Block Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

7

No field in block can take digits 3,6,8

8

6 3

2/3/2014

Page 8: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Block Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

8

No field in block can take digits 3,6,8 propagate to other fields in block

Rows and columns: likewise

1,2,4,5,7,9 8 1,2,4,5,7,9

1,2,4,5,7,9 6 3

1,2,4,5,7,9 1,2,4,5,7,9 1,2,4,5,7,9

2/3/2014

Page 9: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

9

Prune digits from fields such that:

digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

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

2/3/2014

Page 10: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

10

Prune digits from fields such that:

digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

9 6

2

7

1

9 4

8

6 3

4

6 8

1

8

1,3,5,6,7,8

2/3/2014

Page 11: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

11

Prune digits from fields such that:

digits distinct per rows, columns, blocks

9

2 9

5 2

7 3

6

2

6 9

7

4 9

1

8

6 3

8 6

4 1

8

1,3,6,7

2/3/2014

Page 12: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

12

Prune digits from fields such that:

digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

1,3,6

2/3/2014

Page 13: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Iterated Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

13

Iterate propagation for rows, columns, blocks

What if no assignment: search... later

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

2/3/2014

Page 14: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Sudoku is Constraint Programming

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

14

Modeling: variables, values, constraints

Solving: propagation, search

9

2

2 5

9

7 3

6

2

6 9

7

4 9

1

8

6 3

4

6 8

1

8

Variables: fields take values: digits

maintain set of possible values

Constraints: distinct relation among

values for variables

2/3/2014

Page 15: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Programming

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

15

Variable domains finite domain integer, finite sets, multisets, intervals, ...

Constraints distinct, arithmetic, scheduling, graphs, ...

Solving propagation, search, ...

Modeling variables, values, constraints, heuristics, symmetries, ...

2/3/2014

Page 16: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

This Talk...

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

16

Key concepts constraint propagation

search

The Constraint Programmer’s Toolbox..

Some few tools global constraints: distinct reconsidered

branching heuristics: bin packing user-defined constraints: personnel rostering

Summary essence of constraint programming and (very few)

resources

2/3/2014

Page 17: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Key Concepts 17

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 18: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Running Example: SMM

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

18

Find distinct digits for letters such that

SEND

+ MORE

= MONEY

2/3/2014

Page 19: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Model for SMM

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

19

Variables:

S,E,N,D,M,O,R,Y {0,…,9}

Constraints:

distinct(S,E,N,D,M,O,R,Y)

1000×S+100×E+10×N+D

+ 1000×M+100×O+10×R+E

= 10000×M+1000×O+100×N+10×E+Y

S0 M0

2/3/2014

Page 20: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Solving SMM

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

20

Find values for variables

such that

all constraints satisfied

2/3/2014

Page 21: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Finding a Solution

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

21

Compute with possible values rather than enumerating assignments

Prune inconsistent values constraint propagation

Search branch: define shape of search tree

explore: explore search tree for solution

2/3/2014

Page 22: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

constraint store

propagators

constraint propagation

Constraint Propagation 22

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 23: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Store

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

23

Maps variables to possible values

x{1,2,3,4} y{1,2,3,4} z{1,2,3,4}

2/3/2014

Page 24: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Store

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

24

Maps variables to possible values other domains: finite sets, float intervals, graphs, ...

x{1,2,3,4} y{1,2,3,4} z{1,2,3,4}

finite domain constraints

2/3/2014

Page 25: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagators

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

25

Implement constraints

distinct(x1, …, xn)

x + 2×y = z

schedule(t1, …, tn)

2/3/2014

Page 26: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagators

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

26

Strengthen store by constraint propagation prune values in conflict with implemented constraint

x{1,2,3,4} y{1,2,3,4} z{1,2,3,4}

distinct(x, y, z) x + y = 3

2/3/2014

Page 27: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagators

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

27

Strengthen store by constraint propagation prune values in conflict with implemented constraint

x{1,2} y{1,2} z{1,2,3,4}

distinct(x, y, z) x + y = 3

2/3/2014

Page 28: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagators

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

28

Iterate propagator execution until fixpoint no more pruning possible

x{1,2} y{1,2} z{3,4}

distinct(x, y, z) x + y = 3

2/3/2014

Page 29: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagation for SMM

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

29

Results in store S{9} E{4,…,7} N{5,…,8} D{2,…,8}

M{1} O{0} R{2,…,8} Y{2,…,8}

Propagation alone not sufficient! decompose into simpler sub-problems

branching and exploration for search

2/3/2014

Page 30: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

branching

exploration

Search 30

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 31: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Branching

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

31

Create subproblems with additional constraints enables further propagation

defines search tree

x{1,2} y{1,2} z{3,4}

distinct(x, y, z) x + y = 3

x{1} y{2} z{3,4}

distinct(x, y, z) x + y = 3

x{2} y{1} z{3,4}

distinct(x, y, z) x + y = 3

x=1 x≠1

2/3/2014

Page 32: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Heuristic Branching

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

32

Example branching pick variable x (at least two values)

pick value n (from domain of x)

branch with x = n and x ≠ n

Heuristic needed which variable to select?

which value to select?

2/3/2014

Page 33: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

33

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 34: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

34

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 35: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

35

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 36: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

36

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 37: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

37

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 38: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

38

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 39: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

39

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 40: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

40

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 41: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

41

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 42: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

42

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS,

2/3/2014

Page 43: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Search: Heuristic Exploration

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

43

Heuristic branching defines tree shape

Exploration of search tree orthogonal aspect: DFS, BFS, IDFS, LDS, parallel, ...

2/3/2014

Page 44: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Summary

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

44

Modeling variables with domain

constraints to state relations

branching strategy

in real: an array of modeling techniques…

Solving constraint propagation

branching

search tree exploration

in real: an array of solving techniques…

2/3/2014

Page 45: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

The Constraint Programmer’s

Toolbox 45

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 46: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Widely Applicable

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

46

Timetabling

Scheduling

Personnel and crew rostering

Resource allocation

Workflow planning and optimization

Gate allocation at airports

Sports-event scheduling

Railroad: track allocation, train allocation, schedules

Automatic composition of music

Genome sequencing

Frequency allocation

2/3/2014

Page 47: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Current Interest:

Constraint-based Code Generation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

47

From input program (in intermediate representation)

hardware architecture description

generate constraint problem

Solution = executable program code simplicity: avoid bugs, …

flexibility: architecture change, …

quality: possibly optimal, …

Ongoing project 2010 – 2015 funded by Ericsson and Vetenskapsrådet

2/3/2014

Page 48: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Problems Are Hard

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

48

The problems are NP hard no efficient algorithm is likely to exist

Tremendously difficult to always solve any problem instance

scale to large instances

have single silver bullet method

Property of problems…

…not of method

…hence no silver bullet

2/3/2014

Page 49: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Why Is a Toolbox Needed?

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

49

Initial model: model to capture problem correctness

Improved model: model to solve problem robustness and scalability

often difficult

Tools in the toolbox are needed for…

…modeling to solve problems

2/3/2014

Page 50: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Parts of the Toolbox

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

50

“Global” constraints capture structure during modeling

provide strong constraint propagation

Search heuristics application specific

Symmetries and dominance relations reduce size of search space

Propagation-boosting constraints

Randomized restarts during search including no-goods from restarts

2/3/2014

Page 51: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

The Best We Can Hope for…

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

51

problem size

runtim

e

Exponential growth in runtime

Without using tools

feasible

2/3/2014

Page 52: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

The Best We Can Hope for…

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

52

problem size

runtim

e

Exponential growth in runtime

With propagation and heuristic search

feasible

2/3/2014

Page 53: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

The Best We Can Hope for…

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

53

problem size

runtim

e

Exponential growth in runtime

With propagation, heuristic search, symmetry breaking, restarts, …

feasible

2/3/2014

Page 54: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

distinct reconsidered

Capturing Structure 54

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 55: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Naïve Is Not Good Enough

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

55

distinct(x, y, z)

naïve decomposition: x ≠ y and x ≠ z and y ≠ z

propagates only as soon as x, y, or z assigned

x{1,2,3}, y{1,2}, z{1,2} should propagate x{3}

x{1,2}, y{1,2}, z{1,2} should exhibit failure without search

2/3/2014

Page 56: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Strong Propagation Idea

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

56

distinct(x0, …, x4) x0{0,1,2} x1{1,2} x2{1,2} x3{2,4,5} x4{5,6}

Collect all solutions (permutations) x0=0 x1=1 x2=2 x3=4 x4=5

x0=0 x1=1 x2=2 x3=4 x4=6

x0=0 x1=1 x2=2 x3=5 x4=6

x0=0 x1=2 x2=1 x3=4 x4=5

x0=0 x1=2 x2=1 x3=4 x4=6

x0=0 x1=2 x2=1 x3=5 x4=6

Collect values from solutions x0{0} x1{1,2} x2{1,2} x3{4,5} x4{5,6}

2/3/2014

Page 57: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Strong Propagation Idea

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

57

distinct(x0, …, x4) x0{0,1,2} x1{1,2} x2{1,2} x3{2,4,5} x4{5,6}

Collect all solutions (permutations) x0=0 x1=1 x2=2 x3=4 x4=5

x0=0 x1=1 x2=2 x3=4 x4=6

x0=0 x1=1 x2=2 x3=5 x4=6

x0=0 x1=2 x2=1 x3=4 x4=5

x0=0 x1=2 x2=1 x3=4 x4=6

x0=0 x1=2 x2=1 x3=5 x4=6

Collect values from solutions x0{0} x1{1,2} x2{1,2} x3{4,5} x4{5,6}

infeasible: all

permutations!

2/3/2014

Page 58: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Strong Propagation Idea

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

58

distinct(x0, …, x4) x0{0,1,2} x1{1,2} x2{1,2} x3{2,4,5} x4{5,6}

Characterize all solutions x0=0 x1=1 x2=2 x3=4 x4=5

x0=0 x1=1 x2=2 x3=4 x4=6

x0=0 x1=1 x2=2 x3=5 x4=6

x0=0 x1=2 x2=1 x3=4 x4=5

x0=0 x1=2 x2=1 x3=4 x4=6

x0=0 x1=2 x2=1 x3=5 x4=6

Collect values from solutions x0{0} x1{1,2} x2{1,2} x3{4,5} x4{5,6}

translate into

simple graph

problem!

[Régin. A Filtering Algorithm for

Constraints of Difference in CSPs.

AAAI 1994]

2/3/2014

Page 59: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Variable Value Graph

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

59

Translates propagation into graph problem variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0,1,2}

x1

x2

x3

x4

0

1

2

4

5

6

2/3/2014

Page 60: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Variable Value Graph

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

60

Translates propagation into graph problem variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0,1,2}

x1{1,2}

x2{1,2}

x3{2,4,5}

x4{5,6}

0

1

2

4

5

6

2/3/2014

Page 61: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (1)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

61

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{1}

x2{2}

x3{4}

x4{5}

0

1

2

4

5

6

2/3/2014

Page 62: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (1)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

62

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{1}

x2{2}

x3{4}

x4{5}

0

1

2

4

5

6

let’s go for

intuition, not

technicalities!

2/3/2014

Page 63: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (2)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

63

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{1}

x2{2}

x3{4}

x4{6}

0

1

2

4

5

6

2/3/2014

Page 64: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (3)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

64

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{1}

x2{2}

x3{5}

x4{6}

0

1

2

4

5

6

2/3/2014

Page 65: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (4)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

65

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{2}

x2{1}

x3{4}

x4{5}

0

1

2

4

5

6

2/3/2014

Page 66: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (5)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

66

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{2}

x2{1}

x3{4}

x4{6}

0

1

2

4

5

6

2/3/2014

Page 67: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Graph Solution (6)

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

67

Solutions maximal matchings in variable value graph variable nodes → value nodes

x0

x1

x2

x3

x4

x0{0}

x1{2}

x2{1}

x3{5}

x4{6}

0

1

2

4

5

6

2/3/2014

Page 68: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Characterizing All Solutions

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

68

x1

x2

x1{1,2}

x2{1,2}

1

2

2/3/2014

Page 69: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Characterizing All Solutions

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

69

Non-matched edges in alternating cycle with

matched edges...

...appear in some matching

...part of some solution

x1

x2

x1{1,2}

x2{1,2}

1

2

2/3/2014

Page 70: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Characterizing All Solutions

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

70

Non-matched edges in alternating cycle with

matched edges...

...appear in some matching

...part of some solution

x1

x2

x1{1,2}

x2{1,2}

1

2

just swap

matched with

free!

2/3/2014

Page 71: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Characterizing All Solutions

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

71

x3

x4

x3{2,4,5}

x4{5,6}

4

5

6

2/3/2014

Page 72: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Characterizing All Solutions

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

72

Non-matched edges in alternating path from

unmatched node ...

...appears in some matching

...part of some solution

x3

x4

x3{2,4,5}

x4{5,6}

4

5

6

2/3/2014

Page 73: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Characterizing All Solutions

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

73

Non-matched edges in alternating path from

unmatched node ...

...appears in some matching

...part of some solution

x3

x4

x3{2,4,5}

x4{5,6}

4

5

6

just swap

matched with

free!

2/3/2014

Page 74: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Variable Value Graph

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

74

Start from any matching (just one)

Mark edges that can be part of a matching

x0

x1

x2

x3

x4

x0{0,1,2}

x1{1,2}

x2{1,2}

x3{2,4,5}

x4{5,6}

0

1

2

4

5

6

2/3/2014

Page 75: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Variable Value Graph

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

75

Start from any matching (just one)

Mark edges that can be part of a matching

x0

x1

x2

x3

x4

x0{0,1,2}

x1{1,2}

x2{1,2}

x3{2,4,5}

x4{5,6}

0

1

2

4

5

6

simple graph

algorithm O(n2.5) [e.g. Ford Fulkerson]

2/3/2014

Page 76: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Variable Value Graph

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

76

Start from any matching (just one)

Mark edges that can be part of a matching

x0

x1

x2

x3

x4

x0{0,1,2}

x1{1,2}

x2{1,2}

x3{2,4,5}

x4{5,6}

0

1

2

4

5

6

even simpler

graph algorithm

O(n) [e.g. Tarjan’s SCC]

2/3/2014

Page 77: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Propagation, Finally!

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

77

Prune unmarked edges...

…and their corresponding values

x0

x1

x2

x3

x4

x0{0}

x1{1,2}

x2{1,2}

x3{4,5}

x4{5,6}

0

1

2

4

5

6

2/3/2014

Page 78: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Summary

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

78

Constraints capture problem structure (”global”) ease modeling (commonly recurring structures)

enable solving (efficient and strong algorithms available)

Constraints as reusable

powerful

software components in the toolbox

2/3/2014

Page 79: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

SMM: Strong Propagation

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

79

SEND

+ MORE

= MONEY

9567

+ 1085

= 10652

2/3/2014

Page 80: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

bin packing

Branching Heuristics 80

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 81: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Branching Heuristics

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

81

CP advantage: programmable heuristics application domain dependent: scheduling, assignment, bin-

packing, …

requires deep insight into problem structure

limited reuse even though recurring principles

CP disadvantage: universal heuristics just emerging CP solver as “black box” tool

ultimate goal: robust and autonomous search

contrast to SAT and MIP

Here: bin packing as case study for programmable heuristics

2/3/2014

Page 82: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

First-Fail Principle

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

82

Could be paraphrased as:

to succeed, try first where you are most

likely to fail! minimize cost to find out that decision is in fact wrong

cost = amount of search needed (depth-first search)

Avoid thrashing make wrong decision: search will have to find out

make many unrelated or non-difficult decisions

takes ages to find that decision was wrong!

[Haralick, Elliott. Increasing tree search efficiency for constraint satisfaction problems. Artificial Intelligence, 1980]

2/3/2014

Page 83: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Bin Packing Problem

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

83

Given bins of capacity c

n items of size si

Sought find least number of bins such that each item packed into bin

6 6 6 5

3 3

2 2 2 2 2

c=10

2/3/2014

Page 84: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Bin Packing Problem

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

84

Given bins of capacity c

n items of size si

Sought find least number of bins such that each item packed into bin

6 6 6 5

3

3 2

2

2

2 2

c=10

2/3/2014

Page 85: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Simplify Problem

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

85

Repeat simpler problem for m such that:

is it possible to pack n items into m bins?

Restrict m by lower bound

l = (s1 + … + sn) / c

and upper bound

u = #bins from some (non-optimal) packing

Try m between l and u: least feasible m optimal even better lower bounds are known

2/3/2014

Page 86: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Model: Variables

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

86

Bin variable bi for each item i bi {1, …, n} into which bin is item i packed

Load variable lj for each bin j lj {0, …, c} size of items packed into bin j

Packing variable xij xij {0,1} whether item i is packed into bin j

2/3/2014

Page 87: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Model: Constraints

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

87

Total load is size of all items

l1 + … + lm = s1 + … + sn

Load corresponds to items packed into bin j

lj = s1x1j + … + snxnj

Bin variables correspond to packing variables

xij = 1 if and only if bi=j

2/3/2014

Page 88: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Constraint Model: Improved

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

88

Use dedicated bin packing constraint

binpacking(b1,…,bn, s1,…,sn, l1,…,lm) no packing variables needed

much stronger propagation

If items i and j with i<j have same size

bi ≤ bj reduce search space (“symmetry breaking”)

Assign large items (si > c/2) to fixed bins

... [Shaw. A Constraint for Bin Packing. CP 2004]

2/3/2014

Page 89: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

How To Branch?

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

89

Branch over the bin variables bi

that is: assign items to bins

Which item to pick first: largest!

Which bin to pick first: tightest!

best fit (least slack)!

“Easy” to express with standard heuristics…

…can programming do more?

2/3/2014

Page 90: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Programming Heuristic

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

90

Avoid search perfect fit of item i to bin b: assign i to b (no search)

all bins have same slack: assign i to some b

Learn from failure try to assign item i to bin b

if search fails: no other item j with si=sj can go to b

if search fails: item i cannot go to bin with same slack

(also for items j with si=sj)

“symmetry breaking during search”

known as CDBF: complete decreasing best-fit

[Gent, Walsh. From approximate to optimal solutions: constructing pruning and propagation rules.

IJCAI 1997.]

2/3/2014

Page 91: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

beauty and curse of

constraint programming

Local Reasoning 91

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 92: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

92

11 4

5

14 10

17

3

6 4

3

10

3

2/3/2014

Page 93: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

93

Fields take digits

Hints describe for row or column

digit sum must be hint

digits must be distinct

11 4

5

14 10

17

3

6 4

3

10

3

2/3/2014

Page 94: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

94

For hint 3

1 + 2

11 4

5

14 10

17

3

6 4

3

10

3

1

2

2/3/2014

Page 95: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

95

For hint 3

1 + 2

or

2 + 1

11 4

5

14 10

17

3

6 4

3

10

3

2

1

2/3/2014

Page 96: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

96

For hint 4

1 + 3

11 4

5

14 10

17

3

6 4

3

10

3

1 3

2/3/2014

Page 97: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

97

For hint 4

1 + 3

or

3 + 1

11 4

5

14 10

17

3

6 4

3

10

3

3 1

2/3/2014

Page 98: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

98

For hint 3

1 + 2

For hint 4

1 + 3

11 4

5

14 10

17

3

6 4

3

10

3

3 1

2

2/3/2014

Page 99: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

2

Kakuro Solution

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

99

11 4

5

14 10

17

3

6 4

3

10

3

3

9 5 1 2

5 1 3 1

3 1 4 2

2 1

2/3/2014

Page 100: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Modeling and Solving Kakuro

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

100

Obvious model: for each hint distinct constraint

sum constraint

Good case... (?) few variables per hint

few values per variable

Let’s try it... 22×14, 114 hints: 9638 search nodes, 2min 40sec

90×124, 4558 hints: ? search nodes, ? minutes

years? centuries? eons?

2/3/2014

Page 101: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Local Reasoning: Decomposition

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

101

Possible values = all digits

Propagating sum = 4 in isolation!

Propagating distinct in isolation!

Propagating both in combination!

but how?

where is the tool (constraint) for it?

4 1..9 1..9

4 1..3 1..3

all solutions:

1,3 2,2 3,1

4 1..3 1..3

all solutions:

1,2 1,3 2,1

2,3 3,1 3,2

4 1,3 1,3

all solutions:

1,3 3,1

2/3/2014

Page 102: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Failing for Kakuro...

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

102

Beauty of constraint programming local reasoning

propagators are independent

variables as simple communication channels

Curse of constraint programming local reasoing

propagators are independent

variables as simple communication channels

2/3/2014

Page 103: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

personnel rostering

Kakuro reconsidered

User-defined Constraints 103

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 104: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Modeling Rostering: User-defined

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

104

Personnel rostering: example (nonsensical) one day off (o) after weekend shift (w)

one day off (o) after two consectuive long shifts (l)

normal shifts (n)

Infeasible to implement propagator for ever-

changing rostering constraints

User-defined constraints: describe legal

rosters by regular expression (wo | llo | n)*

2/3/2014

Page 105: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Regular Constraint

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

105

Propagation idea: maintain all accepting paths in DFA from start state (0) to a final state (0): solutions!

symbols on transitions comply with variable values

[Pesant. A Regular Language Membership Constraint for Finite Sequences of Variables. CP 2004]

0

1

2

n

l

l

w

o

(wo | llo | n)*

regular(x1, …, xn, r)

x1 … xn word in r

or, accepted by DFA d for r

2/3/2014

Page 106: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Kakuro Reconsidered

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

106

Real model: for each hint one regular constraint combining distinct and sum

example: regular expression for hint 5 with two fields

14 | 23 | 32 | 41

precompute when model is setup

Good case... few solutions for combined constraint

Let’s try again (precomputation time included) 22×14, 114 hints: 0 search nodes, 28 msec

90×124, 4558 hints: 0 search nodes, 345 msec

2/3/2014

Page 107: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Summary

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

107

User-defined constraints high degree of flexibility

efficient and perfect propagation

limited to medium-sized constraints

even better methods than regular known

Kakuro: decomposition is harmful [again] capture essential structure by few constraints

best by single constraint

2/3/2014

Page 108: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Summary 108

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 2/3/2014

Page 109: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Essence

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

109

Constraint programming is about…

...local reasoning exploiting structure

...an array of modeling tools for solving

Strength simplicity, compositionality, exploiting structure

rich toolbox of techniques

Challenges lack of global picture during search

difficult to find global picture due to rich structure

2/3/2014

Page 110: THE CONSTRAINT - GitHub Pages · Sudoku The CP'ers Toolbox, Schulte, SCALE, KTH & SICS 4 Assign blank fields digits such that: digits distinct per rows, columns, blocks 9 2 2 5 9

Resources

The CP'ers Toolbox, Schulte, SCALE, KTH & SICS

110

Overview Rossi, Van Beek, Walsh, eds. Handbook of Constraint

Programming, Elsevier, 2006 (around 950 pages).

National perspective Flener, Carlsson, Schulte. Constraint Programming in

Sweden, IEEE Intelligent Systems, pages 87-89. IEEE Press, March/April, 2009.

SweConsNet: Swedish network for people interested in constraints. Yearly workshops, see:

www.it.uu.se/research/SweConsNet/

Advanced (ID2204)/graduate (ID3005) course taught by me

period 4, 2014

2/3/2014