SLIDE 1 Implementing a Constraint Solver: A Case Study
Emmanuel Hebrard
Cork Constraint Computation Centre & University College Cork
SLIDE 2 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Search
- Code Optimization
- Competition
SLIDE 3 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Search
- Code Optimization
- Competition
SLIDE 4
Goal
SLIDE 5 Goal
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 Goal
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 7
Does Mistral achieve this goal?
SLIDE 8 Does Mistral achieve this goal?
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 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
License
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
License
- Why Mistral?
- because Mistral IS a
Terrific Recursive Acronym for a Library.
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
License
- Why Mistral?
- because Mistral IS a
Terrific Recursive Acronym for a Library.
Cork Montpellier Frederick Mistral
SLIDE 13
Model: Golomb ruler
SLIDE 14
Model: Golomb ruler
SLIDE 15
Model: Golomb ruler
SLIDE 16
Model: Golomb ruler
SLIDE 17
Model: Golomb ruler
SLIDE 18
Model: Golomb ruler
SLIDE 19
Message:
SLIDE 20 Message:
- Efficient implementation
- Details do matter
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
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 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Search
- Code Optimization
- Competition
SLIDE 24 A little bit of structure
Search
- Search algorithms
- Heuristics
Data Structures
- Variables
- Backtrackable types
Propagation
- Library of constraints
- Generic algorithms
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 Road-map
- Goal
- Blueprint
- Data Structures
- Variables (Baktracks)
- Propagation
- Search
- Code Optimization
- Competition
SLIDE 27 Backtrackable Data- Structures
- Copying/Trailing
- See Shulte’
s papers and PhD Thesis
Easier to implement data
structures
Easier to implement search
strategies
Easier to parallelize
Do only necessary work Memory efficient
Copying Trailing
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 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 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
Domain as a Bitset
00000000
X in {0,1,2,5,7,18,19,21}
SLIDE 32
Domain as a Bitset
11100101 00000000 00110100
X in {0,1,2,5,7,18,19,21}
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 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 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 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 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 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 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 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 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 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 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
Domain as a List
SLIDE 45
Domain as a List
9 1 5 12 2 14 6 4
list
SLIDE 46
Domain as a List
9 1 5 12 2 14 6 4
8 size list
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
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
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
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
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
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
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
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
Pigeon holes
SLIDE 56 Pigeon holes
Space complexity in O(max-min) Restore up to 32 values at a
time
600,000 Bts/second
SLIDE 57 Pigeon holes
Space complexity in O(max-min) Restore up to 32 values at a
time
600,000 Bts/second
Similar space complexity Restore any number of values in
900,000 Bts/second However, operations are much
slower on the list (interval reasoning, set operations)
SLIDE 58 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Nested predicates
- GAC valid v allowed
- Search
- Code Optimization
- Competition
SLIDE 59 Constraint Propagation
- Variable/Constraint Queue
- Specific Propagators
- Nested Predicates
- Generic AC algorithms
- Binary: AC3Bitset
- Tight: GAC2001Allowed
- Loose: GAC3rValid
Pruning:
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 Nested Predicates: Decomposition
Example: open-shop scheduling:
Or < > + +
64 85 X1 X2 X1 X2
SLIDE 62 Nested Predicates: Decomposition
Example: open-shop scheduling:
Or < > +
64 X2 Y1 Y1 = X1 + 85 X1 X2
SLIDE 63 Nested Predicates: Decomposition
Example: open-shop scheduling:
Or > +
64 Y2 Y1 = X1 + 85 Y2 = (Y1 < X2) X1 X2
SLIDE 64 Nested Predicates: Decomposition
Example: open-shop scheduling:
Or >
Y2 Y3 Y1 = X1 + 85 Y3 = X2 + 64 Y2 = (Y1 < X2) X1
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
Nested Predicates: Decomposition
Example: open-shop scheduling:
Y1 = X1 + 85 Y3 = X2 + 64 Y2 = (Y1 < X2) Y4 = (Y3 < X1) (Y2 ∨ Y4)
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 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 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
Golomb ruler / FAPP
Instance: Decomposition GAC-Checker
SLIDE 71
Golomb ruler / FAPP
Instance: Decomposition GAC-Checker Golomb Ruler 128 nodes 0.18 seconds 87 nodes 38.22 seconds
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
Making the Right Choice
SLIDE 74
Making the Right Choice
Feature GAC Decomp OSP
SLIDE 75 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary
SLIDE 76 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary Ratio node/leaves
+
SLIDE 77 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary Ratio node/leaves
+
Domain continuity
no holes
SLIDE 78 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary Ratio node/leaves
+
Domain continuity
no holes Cartesian product cardinality
>60,000
SLIDE 79 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary Ratio node/leaves
+
Domain continuity
no holes Cartesian product cardinality
>60,000 Boolean domains
no
SLIDE 80 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary Ratio node/leaves
+
Domain continuity
no holes Cartesian product cardinality
>60,000 Boolean domains
no Total number of constraints
+
SLIDE 81 Making the Right Choice
Feature GAC Decomp OSP Constraint Arity
binary Ratio node/leaves
+
Domain continuity
no holes Cartesian product cardinality
>60,000 Boolean domains
no Total number of constraints
+
Decomposition!
SLIDE 82 GAC Allowed v. GAC Valid
Backtracks/second
Tighter Looser
SLIDE 83 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Search
- Heuristics
- Code Optimization
- Competition
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 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 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Search
- Code Optimization
- Competition
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
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
Values Iteration (20%)
SLIDE 90 Values Iteration (20%)
SLIDE 91 Values Iteration (20%)
- Random binary CSP
- Domain as a Bitset:
- 6,500 Bts/second
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 Road-map
- Goal
- Blueprint
- Data Structures
- Propagation
- Search
- Code Optimization
- Competition
SLIDE 94 Quick Comparison
(#instances)
Abscon Choco Mistral
SLIDE 95 Quick Comparison
(#instances)
30% 50% 70% 90% BIN-EXT BIN-INT N-EXT N-INT GLOBAL ALL
Abscon Choco Mistral
SLIDE 96 Quick Comparison
(#instances)
30% 50% 70% 90% BIN-EXT BIN-INT N-EXT N-INT GLOBAL ALL
Abscon Choco Mistral
SLIDE 97 Quick Comparison
(cpu time)
Abscon Choco Mistral
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 Quick Comparison
(cpu time)
20s 60s 100s 140s 180s BIN-EXT BIN-INT N-EXT N-INT GLOBAL
Abscon Choco Mistral
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 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
Conclusion
SLIDE 103 Conclusion
- Implementing a constraint
solver may appear like a daunting task
SLIDE 104 Conclusion
- Implementing a constraint
solver may appear like a daunting task
SLIDE 105 Conclusion
- Implementing a constraint
solver may appear like a daunting task
- It is so.
- But you’ll learn a lot!
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 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 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 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