Top Banner
ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian Schulte [email protected] Software and Computer Systems School of Electrical Engineering and Computer Science KTH Royal Institute of Technology Sweden
39

Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

Sep 06, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

ID2204: Constraint Programming

Introduction & OverviewLecture 01, 2018-03-19

Christian [email protected]

Software and Computer SystemsSchool of Electrical Engineering and Computer ScienceKTH Royal Institute of TechnologySweden

Page 2: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Sudoku

� 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

ID2204, L01, Christian Schulte, EECS, KTH 4

Page 3: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagation

� 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

ID2204, L01, Christian Schulte, EECS, KTH 10

Page 4: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagation

� Prune digits from fields such that:digits distinct per rows, columns, blocks

9

2

2 5

9

7 3

6

96

2

7

1

94

8

6 3

4

6 8

1

8

1,3,5,6,7,8

ID2204, L01, Christian Schulte, EECS, KTH 11

Page 5: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagation

� Prune digits from fields such that:digits distinct per rows, columns, blocks

9

2 9

52

7 3

6

2

6 9

7

4 9

1

8

6 3

86

4 1

8

1,3,6,7

ID2204, L01, Christian Schulte, EECS, KTH 12

Page 6: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagation

� 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

ID2204, L01, Christian Schulte, EECS, KTH 13

Page 7: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Iterated Propagation

� 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

ID2204, L01, Christian Schulte, EECS, KTH 14

Page 8: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Running Example: SMM

� Find distinct digits for letters, such that

SEND

+ MORE

= MONEY

ID2204, L01, Christian Schulte, EECS, KTH 19

Page 9: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Constraint Model for SMM

� 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+YSz0 Mz0

ID2204, L01, Christian Schulte, EECS, KTH 20

Page 10: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Finding a Solution

� Compute with possible values � rather than enumerating assignments

� Prune inconsistent values� constraint propagation

� Search� branch: define search tree� explore: explore search tree for solution

ID2204, L01, Christian Schulte, EECS, KTH 22

Page 11: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

Constraint Propagation

Page 12: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Constraint Store

� Maps variables to possible values� Others: finite sets, intervals, trees, ...

x�{3,4,5} y�{3,4,5}

finite domain constraints

ID2204, L01, Christian Schulte, EECS, KTH 26

Page 13: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Implement (non-basic) constraints

distinct(x1,…,xn)

x + 2*y = z

ID2204, L01, Christian Schulte, EECS, KTH 27

Page 14: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Amplify store by constraint propagation

x�{3,4,5} y�{3,4,5}

xty y>3

ID2204, L01, Christian Schulte, EECS, KTH 29

Page 15: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Amplify store by constraint propagation

x�{3,4,5} y�{4,5}

xty y>3

ID2204, L01, Christian Schulte, EECS, KTH 30

Page 16: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Amplify store by constraint propagation

x�{3,4,5} y�{4,5}

xty y>3

ID2204, L01, Christian Schulte, EECS, KTH 31

Page 17: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Amplify store by constraint propagation

x�{4,5} y�{4,5}

xty y>3

ID2204, L01, Christian Schulte, EECS, KTH 32

Page 18: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Amplify store by constraint propagation� Disappear when done (subsumed, entailed)

� no more propagation possible

x�{4,5} y�{4,5}

xty y>3

ID2204, L01, Christian Schulte, EECS, KTH 33

Page 19: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagators

� Amplify store by constraint propagation� Disappear when done (subsumed, entailed)

� no more propagation possible

x�{4,5} y�{4,5}

xty

ID2204, L01, Christian Schulte, EECS, KTH 34

Page 20: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Propagation for SMM

� Results in storeS�{9} E�{4,…,7} N�{5,…,8} D�{2,…,8} M�{1} O�{0} R�{2,…,8} Y�{2,…,8}

� Propagation alone not sufficient!� create simpler sub-problems� branching

ID2204, L01, Christian Schulte, EECS, KTH 35

Page 21: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Constraints and Propagators

� Constraints state relations among variables� which value combinations satisfy constraint

� Propagators implement constraints� prune values in conflict with constraint

� Constraint propagation drives propagators for several constraints

ID2204, L01, Christian Schulte, EECS, KTH 36

Page 22: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

Search

Page 23: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Search: Branching

� Create subproblems with additional information� enable further constraint propagation

x�{4,5} y�{4,5}

xty

x�{4} y�{4}

xty

x�{5} y�{4,5}

xty

x=4 xz4

ID2204, L01, Christian Schulte, EECS, KTH 39

Page 24: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Example Branching Strategy

� Pick variable x with at least two values� Pick value n from domain of x� Branch with

x=n and xzn

� Part of model

ID2204, L01, Christian Schulte, EECS, KTH 40

Page 25: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Search: Exploration

� Iterate propagation and branching� Orthogonal: branching ' exploration� Nodes:

x Unsolved x Failed x Succeeded

ID2204, L01, Christian Schulte, EECS, KTH 41

Page 26: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

SMM: Unique Solution

SEND+ MORE= MONEY

9567+ 1085= 10652

ID2204, L01, Christian Schulte, EECS, KTH 42

Page 27: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Heuristics for Branching

� Which variable� least possible values (first-fail)� application dependent heuristic

� Which value� minimum, median, maximum

x=m or xzm� split with median m

x<m or xtm

� Problem specific

ID2204, L01, Christian Schulte, EECS, KTH 43

Page 28: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

SMM: Solution With First-fail

SEND+ MORE= MONEY

9567+ 1085= 10652

ID2204, L01, Christian Schulte, EECS, KTH 44

Page 29: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Send Most Money (SMM++)

� Find distinct digits for letters, such that

and MONEY maximal

SEND

+ MOST

= MONEY

ID2204, L01, Christian Schulte, EECS, KTH 45

Page 30: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Best Solution Search

� Naïve approach:� compute all solutions� choose best

� Branch-and-bound approach:� compute first solution� add “betterness” constraint to open nodes� next solution will be “better”� prunes search space

ID2204, L01, Christian Schulte, EECS, KTH 46

Page 31: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Branch-and-bound Search

� Find first solutionID2204, L01, Christian Schulte, EECS, KTH 47

Page 32: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Branch-and-bound Search

� Explore with additional constraintID2204, L01, Christian Schulte, EECS, KTH 49

Page 33: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Branch-and-bound Search

� Guarantees better solutionsID2204, L01, Christian Schulte, EECS, KTH 51

Page 34: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Branch-and-bound Search

� Last solution bestID2204, L01, Christian Schulte, EECS, KTH 52

Page 35: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Branch-and-bound Search

� Proof of optimalityID2204, L01, Christian Schulte, EECS, KTH 53

Page 36: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Modelling SMM++

� Constraints and branching as before� Order among solutions with constraints

� so-far-best solution S,E,N,D,M,O,T,Y� current node S,E,N,D,M,O,T,Y� constraint added

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

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

ID2204, L01, Christian Schulte, EECS, KTH 54

Page 37: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

SMM++: Branch-and-bound

SEND+ MOST= MONEY

9782+ 1094= 10876

ID2204, L01, Christian Schulte, EECS, KTH 55

Page 38: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

SMM: Strong Propagation

SEND+ MORE= MONEY

9567+ 1085= 10652

ID2204, L01, Christian Schulte, EECS, KTH 63

Page 39: Introduction & Overviewuser.it.uu.se/~pierref/courses/COCP/slides/T12b-CP... · 2018. 10. 24. · ID2204: Constraint Programming Introduction & Overview Lecture 01, 2018-03-19 Christian

2018-03-19

Acknowledgments

� I am grateful to Pierre Flener for helpful comments and bugreports on these slides

ID2204, L01, Christian Schulte, EECS, KTH 97