Slides - Constraint Programming
Slides - Constraint Programming
Slides - Constraint Programming
Justin Pearson
Uppsala University
Outline
2 Introduction to MiniZinc
5 Final Thoughts
Slogan of CP
Constraint Program = Model [ + Search ]
CP provides:
high level declarative modelling abstractions,
a framework to separate search from from modelling.
Search proceeds by intelligent backtracking with intelligent
inference called propagation that is guided by high-level
modelling constructs called global constraints.
Outline
2 Introduction to MiniZinc
5 Final Thoughts
Constraint-Based Modelling
Example (Port tasting, PT)
Alva Dan Eva Jim Leo Mia Ulla
2011
2003
2000
1994
1977
1970
1966
Constraints to be satisfied:
Equal jury size: Every wine is evaluated by 3 judges.
Equal drinking load: Every judge evaluates 3 wines.
Fairness: Every port pair has 1 judge in common.
Constraint-Based Modelling
Example (Port tasting, PT)
Alva Dan Eva Jim Leo Mia Ulla
2011 3 3 3
2003 3 3 3
2000 3 3 3
1994 3 3 3
1977 3 3 3
1970 3 3 3
1966 3 3 3
Constraints to be satisfied:
Equal jury size: Every wine is evaluated by 3 judges.
Equal drinking load: Every judge evaluates 3 wines.
Fairness: Every port pair has 1 judge in common.
Constraint-Based Modelling
Example (Port tasting, PT)
Alva Dan Eva Jim Leo Mia Ulla
2011 3 3 3
2003 3 3 3
2000 3 3 3
1994 3 3 3
1977 3 3 3
1970 3 3 3
1966 3 3 3
Constraints to be satisfied:
Equal jury size: Every port is evaluated by 3 judges.
Equal drinking load: Every judge evaluates 3 wines.
Fairness: Every port pair has 1 judge in common.
Constraint-Based Modelling
Example (Port tasting, PT)
Alva Dan Eva Jim Leo Mia Ulla
2011 1 1 1 0 0 0 0
2003 1 0 0 1 1 0 0
2000 1 0 0 0 0 1 1
1994 0 1 0 1 0 1 0
1977 0 1 0 0 1 0 1
1970 0 0 1 1 0 0 1
1966 0 0 1 0 1 1 0
Constraints to be satisfied:
Equal jury size: Every port is evaluated by 3 judges.
Equal drinking load: Every judge evaluates 3 wines.
Fairness: Every port pair has 1 judge in common.
Constraints to be satisfied:
Equal jury size: Every port is evaluated by 3 judges.
Equal drinking load: Every judge evaluates 3 wines.
Fairness: Every port pair has 1 judge in common.
6 1 4 5
8 3 5 6
2 1
8 4 7 6
6 3
7 9 1 4
5 2
7 2 6 9
4 5 8 7
Global Constraints
A LL D IFFERENT([a, b, c, d ]) (1)
n(n1)
Modelling: (1) is equivalent to 2 binary constraints:
a 6= b a 6= c a 6= d b 6= c b 6= d c 6= d (2)
A LL D IFFERENT([a, b, c, d ]) (1)
n(n1)
Modelling: (1) is equivalent to 2 binary constraints:
a 6= b a 6= c a 6= d b 6= c b 6= d c 6= d (2)
A LL D IFFERENT([a, b, c, d ]) (1)
n(n1)
Modelling: (1) is equivalent to 2 binary constraints:
a 6= b a 6= c a 6= d b 6= c b 6= d c 6= d (2)
A LL D IFFERENT([a, b, c, d ]) (1)
n(n1)
Modelling: (1) is equivalent to 2 binary constraints:
a 6= b a 6= c a 6= d b 6= c b 6= d c 6= d (2)
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
u {0, 1} u 0
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
u {0, 1} u 0
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
v {1, 2} v 1
w {0, 2} w 2
x {1, 3} x 3
y {2, 3, 4, 5} y 4
z {5, 6} z 5
Propagation: etc.
Heuristics
Line 3: variable ordering heuristic: smallest domain . . .
Lines 4 & 5: value & guess ordering heuristic: max, . . .
Tree exploration: depth-first, . . . , with backtracking
History of CP
Stand-alone languages:
ALICE by Jean-Louis Lauriere, France, 1976
CHIP at ECRC, Germany, 1987 1990,
then marketed by Cosytec, France
OPL, by P. Van Hentenryck, USA, and ILOG, France:
front-end to both ILOG CP Optimizer and ILOG CPLEX
Comet, by P. Van Hentenryck and L. Michel, USA
MiniZinc, by the ORG group at CSIRO/NICTA, Australia
Libraries (the ones listed before ; are open-source):
Prolog: ECLiPSe, . . . ; SICStus Prolog, . . .
C++: Gecode, Google or-tools; IBM CP Optimizer, CHIP
Java: Choco, Google or-tools, JaCoP; . . .
Scala: OscaR; . . .
Outline
2 Introduction to MiniZinc
Features
Backends
Internals
5 Final Thoughts
Overview
http://www.minizinc.org
MiniZinc Features
Declarative language
High-level syntax
Solving technology independent
Solver independent
Separation of model and data
Open-source toolchain
Library of many predefined constraint predicates
Support for user-defined predicates and functions
Support for annotations
MiniZinc Backends
MiniZinc Backends
MiniZinc Backends
MiniZinc Backends
MiniZinc Backends
MiniZinc Backends
MiniZinc Backends
MiniZinc Backends
Two phases:
1 Compile (or flatten) the model into a FlatZinc model.
2 Interpret the FlatZinc model with a solver.
Flattening
Unroll all quantifiers and array comprehensions
Inline all function and predicate calls
Evaluate all expressions not depending on the value of a
variable
Replace unsupported variable types
Decompose complex expressions,
introducing new variables
Example: A LL D IFFERENT
We have all ready seen that of a CP solver it is better to write:
Instead of writing:
constraint alldifferent(x);
instead of:
constraint forall(i in 1..n, j in i+1..n)
(x[i] != x[j]);
Decompositions of A LL D IFFERENT
Default Decomposition
predicate alldifferent(array[int] of var int: x) =
forall(i,j in index_set(x) where i < j)
( x[i] != x[j] );
Used, e.g., by the SMT backend fzn2smt
Built-in Predicate
predicate alldifferent(array[int] of var int: x);
Most CP solvers use a dedicated propagator.
Decompositions
[...]
Decompositions
Decompositions
Decompositions
Decompositions
Decompositions
Tool Support
Modelling Extensions
Solving Extensions
Outline
1 Introduction to Constraint Programming (CP)
2 Introduction to MiniZinc
5 Final Thoughts
Uppsala University Justin Pearson Constraint Programming
CP MiniZinc Strings Other Final
Decision Variables
Constraints
Some of the constraints that we have considered, sj are string
variables, cj are character variables and ij are integer variables.
Bounded-length Strings
New data-types in CP
Bounded-length Strings in CP
bounded-length sequence representation
Complications
undefinedness: empty variable domains failure!
representation invariant: implemented as an additional
constraint
h, i-consistency
translate
translate
bounds(Z) consistency
domain consistency
data structure
candidate lengths N: range sequence, bitset, interval
candidate characters N: range sequence, bitset, interval
sequence: array, list, list of arrays, etc
propagation events
representation invariant: many promising looking event
systems are not monotonic
data structure
candidate lengths N: range sequence, bitset, interval
candidate characters N: range sequence, bitset, interval
sequence: array, list, list of arrays, etc
propagation events
representation invariant: many promising looking event
systems are not monotonic
Results
Three CP-based methods investigated
padded
fixed-length, pessimistically large array of integer variables
multiple occurrences of a padding character allowed at end
aggregate
native
2 benchmark sets from software verification:
Kaluza
Norn
even the padded method beats Kaluza [He et al @ CP 13]
Results
Aggregate implementation: 3 to 4 times speed-up over
padded
Native implementation: roughly 10 times speed-up over
padded
Outline
2 Introduction to MiniZinc
5 Final Thoughts
Final Thoughts
Thank you
Questions?
References
To read about Strings and Galois Connections see:
J. Scott. Other Things Besides Number: Abstraction,
Constraint Propagation, and String Variable Types.
PhD thesis, Department of Information Technology,
Uppsala University, Sweden, March 2016.
http: // urn. kb. se/ resolve? urn= urn: nbn: se:
uu: diva-273311
References