Title: H
1Learning Constraint Programming Some lessons
learned
Håkan Kjellerstrand Site
http//www.hakank.org/ Blogs My Constraint
Programming Blog
http//www.hakank.org/constraint_programming_blog/
hakank.blogg http//www.hakank.org/webblogg/
(Swedish, since 2003)?
2Constraint programming systems
MiniZinc http//www.hakank.org/minizinc/ gt
550 models Choco http//www.hakank.org/choc
o/ 17 models JaCoP
http//www.hakank.org/JaCoP/ 17
models Gecode/R http//www.hakank.org/gecode_r/
27 models Comet http//www.hakank.org/
comet/ 140 models Gecode
http//www.hakank.org/gecode/ 40 models
3Overview
- Presentation of myself and my interest in
constraint programming - "Learning models" -
The systems pro/cons - Questions
4Håkan Kjellerstrand
- software developer at Bokus (http//www.bokus.co
m), Swedish on-line book store.
1997-2001, 2005-today. - software developer since
1996, mostly web/internet/dot.com - technical
writer and support, Swedish word processor Cicero
1987 1994 - education political
science (public administration), philosophy and
computer science
5Constraint Programming
- private interest - CLP in 1998 and later 2001
(various Prolog systems, e.g. SICStus, ECLiPSe).
Least diff problem minimize ABCDE - FGHIJ (all
letters distinct)?
50123 - 49876 247 - 2007 autumn
mathematical programming, operations research,
AMPL, Xpress, Excel when modelling
SENDMOREMONEY, Diet etc missed
"alldifferent" and other constraints from
CP/CLP - 2008 February Found MiniZinc - late
2008 Started My Constraint Programming Blog
6About 15 learning models
- SENDMOREMONEY / n-queens running a model,
what to expect in output - Least Diff minimize
the difference ABCDE - FGHIJ (digits all
distinct)? - Diet how to interact with integer
arrays and variable arrays - Seseman generate
one or all solutions, handling of matrices -
Coins grid Tony Hubermann's grid puzzle,
minimize distances, MIP - Simple map colouring
using graph/matrix, optimization - de Bruijn
sequence a personal favourite, command line
options - alldifferent_except_0 (decomposition
of a) global constraint, logical operators -
Furniture Moving scheduling, cumulative -
Minesweeper more advanced example, problems from
a file. - Quasi group Completion alldifferent on
rows/columns, matrices - Young Tableaux and
partitions combinatorial problem - Send Most
Money in any base first optimize, then
generating all solutions - Nonogram search
strategies, regular or "real" regular
expressions - xkcd simple problem, knapsack /
set covering - Crosswords simple (from Apt etc),
strings/chars, non-trivial Element - Word square
another non-trivial Element, how to read a file
(word list)? - Missing set variables,
extensional / table, propagators, ...
7Comparison of the systems, for learning a CP
system
Focus on ease of modelling, syntax (example
Element for integer variable matrices)?
documentation, site, introduction material
number of examples number of built in
constraints (can be hard to count)? active
community mailing list, forum, Wiki
input/output, command line options build in
search strategies other features
8MiniZinc
- http//www.g12.cs.mu.oz.au/minizinc/ -
http//www.hakank.org/minizinc/ - Version 1.0
last week - FlatZinc, many solvers
distribution, Gecode Gist, ECLiPSe, SICStus
Prolog - ease of modelling high level, Element
mi,j, refications, logical operators -
documentation specification, not much else -
examples many examples benchmarks (2 are
mine), many test cases - constraints many
(decompositions and solver dependent)? -
community not active - input/output no support
except data file - search strategies fixed
(ECLiPSe's), cannot write own - other features
set vars, float vars (solver dependent)?
- typed, e.g. bool2int, int2float (Mercury)? -
cannot write own propagators in MiniZinc
9JaCoP
- http//jacop.osolpro.com/ - http//www.hakank.or
g/JaCoP/ - version 2.3 - ease of modelling
Java, Element can be tricky, "minimalistic"
approach - documentation not much, API from
source code - examples many (6 are mine)? -
constraints many - community Wiki, not active -
input/output Java - search strategies yes -
other features great class structure (find
easy)? XCSP (reading a writing)?
regular expressions - no float vars, no set
vars
10Choco
- http//www.emn.fr/x-info/choco-solver/doku.php -
http//www.hakank.org/choco/ - version 2.1 -
ease of modelling Java, Element can be tricky -
documentation great site, introduction
material - examples many (but undocumented)? -
constraints many, nicely documented with example
code - community active - input/output Java -
search strategies yes - other features
XCSP FlatZinc (don't work?)? - class
structure cluttered (hard to find
constraints)?
11Comet
- http//dynadec.com/ - http//www.hakank.org/come
t/ - version 1.2 - many solvers Finite domain,
LP, MIP, Local search (slightly different
syntax)? - ease of modelling High level, very
OPL like, Element mi,j, reification,
boolean operators, C like objects etc
(undocumented)? - documentation not much, API in
distribution (but OPL book, Comet book), -
examples not many, just 9 FD, 5 LP, 3 LS -
constraints many - community very active -
input/output C like - search strategies ease
of writing own (not good documented)? - other
features GUI (for local search)?
debugger text and GUI scheduling (OPL
like)? - no set vars for FD (in version 1.3?)?
- license commercial, academic or evaluation
12Gecode
- http//www.gecode.org/ - http//www.hakank.org/g
ecode/ - version 3.1 - ease of modelling C,
Element tricky, logical operators overloaded -
documentation great site (searchable), great
introduction - examples many (with bells
whistles, can be hard to read)? - constraints
many (overridden with same name)? - community
active - input/output C, great command line
options - search strategies many different built
in - other features Gist (interactive search
tree)? FlatZinc regular expressions -
matrix of variable integers as arrays - gotcha
Matrix view of integers m(cols, rows)?
13Gecode/R
- http//gecoder.rubyforge.org/ -
http//www.hakank.org/gecode_r/ - version For
Gecode 2.2 (soon version 3.0)? - ease of
modelling Ruby, nice short cuts - documentation
well structured site, API, introduction -
examples not many (12, 3 are mine)? -
constraints not many, some missing from Gecode
(e.g. gcc)? - community not very active -
input/output Ruby - search strategies some of
Gecode's - other features Ruby's ease of
handling arrays regular expressions -
matrices can be tricky - reifications can be
tricky - error messages can be confusing
14Comparison of the systems, subjective feature
matrix
Range 1..5 where 5 is very good, 1 is not good
(or N/A). C Feature to come.
MiniZinc Comet Choco JaCoP Gecode
Gecode/R - ease of modelling 5 5
3 3 4 4 - documentation, site 3
3 4 3 4 3 - num.
examples 4 2 3 4 4
3 - num. constraints 3 4 4
4 4 3 - active community 2
4 4 2 4 3 -
input/output 2 3 4 4
5 4 - command line option 2 3
3 3 5 3 - propagators etc
1 4 4 4 4 2 -
regular expressions 1 1 4 4
5 4 - Element 5 5
4 3 3 3 - set var
5 1 (C) 5 1 5 4 -
float var 4 4 4 1
1 1 - debugging 2 5
3 3 4 3 - CVS/SVN
3 (ROTD) 1 5 1 5 5 -
FlatZinc 5 1 3 3
(C) 5 1? - XCSP 1
1 4 5 1 1 - open source
5 1 5 5 5
5 Note The features has not the same weights!
15Thank you!
- Questions?
Thanks to Mikael Zayenz Lagerkvist for help and
interesting discussions!
Håkan Kjellerstrand http//www.hakank.org/
16all_different_except_0 decomposition
MiniZinc forall(i,j in 1..length(x) where i !
j) ( (xi gt 0 /\ xj gt 0) -gt xi !
xj )? Comet int n x.getSize() forall(i in
1..n, j in i1..n) m.post(xi gt 0 xj gt
0 gt xi ! xj)
17all_different_except_0 decomposition
Choco for(int i 0 i lt v.length i)
for(int j i1 j lt v.length j)
m.addConstraint(ifThenElse(
and(
gt(vi, 0),
gt(vj, 0)?
),
neq(vi, vj),
TRUE
)?
)
18all_different_except_0 decomposition
JaCoP for(int i 0 i lt v.length i)
for(int j i1 j lt v.length j)
m.impose(new IfThen(
new And(
new XneqC(vi, 0),
new XneqC(vj, 0)?
),
new XneqY(vi, vj)?
)? )
19all_different_except_0 decomposition
Gecode/R count (1..self0.domain.max).eachi
self.count(i).must lt 1 global
cardinality. xgcc is an array containing the
counts to be satisfied (self0.domain.max1).time
si xgcci.must self.count(i)?
20all_different_except_0 decomposition
Gecode/R (continued)? reification n
x.length b1_is_an bool_var_matrix(n,n)? b2_is_an
bool_var_matrix(n,n)? b3_is_an bool_var_matrix(n,n
)? n.timesi n.timesj if i ! j
then xi.must_not.equal(0, reify gt
b1i,j)? xi.must_not.equal(0, reify
gt b2i,j)? xi.must_not.equal(xj,
reify gt b3i,j)? (b1i,j
b2i,j).must.imply(b3i,j)? else
b1i,j.must.true b2i,j.must.true
b3i,j.must.true end
21all_different_except_0 decomposition
Gecode for(int i 0 i lt x.size() i)
for(int j i1 j lt x.size() j)
post(space, tt( imp(
xi ! 0 xj ! 0,
xi ! xj)? )?
)
22Introduction in Constraint Programming Books
- "Finite Domain Constraint Programming in Oz. A
Tutorial" by Christian Schulte and Gert
Smolka. http//www.mozart-oz.org/home/doc/fdt/i
ndex.html - Pascal Van Hentenryck "The OPL
Optimization Programming Language" (out of
print)? - Two operations research books
Wayne Winston "Operations Research Tamdy
Taha "Operations Research"
23Case study Non trivial Element
Word square problem aalborg abaiser
lantaca bittman osamine recanes
granese Same word for i'th row and i'th column
("aalborg").
24Word square problem, problem
E 0..8, 0..8, 0..8, 0..8, 0..8,
0..8, 0..8 words aalborg
abaiser bittman gardner
granese lantaca osamine
recanes yoghurt meaning
E0E1E2E3E4E5E6 E0 E1 E2 E3 E4 E5 E6
Size of length 7 words 40230 words
25Word square problem, solution
E 0, 1, 5, 2, 6, 7, 4 words
aalborg abaiser bittman
gardner granese lantaca
osamine recanes
yoghurt meaning E0E1E2E3E4E5E6 E0 a a l
b o r g E1 a b a i s e r E2 l a n t a c a E3 b
i t t m a n E4 o s a m i n e E5 r e c a n e
s E6 g r a n e s e
26Case study Non trivial Element
Principle Find the crossings of the
words. MiniZinc forall(I, J in 0..word_len-1) (
wordsEI, J wordsEJ,I
)? Comet forall(i in 0..word_len-1)
forall(j in 0..word_len-1)
cp.post(wordsEi, j wordsEj,i)
27Case study Non trivial Element
JaCoP // Note words is a transposed word
matrix for(int i 0 i lt word_length
i) for(int j 0 j lt word_length
j) FDV tmp new FDV(store, "tmp"
i " " j, 0, dict_size)
store.impose(new Element(Ei, wordsj, tmp))
store.impose(new Element(Ej,
wordsi, tmp)) Choco
for(int I 0 I lt word_length I)
for(int J 0 J lt word_length J)
IntegerVariable tmp makeIntVar("tmp" I "
" J, 0, dict_size)
M.addConstraint(nth(EI, CJ, words, tmp))
M.addConstraint(nth(EJ, CI, words,
tmp))
28Case study Non-trivial Element
Gecode void element_offset(Space space, IntArgs
words, IntVar e, IntVar word_len,
IntVar c, IntVar res,
IntConLevel icl ICL_DOM)
element(space, words, plus(space,
// words is an
array mult(space,
// wordsEiword_lenc
e,
word_len, icl),
c, icl), res, icl) //
... for(int i 0 i lt word_len i)
for(int j 0 j lt word_len j) IntVar
tmp(this, 0, num_words)
element_offset(this, words, Ei, word_len_v,
Cj, tmp, opt.icl()) element_offset(this
, words, Ej, word_len_v, Ci, tmp,
opt.icl())
29Case study Non trivial Element
Gecode/R TBW.
30Global constraints
- alldifferent, global cardinality, cumulative,
alldifferent except 0, etc - Revelation Global
constraint catalog gt 300 global constraints
with explanation, reference, examples etc
http//www.emn.fr/x-info/sdemasse/gccat/ -
implemented about 100 global constraints in
MiniZinc e.g. alldifferent_except_0, clique,
circuit, global contiguity, period, etc. all
decompositions (i.e. no propagators)? -
conceptual level - "patterns" as shorthand
31What's next?
- propagators, global constraints - extensional /
table - read more theory and papers -
scheduling - a larger CP project? - CLP SICStus
Prolog/ECLiPSe