H - PowerPoint PPT Presentation

About This Presentation
Title:

H

Description:

What I (still) like about Constraint Programming H kan Kjellerstrand (hakank_at_gmail.com) Independent Researcher, Malm http://www.hakank.org/ My Constraint ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 45
Provided by: H500
Learn more at: http://www.hakank.org
Category:
Tags:

less

Transcript and Presenter's Notes

Title: H


1
What I (still) like about Constraint Programming
Håkan Kjellerstrand (hakank_at_gmail.com) Independent
Researcher, Malmö http//www.hakank.org/ My
Constraint Programming Blog http//www.hakank.org
/constraint_programming_blog/ This talk at
SweConsNet 20130527 http//www.hakank.org/constra
int_programming/sweconsnet_talk_20130527.ppt
2
Some more about me
Not a theory guy, much more a modeling guy I
like to solve a good puzzle with CP. I don't
do CP professionally which might explain some
things... Co-organizer of the CP 2013 Workshop
(in Uppsala, September) CP Solvers Modeling,
Applications, Integration, and Standardization
http//cp2013.a4cp.org/workshops/cpsolvers
Organizers Jacob Feldman, Helmut Simonis, and
me Constraint Solvers Catalog
http//openjvm.jvmhost.net/CPSolvers/
3
Tested 24 CP Systems
- Choco (21 models)
- Comet (168 models) - ECLiPSe CLP (177
models) - Gecode (165
models) - Gecode/R (29 models)
- JaCoP (18 models) - JaCoP/Scala
(39 models) - MiniZinc
(1031 models) - SICStus Prolog (152 models)
- Essence'/Tailor (26 models) -
Essence'/Savile Row (56 models) -
Zinc (39 models) - Google or-tools/Python (202
models) - Google or-tools/Java (36
models) - Google or-tools/C (129 models)
- OscaR (Scala in OR) (146 models) -
Java JSR-331 (42 models)
- Numberjack (52 models) - AIMMSCP (39 models)
- B-Prolog (207
models) - Choco3 (90 models)
- AMPL (100 "pure" CP models) -
ILOG CP Optimizer OPL (as we speak, gt 100
models) - Answer Set Programming ("related
paradigm" 87 encodings) In total gt 3200
models. Note Not all of these are pure CP
models, some are plain IP models.
4
Common Constraint Programming Problems
http//hakank.org/common_cp_models/ I always
start testing a CP system with a number of
standard "learning" problems to get a feel for
different constructs in the CP system.
Also, I always report bugs and whine about things
I don't like (or is inconvenient/weird/etc) to
the developers. Forthcoming (perhaps) -
Picat, http//picat-lang.org/ (available
20130531) - JaCoP v 4.0 - Gecode-python,
https//launchpad.net/gecode-python (Python
inteface to Gecode) - Copris,
http//bach.istc.kobe-u.ac.jp/copris/ (Scala)
- Clojure core.logic, https//github.com/clojure/c
ore.logic - or-tools/C (no syntactic sugar
at all...) - new MiniZinc solvers Other
suggestions?
5
So, what do I (still) like about Constraint
Programming?
In short The modeling part, the ease of
modeling many types of problems. Some CP
features - element - reification - generating
1/N/all solution(s) - global constraints -
reversibility/bidirection - nonlinear
constraints - symmetry breaking - declarative
(high level) - nifty language features (wish
list) Many of these features was what caught my
interest (blew my mind) in 2008 after checking
out mathematical programming some year earlier.
Hence the still.
6
Element constraint
The signum of Constraint Programming. Good
example (IMHO) xy z MiniZinc,
similar Essence' cp.add(x(y) z, Strong)
// OscaR (Scala) OK rel(this, z
element(x, y) // Gecode Not so good
solver.addConstraint( solver.makeEquality(z,
solver.makeElement(x, y).var())) //
or-tools/Java Yes, I punish certain host
languages (e.g. Java) that don't have operator
overloading. AMPL can simulate element with
exists. Here 1..n is the domain of y. s.t.
c1 existsj in 1..n j y and
xj z
7
Reification
alldifferent_except_0 Implementing a
decomposition of this constraint is often a good
proxy of the "syntactical sugarness" of a CP
system. It's one of the first things I test.
good example MiniZinc, Zinc, Comet, Essence'
MiniZinc version forall(i, j in 1..length(x)
where i lt j) ( (xi ! 0 /\ xj ! 0) -gt
xi ! xj ) good example Gecode
for(int i 0 i lt x.size() i) for(int
j i1 j lt x.size() j)
rel(space, (xi ! 0 xj ! 0)
gtgt (xi ! xj), Icl)

8
Reification Choco3 (beta)
// only half-reification (might be easier in
fortcoming versions) BoolVar b
VariableFactory.boolArray("b_"i"_"j, 3,
solver) solver.post(IntConstraintFactory.implies(
b0, (IntConstraintFactory.arithm(v
i, "!", c)))) solver.post(IntConstraintFactory
.implies(VariableFactory.not(b0),
(IntConstraintFactory.arithm(vi, "",
c)))) solver.post(IntConstraintFactory.implies(b
1, (IntConstraintFactory.arithm(vj
, "!", c)))) solver.post(IntConstraintFactory.i
mplies(VariableFactory.not(b1),
(IntConstraintFactory.arithm(vj, "",
c)))) solver.post(IntConstraintFactory.implies(b
2, (IntConstraintFactory.arithm(vi
, "!", vj)))) solver.post(IntConstraintFactor
y.implies(VariableFactory.not(b2),
(IntConstraintFactory.arithm(vi, "",
vj)))) ALogicTree t Node.implies(Node.and(Lit
eral.pos(b0),
Literal.pos(b1)),
Literal.pos(b2)
) solver.post(IntConstraintFactory.c
lauses(t, solver))
9
Generating 1/N/all solution(s)
Debugging Example 8-queens should have 92
solutions, otherwise the model is wrong. Or -
much rarer - the solver is wrong. Later This
assumes that no symmetry breaking is used.
(Thanks Mats Carlsson.) I use this very much.
In the current AMPLGecode implementation this
is not implemented which was quite strenuous..
Ensure unicity of a solution, e.g. a Sudoku
problem (set the numbet of solution to 2 and
expect just 1) Certain combinatorial problems
is about counting the number of solutions.
Generating problem instances This is - kind of
- the reverse of solving a problem, by letting
all decision variable be free. E.g. "Drive Ya
Nuts". Special case Search for optimal value
and then generate all solutions with that value.
No CP system has this as built-in. Hint, hint -)
10
Global constraints
Global Constraint Catalog (364 listed global
constraints) http//www.emn.fr/z-info/sdemasse/gcc
at/index.html Advantages Special tailored
propagators This is the usual sale pitch for
global constraints. For me the modeling part
High level concepts as "patterns" in
modeling Some personal favorites
all_different
all_different_except_0 element (
xy z)
global_cardinality_count decrease/increase
(sortedness) regular (finite
state machine) cumulative (for scheduling like
problems) circuit (Hamiltonian circuit) table
(allowed assignments)
inverse (useful in some puzzles )
http//hakank.org/minizinc/global (170
decompositions in MiniZinc, more or less general)
11
Reversibility (bi-direction)
Simple example convert a number ("num") to/from
its digits ("x"). // MiniZinc (general
predicate, base 10) predicate toNum(arrayint of
var int a, var int n) let int len
length(a) in n sum(i in 1..len) (
pow(10,len-i) alen-i1 ) var
0..999 num array1..3 of var 0..9
x constraint toNum(x, num) /\ num
2 1 /\ constraint on num is an odd number
x2 gt 5 constraint of digits second digits
gt 5 - more general channelling/dual model
12
Nonlinear constraints
Compared to (traditional) IP modeling, there is
much less need to reformulate/linearize
nonlinear constraints. There is no need to
remember which IP solver it is that handle that
nonlinear constraints (quadratic etc). No big
M's! Though we want as small domains as
possible.
13
Symmetry breaking
Pruning the search tree. Global constraints -
increasing/decreasing - lex family -
precedence Some system supports dynamic symmetry
breaking, e.g. Chris Mears' Lightweight Dynamic
Symmetry Breaking (LDSB) http//www.cmears.id.au/s
ymmetry/ Supported by - ECLiPSe CLP -
Gecode ECLiPSe CLP also has support for -
Symmetry Breaking During Search (SBDS) -
GAP-based Symmetry Breaking via Dominance
Detection (SBDD)
14
Declarative, high level
Note I'm not sure how to define "declarative",
but I know it when I see it. -) http//en.wikipe
dia.org/wiki/Declarative_programming The
introduction In computer science, declarative
programming is a programming paradigm that
expresses the logic of a computation without
describing its control flow. Many languages
applying this style attempt to minimize or
eliminate side effects by describing what the
program should accomplish, rather than
describing how to go about accomplishing it (the
how is left up to the language's
implementation). This is in contrast with
imperative programming, in which algorithms
are implemented in terms of explicit steps.
Sometimes it's hard claiming declarativness of CP
modeling when the code is full of for(all)
loops. Though, I still insist that it's
declarative... High level The higher, the
better (IMHO), at least for prototyping.
15
What I (still) don't like about CP
- Can be hard to debug "Everything happens at
once" is brilliant, but can also be hard to
debug. I tend to rely on printf-debuggingand
removing all constraints and then put them back
one after another (or testing after adding each
single constraint). - For more complex problems
must use search heuristics (or remodel)
Getting the heuristics right is (still) an art,
not a science. There are some Black Box
solvers/heuristics, but - IMHO - they need to
be tweaked as well. Examples - Gecode (v
4.0) AFC variants, Activity-Based - Choco
3 Activity-Based, Impact-Based - or-tools
Impact-Based - ILOG CP Optimizer recommends
to use no labeling at all first Question
Is black box solvers a realistic/interesting/feas
ible goal for CP research?
16
Nifty language features I
Some of these features are not unique to CP, but
are very handy. Many are just syntactic sugar. -
syntactic sugar is everything (hakank) If you
want to make me happy, implement as many of these
as possible. -) Some may be possible only
for new dedicated CP languages or DSL. - element
as xy z where x, y, and z (or at least y)
are decision variables - general matrix
element Gecode and Choco2 has some support for
this
17
Nifty language features II
  • - exists
  • Supported by MiniZinc and AMPLCP.
  • Useful when the range in a forall loop is
    dynamic
  • or instead of declaring a temporary decision
    variable (might give
  • worse propagation).
  • Small example (MiniZinc)
  • z is a decision variable used elsewhere
  • exists(i in 1..n) (
  • z i /\
  • forall(j in i1..n) ( xj 0)
  • )
  • - array/set comprehension
  • MiniZinc
  • forall(i in 1..n) (
  • alldifferent(xi,j j in 1..n where
    costi,j gt 0)
  • )

18
Nifty language features III
- external loops Which all host languages
have (except some Prolog's). This is one of
the feature I miss most in MiniZinc, i.e. the
possibilty to do simple for loops, e.g. to
create temporary variables, counting etc. -
if-then-else on decision variables as well as
non-decision variables Example AIMMS and
(sometimes) AMPL. In most other CP system one
have to use reifications or dedicated methods
like ifThenElse(condition,thenclause,
elseclause) or MiniZinc (condition ?
then clause) /\ (not(condition) ? else
clause) Choco3 - current beta version - only
support half-reification which is a nuisance.
(See earlier slide.)
19
Nifty language features IV
- predicates (functions) Is surprisingly -
not supported in some of the high level systems
such as AMPL, OPL and Essence'. - set
variables Sometimes extremely useful. (I
definitely respect the complexity of this.) -
Systematically testing all variable and value
heuristics It would be nice to have a simple
way of systematically testing all variable
value heuristics on a model, e.g. with a simple
flag. I haven't seen this in any system yet,
and in some it's quite easy to implement.

20
Thank you!
- Questions? - Comments? Hakan
Kjellerstrand (hakank_at_gmail.com) http//www.hakank
.org/
21
CP bloggers
There are few CP bloggers (tweeters etc) compared
to the OR bloggers. - Jean-Charles Regin/Pierre
Schaus "CP is fun" http//cp-is-fun.blogspot.c
om/ - Jacob Feldman "CP Standardization Blog"
http//cpstandard.wordpress.com/ - Helmut
Simonis "CP Applications Blog"
http//hsimonis.wordpress.com/ - Hakan
Kjellerstrand My Constraint Programming Blog
http//www.hakank.org/constraint_programming_blog/
Some OR people that sometimes blog about CP.
(See next slide.)
22
OR people that sometimes blog about CP
Here are some great OR-bloggers that sometimes
mention CP - Mike Trick "Michael Tricks
Operations Research Blog" http//mat.gsia.cmu.ed
u/blog/ - Jean-Francois Puget "IT Best Kept
Secret Is Optimization" https//www.ibm.com/dev
eloperworks/community/blogs/jfp/?langen - Erwin
Kalvelagen "Yet Another Math Programming
Consultant" http//yetanothermathprogrammingcons
ultant.blogspot.com/ - Paul Rubin OR in an OB
World http//orinanobworld.blogspot.com
23
all_different_except_0 decomposition in
different CP systems
Implementation of all_different_except_0 in
different CP systems - proxy for ease of
modelling - overloading of operators (if
possible) - logical operators -
reification Note It's not the only way to
encode this constraint.
24
all_different_except_0 decomposition
G12 MiniZinc forall(i, j in 1..length(x) where i
lt 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)
25
all_different_except_0 decomposition
Choco2 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)?
)
26
all_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)? )?
)
27
all_different_except_0 decomposition
JaCoP/Scala for(i lt- 0 until y.length j lt- 0
until i) val b new BoolVar("b") b ltgt
AND((y(i) \ 0), (y(j) \ 0)) b -gt (y(i)
\ y(j))
28
all_different_except_0 decomposition
Gecode for(int i 0 i lt x.size() i)
for(int j i1 j lt x.size() j)
rel(space, (xi ! 0 xj ! 0) gtgt
(xi ! xj), icl)
29
all_different_except_0 decomposition
Gecode/R 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
30
all_different_except_0 decomposition
ECLiPSe CLP alldifferent_except_0(Xs) -
dim(Xs, Len), labeling(Xs), (
for(I, 1, Len) for(J, 1, Len), param(Xs) do
( I \ J, XsI \ 0, XsJ \ 0 )?
-gt XsI \ XsJ
true ).
31
all_different_except_0 decomposition
SICStus Prolog alldifferent_except_0(Xs) -
( foreach(X,Xs) do indomain(X)), (
foreach(XI,Xs), count(I,1,_),
param(Xs) do ( foreach(XJ,Xs),
count(J,1,_), param(I,XI) do
I lt J, XI \0, XJ \0 -gt
XI \ XJ true
) ).
32
all_different_except_0 decomposition
Essence' forall i,j int(1..n) . ( (i ! j)
gt (((xi ! 0) /\ (xj ! 0)) gt (xi !
xj))? ),
33
all_different_except_0 decomposition
G12 Zinc forall(i,j in index_set(x) where i !
j) ( (xi gt 0 /\ xj gt 0) -gt xi ! xj
)
34
all_different_except_0 decomposition
or-tools/Python n len(a) for i in range(n)
for j in range(i) s.Add((ai ! 0) (aj
! 0) lt (ai ! aj))
35
all_different_except_0 decomposition
or-tools/Java int n a.length for(int i 0 i
lt n i) for(int j 0 j lt i j)
IntVar bi s.makeIsDifferentCstVar(ai, 0)
IntVar bj s.makeIsDifferentCstVar(aj, 0)
IntVar bij s.makeIsDifferentCstVar(ai,
aj) solver.addConstraint(
s.makeLessOrEqual( s.makeProd(bi,
bj).var(), bij))
36
all_different_except_0 decomposition
or-tools/C int n a.Length for(int i 0 i lt
n i) for(int j 0 j lt i j)
s.Add((ai ! 0) (aj ! 0) lt (ai !
aj))
37
all_different_except_0 decomposition
AIMMSCP CONSTRAINT identifier
CAllDifferentExcept0 index domain (i,j) i
lt j definition if (x(i) ltgt 0 and x(j) ltgt
0) then x(i) ltgt x(j)
endif
38
all_different_except_0 decomposition
AMPLCP s.t. alldifferent_except0i in dom,j in
dom i lt j (xi gt 0 xj gt 0) gt
(xi ! xj)
39
all_different_except_0 decomposition
B-Prolog alldifferent_except_0(Xs) - Len _at_
Xslength, foreach(I in 1..Len, J in 1..Len,
(I \J /\ XsI \ 0 /\ XsJ \
0) gt (XsI \
XsJ)).
40
all_different_except_0 decomposition
Choco 3 (beta version) BoolVar b
VariableFactory.boolArray("b_"i"_"j, 3,
solver) solver.post(IntConstraintFactory.implies(
b0, (IntConstraintFactory.arithm(v
i, "!", c)))) solver.post(IntConstraintFactory
.implies(VariableFactory.not(b0),
(IntConstraintFactory.arithm(vi, "",
c)))) solver.post(IntConstraintFactory.implies(b
1, (IntConstraintFactory.arithm(vj
, "!", c)))) solver.post(IntConstraintFactory.i
mplies(VariableFactory.not(b1),
(IntConstraintFactory.arithm(vj, "",
c)))) solver.post(IntConstraintFactory.implies(b
2, (IntConstraintFactory.arithm(vi
, "!", vj)))) solver.post(IntConstraintFactor
y.implies(VariableFactory.not(b2),
(IntConstraintFactory.arithm(vi, "",
vj)))) ALogicTree t Node.implies(Node.and(Lit
eral.pos(b0),
Literal.pos(b1)),
Literal.pos(b2)
) solver.post(IntConstraintFactory.c
lauses(t, solver))
41
all_different_except_0 decomposition
Numberjack return ( ((a ! 0) (b ! 0)) lt
(a ! b ) ) for a, b in
pair_of(x)
42
all_different_except_0 decomposition
OscaR (Scala) for(i lt- 0 until y.length j lt- 0
until i) cp.add( ((y(i) ! 0) (y(j) !
0)) gt (y(i) ! y(j))
)
43
all_different_except_0 decomposition
JSR-331 for(int i 0 i lt v.length i)
for(int j i1 j lt v.length j)
Constraint c1 p.linear(vi,"!", 0)
Constraint c2 p.linear(vj,"!", 0)
Constraint c3 p.linear(vi,"!", vj)
p.postIfThen(c1.and(c2), c3)
44
all_different_except_0 decomposition
Answer Set Programming (related
paradigm) const n 6. const m
9. values(0..m). ix(1..n). unique indices of
x, 1..n 1 x(I, Val) values(Val) 1 -
ix(I). alldifferent except 0 If Val gt 0
then there must be 0..1 occurrences of Val in
x. x(I, Val) ix(I) 1 - values(Val), Val gt
0.
Write a Comment
User Comments (0)
About PowerShow.com