Implementing a Constraint Solver: A Case Study Emmanuel Hebrard - - PowerPoint PPT Presentation

implementing a constraint solver a case study
SMART_READER_LITE
LIVE PREVIEW

Implementing a Constraint Solver: A Case Study Emmanuel Hebrard - - PowerPoint PPT Presentation

Implementing a Constraint Solver: A Case Study Emmanuel Hebrard Cork Constraint Computation Centre & University College Cork Road-map Goal Blueprint Data Structures Propagation Search Code Optimization


slide-1
SLIDE 1

Implementing a Constraint Solver: A Case Study

Emmanuel Hebrard

Cork Constraint Computation Centre & University College Cork

slide-2
SLIDE 2

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Search
  • Code Optimization
  • Competition
slide-3
SLIDE 3

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Search
  • Code Optimization
  • Competition
slide-4
SLIDE 4

Goal

slide-5
SLIDE 5

Goal

  • “Constraint Programming

represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it. ” [E. Freuder]

slide-6
SLIDE 6

Goal

  • “Constraint Programming

represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it. ” [E. Freuder]

  • ...if given enough time!
slide-7
SLIDE 7

Does Mistral achieve this goal?

slide-8
SLIDE 8

Does Mistral achieve this goal?

  • Library in C++
slide-9
SLIDE 9

Does Mistral achieve this goal?

  • Library in C++
  • Developed during my PhD
  • Ilog Solver is not open

source

  • Good substitutes (Gecode,

Choco and others) did not exist yet

slide-10
SLIDE 10

Does Mistral achieve this goal?

  • Library in C++
  • Developed during my PhD
  • Ilog Solver is not open

source

  • Good substitutes (Gecode,

Choco and others) did not exist yet

  • Under GNU General Public

License

slide-11
SLIDE 11

Does Mistral achieve this goal?

  • Library in C++
  • Developed during my PhD
  • Ilog Solver is not open

source

  • Good substitutes (Gecode,

Choco and others) did not exist yet

  • Under GNU General Public

License

  • Why Mistral?
  • because Mistral IS a

Terrific Recursive Acronym for a Library.

slide-12
SLIDE 12

Does Mistral achieve this goal?

  • Library in C++
  • Developed during my PhD
  • Ilog Solver is not open

source

  • Good substitutes (Gecode,

Choco and others) did not exist yet

  • Under GNU General Public

License

  • Why Mistral?
  • because Mistral IS a

Terrific Recursive Acronym for a Library.

Cork Montpellier Frederick Mistral

slide-13
SLIDE 13

Model: Golomb ruler

slide-14
SLIDE 14

Model: Golomb ruler

slide-15
SLIDE 15

Model: Golomb ruler

slide-16
SLIDE 16

Model: Golomb ruler

slide-17
SLIDE 17

Model: Golomb ruler

slide-18
SLIDE 18

Model: Golomb ruler

slide-19
SLIDE 19

Message:

slide-20
SLIDE 20

Message:

  • Efficient implementation
  • Details do matter
slide-21
SLIDE 21

Message:

  • Efficient implementation
  • Details do matter
  • Modeling choices
  • Automatic choices of the best representation/algorithm

Variable (Constant, Boolean, Interval, Bitset, List, ...) Constraints (Specific algorithm, Decomposition, Generic

algorithms, ...)

Heuristics

  • Automatic rewritting?
slide-22
SLIDE 22

Message:

  • Efficient implementation
  • Details do matter
  • Modeling choices
  • Automatic choices of the best representation/algorithm

Variable (Constant, Boolean, Interval, Bitset, List, ...) Constraints (Specific algorithm, Decomposition, Generic

algorithms, ...)

Heuristics

  • Automatic rewritting?
  • Robustness
  • Worst case principle
slide-23
SLIDE 23

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Search
  • Code Optimization
  • Competition
slide-24
SLIDE 24

A little bit of structure

Search

  • Search algorithms
  • Heuristics

Data Structures

  • Variables
  • Backtrackable types

Propagation

  • Library of constraints
  • Generic algorithms
slide-25
SLIDE 25

A little bit of structure

Search

  • Search algorithms
  • Heuristics

Data Structures

  • Variables
  • Backtrackable types

Propagation

  • Library of constraints
  • Generic algorithms

Backtrack Decision Domain events Filtering Learning

slide-26
SLIDE 26

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Variables (Baktracks)
  • Propagation
  • Search
  • Code Optimization
  • Competition
slide-27
SLIDE 27

Backtrackable Data- Structures

  • Copying/Trailing
  • See Shulte’

s papers and PhD Thesis

  • Copying

Easier to implement data

structures

Easier to implement search

strategies

Easier to parallelize

  • Trailing

Do only necessary work Memory efficient

Copying Trailing

slide-28
SLIDE 28

Domain as a Bitset

  • One 32 bits word for every

value in [min(D)..max(D)]

11100101

X in {0,1,2,5,7,18,19,21}

00000000 00110100

slide-29
SLIDE 29

Domain as a Bitset

  • One 32 bits word for every

value in [min(D)..max(D)]

  • For every word, we allocate

as many word as values in that word:

  • 0((max-min+1) + 32*|D|) bits

11100101

X in {0,1,2,5,7,18,19,21}

00000000 00110100

slide-30
SLIDE 30

Domain as a Bitset

  • One 32 bits word for every

value in [min(D)..max(D)]

  • For every word, we allocate

as many word as values in that word:

  • 0((max-min+1) + 32*|D|) bits

11100101

X in {0,1,2,5,7,18,19,21}

00000000 00110100

Allocated statically

slide-31
SLIDE 31

Domain as a Bitset

00000000

X in {0,1,2,5,7,18,19,21}

slide-32
SLIDE 32

Domain as a Bitset

11100101 00000000 00110100

X in {0,1,2,5,7,18,19,21}

slide-33
SLIDE 33

Domain as a Bitset

11100101 00000000 00110100

decision 1

{0,5,7,18,19,21} X in {0,1,2,5,7,18,19,21} 1,2,

slide-34
SLIDE 34

Domain as a Bitset

11100101 10000101 00000000 00110100

decision 1

{0,5,7,18,19,21}

w1

X in {0,1,2,5,7,18,19,21} 1,2,

slide-35
SLIDE 35

Domain as a Bitset

11100101 10000101 00000000 00110100

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

w1

X in {0,1,2,5,7,18,19,21} 1,2, ,21

slide-36
SLIDE 36

Domain as a Bitset

11100101 10000101 00000000 00110100 00110000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21

slide-37
SLIDE 37

Domain as a Bitset

11100101 10000101 00000000 00110100 00110000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

decision 3

{0,5,18}

w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21 7,18,19

slide-38
SLIDE 38

Domain as a Bitset

11100101 10000101 10000100 00000000 00110100 00110000 00100000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

decision 3

{0,5,18}

w1 w2 w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21 7,18,19

slide-39
SLIDE 39

Domain as a Bitset

11100101 10000101 10000100 00000000 00110100 00110000 00100000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

decision 3

{0,5,18}

decision 4

{0,5}

w1 w2 w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21 7,18,19 ,7,18,

slide-40
SLIDE 40

w2

Domain as a Bitset

11100101 10000101 10000100 00000000 00110100 00110000 00100000 00000000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

decision 3

{0,5,18}

decision 4

{0,5}

w1 w2 w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21 7,18,19 ,7,18,

slide-41
SLIDE 41

w2

Domain as a Bitset

11100101 10000101 10000100 00000000 00110100 00110000 00100000 00000000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

decision 3

{0,5,18}

decision 4

{0,5}

w1 w2 w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21 7,18,19 ,7,18,

slide-42
SLIDE 42

Domain as a Bitset

11100101 10000101 10000100 00000000 00110100 00110000 00100000 00000000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

decision 3

{0,5,18}

w1 w2 w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21 7,18,19

slide-43
SLIDE 43

Domain as a Bitset

11100101 10000101 10000100 00000000 00110100 00110000 00100000 00000000

decision 1

{0,5,7,18,19,21}

decision 2

{0,5,7,18,19}

w1 w2

X in {0,1,2,5,7,18,19,21} 1,2, ,21

slide-44
SLIDE 44

Domain as a List

slide-45
SLIDE 45

Domain as a List

9 1 5 12 2 14 6 4

list

slide-46
SLIDE 46

Domain as a List

9 1 5 12 2 14 6 4

8 size list

slide-47
SLIDE 47

Domain as a List

9 1 5 12 2 14 6 4 1 4 7 2 6 3 5 ∞ ∞ ∞ ∞ ∞ ∞ ∞

8 size list index

slide-48
SLIDE 48

Domain as a List

9 1 5 12 2 14 6 4 1 4 7 2 6 3 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

8 membership: index[v] < size size list index

slide-49
SLIDE 49

Domain as a List

9 1 5 12 2 14 6 4 1 4 7 2 6 3 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

8 membership: index[v] < size size list index

slide-50
SLIDE 50

Domain as a List

9 1 5 12 2 14 6 4 1 4 7 2 6 3 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

8 membership: index[v] < size size list index

slide-51
SLIDE 51

Domain as a List

9 1 5 12 4 14 6 2 1 7 4 2 6 3 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

7 membership: index[v] < size size list index

slide-52
SLIDE 52

Domain as a List

6 12 5 1 4 14 9 2 3 7 4 2 6 1 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

2 membership: index[v] < size size list index

slide-53
SLIDE 53

Domain as a List

6 12 5 1 4 14 9 2 3 7 4 2 6 1 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

2

Backtrack

membership: index[v] < size size list index

slide-54
SLIDE 54

Domain as a List

6 12 5 1 4 14 9 2 3 7 4 2 6 1 5

1 2 3 4 5 6 7 8 9 10 11 13 14 12

∞ ∞ ∞ ∞ ∞ ∞ ∞

8 membership: index[v] < size size list index

slide-55
SLIDE 55

Pigeon holes

slide-56
SLIDE 56

Pigeon holes

  • Domain as a Bitset:

Space complexity in O(max-min) Restore up to 32 values at a

time

600,000 Bts/second

slide-57
SLIDE 57

Pigeon holes

  • Domain as a Bitset:

Space complexity in O(max-min) Restore up to 32 values at a

time

600,000 Bts/second

  • Domain as a List:

Similar space complexity Restore any number of values in

  • ne operation

900,000 Bts/second However, operations are much

slower on the list (interval reasoning, set operations)

slide-58
SLIDE 58

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Nested predicates
  • GAC valid v allowed
  • Search
  • Code Optimization
  • Competition
slide-59
SLIDE 59

Constraint Propagation

  • Variable/Constraint Queue
  • Specific Propagators
  • Nested Predicates
  • Generic AC algorithms
  • Binary: AC3Bitset
  • Tight: GAC2001Allowed
  • Loose: GAC3rValid

Pruning:

slide-60
SLIDE 60

Nested Predicates

<predicate name="P0"> <parameters>int X0 int X1 int X2 int X3 int X4 int X5</parameters> <expression> <functional>or(le(add(X0,X1),X2),le(add(X3,X4),X5))<functional> </expression> </predicate> <constraint name="C0" arity="2" scope="V0 V1" reference="P0"> <parameters>V0 85 V1 V1 64 V0</parameters> </constraint>

Example: open-shop scheduling:

slide-61
SLIDE 61

Nested Predicates: Decomposition

Example: open-shop scheduling:

Or < > + +

64 85 X1 X2 X1 X2

slide-62
SLIDE 62

Nested Predicates: Decomposition

Example: open-shop scheduling:

Or < > +

64 X2 Y1 Y1 = X1 + 85 X1 X2

slide-63
SLIDE 63

Nested Predicates: Decomposition

Example: open-shop scheduling:

Or > +

64 Y2 Y1 = X1 + 85 Y2 = (Y1 < X2) X1 X2

slide-64
SLIDE 64

Nested Predicates: Decomposition

Example: open-shop scheduling:

Or >

Y2 Y3 Y1 = X1 + 85 Y3 = X2 + 64 Y2 = (Y1 < X2) X1

slide-65
SLIDE 65

Nested Predicates: Decomposition

Example: open-shop scheduling:

Or

Y2 Y4 Y1 = X1 + 85 Y3 = X2 + 64 Y2 = (Y1 < X2) Y4 = (Y3 < X1)

slide-66
SLIDE 66

Nested Predicates: Decomposition

Example: open-shop scheduling:

Y1 = X1 + 85 Y3 = X2 + 64 Y2 = (Y1 < X2) Y4 = (Y3 < X1) (Y2 ∨ Y4)

slide-67
SLIDE 67

Nested Predicates: GAC-Checker

Example: open-shop scheduling:

Or < > + +

64 85 check ([30, 100]) { assign leaves; query root; } X1 X2 X1 X2

slide-68
SLIDE 68

Nested Predicates: GAC-Checker

Example: open-shop scheduling:

Or < > + +

64 85 check ([30, 100]) { assign leaves; query root; } assign leaves; X1 X2 X1 X2

30 100 30 100

slide-69
SLIDE 69

Nested Predicates: GAC-Checker

Example: open-shop scheduling:

Or < > + +

64 85 check ([30, 100]) { assign leaves; query root; } assign leaves; query root;

False

115 164

False False

X1 X2 X1 X2

30 100 30 100

slide-70
SLIDE 70

Golomb ruler / FAPP

Instance: Decomposition GAC-Checker

slide-71
SLIDE 71

Golomb ruler / FAPP

Instance: Decomposition GAC-Checker Golomb Ruler 128 nodes 0.18 seconds 87 nodes 38.22 seconds

slide-72
SLIDE 72

Golomb ruler / FAPP

Instance: Decomposition GAC-Checker Golomb Ruler 128 nodes 0.18 seconds 87 nodes 38.22 seconds FAPP 60181 nodes 55.18 seconds 374 nodes 1.18 seconds

slide-73
SLIDE 73

Making the Right Choice

slide-74
SLIDE 74

Making the Right Choice

Feature GAC Decomp OSP

slide-75
SLIDE 75

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary

slide-76
SLIDE 76

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary Ratio node/leaves

+

  • 5/2
slide-77
SLIDE 77

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary Ratio node/leaves

+

  • 5/2

Domain continuity

  • +

no holes

slide-78
SLIDE 78

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary Ratio node/leaves

+

  • 5/2

Domain continuity

  • +

no holes Cartesian product cardinality

  • +

>60,000

slide-79
SLIDE 79

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary Ratio node/leaves

+

  • 5/2

Domain continuity

  • +

no holes Cartesian product cardinality

  • +

>60,000 Boolean domains

  • +

no

slide-80
SLIDE 80

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary Ratio node/leaves

+

  • 5/2

Domain continuity

  • +

no holes Cartesian product cardinality

  • +

>60,000 Boolean domains

  • +

no Total number of constraints

+

  • small
slide-81
SLIDE 81

Making the Right Choice

Feature GAC Decomp OSP Constraint Arity

  • +

binary Ratio node/leaves

+

  • 5/2

Domain continuity

  • +

no holes Cartesian product cardinality

  • +

>60,000 Boolean domains

  • +

no Total number of constraints

+

  • small

Decomposition!

slide-82
SLIDE 82

GAC Allowed v. GAC Valid

Backtracks/second

Tighter Looser

slide-83
SLIDE 83

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Search
  • Heuristics
  • Code Optimization
  • Competition
slide-84
SLIDE 84

Search Strategies

  • Depth-first, Breadth-first,

LDS,...

  • Branching Choices
  • Domain Splitting
  • Arbitrary Constraint
  • Variable/Value Ordering
  • “Learning” heuristics

Weighted Degree, Impact

slide-85
SLIDE 85

Weighted Heuristics

  • The best general purpose orderings are based on some

kind of learning (or weighting)

  • Weighted Degree [Boussemart, Hemery, Lecoutre, Sais 2004]
  • Impact [Refalo 2004]
  • A “Weighter” can suscribe for different types of event
  • Weighted degree: failures
  • Impact: Decisions, success, failures
  • This architecture allows easy development of variations

around these models

  • Why isn’t Impact/Weighted Degree any good?
slide-86
SLIDE 86

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Search
  • Code Optimization
  • Competition
slide-87
SLIDE 87

Optimisation: Binary Extensional

  • Standard algorithms:
  • AC3-bitset, Variable queue (fifo), revision condition
  • Profiling, what does take time?
  • Propagation:.......................................... 68%
  • “&” operation:..................................... 25.9%
  • Domain iteration:................................ 20.6%
  • AC3 (queuing/dequeuing):................ 11.4%
  • Revision condition + virtual call:.. 10.0%
  • Data structure modification:........... 19%
  • Domain modification:.......................... 19.0%
  • Search:..................................................... 12%
  • Trailing:..................................................... 9.7%
  • Variable choice + Branching:........... 2.2%
slide-88
SLIDE 88

Intersection on Bitsets (25%)

inline bool MistralSet::wordIntersect(const MistralSet& s) const { return ( table[neg_words] & s.table[neg_words] ) ; } bool VariableList::wordIntersect(const MistralSet& s) const { return values.wordIntersect(s); }

slide-89
SLIDE 89

Values Iteration (20%)

slide-90
SLIDE 90

Values Iteration (20%)

  • Random binary CSP
slide-91
SLIDE 91

Values Iteration (20%)

  • Random binary CSP
  • Domain as a Bitset:
  • 6,500 Bts/second
slide-92
SLIDE 92

Values Iteration (20%)

  • Random binary CSP
  • Domain as a Bitset:
  • 6,500 Bts/second
  • Domain as a List (hybrid

bitset/list):

  • 10,000 Bts/second
  • Values are stored

contiguously in an array

  • The order does not matter
slide-93
SLIDE 93

Road-map

  • Goal
  • Blueprint
  • Data Structures
  • Propagation
  • Search
  • Code Optimization
  • Competition
slide-94
SLIDE 94

Quick Comparison

(#instances)

Abscon Choco Mistral

slide-95
SLIDE 95

Quick Comparison

(#instances)

30% 50% 70% 90% BIN-EXT BIN-INT N-EXT N-INT GLOBAL ALL

Abscon Choco Mistral

slide-96
SLIDE 96

Quick Comparison

(#instances)

30% 50% 70% 90% BIN-EXT BIN-INT N-EXT N-INT GLOBAL ALL

Abscon Choco Mistral

slide-97
SLIDE 97

Quick Comparison

(cpu time)

Abscon Choco Mistral

slide-98
SLIDE 98

Quick Comparison

(cpu time)

20s 60s 100s 140s 180s BIN-EXT BIN-INT N-EXT N-INT GLOBAL

Abscon Choco Mistral

slide-99
SLIDE 99

Quick Comparison

(cpu time)

20s 60s 100s 140s 180s BIN-EXT BIN-INT N-EXT N-INT GLOBAL

Abscon Choco Mistral

slide-100
SLIDE 100

Backtracks v CPU-time

750,000 1,500,000 2,250,000 3,000,000 500 1,000 1,500 2,000

Mistral Abscon Choco

Bkts CPU-time

rand-2-40-19-443-230-* and frb40-19

slide-101
SLIDE 101

Backtracks v CPU-time

750,000 1,500,000 2,250,000 3,000,000 500 1,000 1,500 2,000

Mistral Abscon Choco

Bkts CPU-time

rand-2-40-19-443-230-* and frb40-19

slide-102
SLIDE 102

Conclusion

slide-103
SLIDE 103

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

slide-104
SLIDE 104

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

  • It is so.
slide-105
SLIDE 105

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

  • It is so.
  • But you’ll learn a lot!
slide-106
SLIDE 106

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

  • It is so.
  • But you’ll learn a lot!
  • Adaptability
slide-107
SLIDE 107

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

  • It is so.
  • But you’ll learn a lot!
  • Adaptability
  • Attention to details
slide-108
SLIDE 108

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

  • It is so.
  • But you’ll learn a lot!
  • Adaptability
  • Attention to details
  • Robustness
slide-109
SLIDE 109

Conclusion

  • Implementing a constraint

solver may appear like a daunting task

  • It is so.
  • But you’ll learn a lot!
  • Adaptability
  • Attention to details
  • Robustness
  • Weaknesses are always

more obvious to a user than strengths