modelling & solving with constraints prof. toby walsh university college cork/uppsala university
TRANSCRIPT
Modelling & SolvingModelling & Solving with Constraints with Constraints
Modelling & SolvingModelling & Solving with Constraints with Constraints
Prof. Toby WalshProf. Toby Walsh
University College Cork/Uppsala UniversityUniversity College Cork/Uppsala University
Overview• Introduction to constraint programming
– Constraint propagation– Backtracking search
• Modelling case studies– Simple recipe
• Solving with constraints– Global constraints– Set variables– Branching heuristics
Resources• Course links
– www.cs.york.ac.uk/~tw/Links/csps/
• Benchmark problems– www.csplib.org
• Constraints solvers– LP based like ECLIPSE, Java based
solvers like JCL, …
Constraint programming• “Dream” of declarative programming
– State the constraints– Solver finds a solution
• Method of choice for many hard combinatorial problems– Scheduling, assignment, routing, …
Constraints are everywhere!
• No meetings before 10am
• Network traffic < 100 Gbytes/sec
• PCB width < 21cm• Salary > 45k Euros…
Constraint satisfaction• Constraint satisfaction problem (CSP) is a
triple <V,D,C> where:– V is set of variables– Each X in V has set of values, D_X
• Usually assume finite domain• {true,false}, {red,blue,green}, [0,10], …
– C is set of constraints
Goal: find assignment of values to variables to satisfy all the constraints
Example CSP• Course timetabling
– Variable for each courseCS101, OS102 ..
– Domain are possible timeswed9am, fri10am, ..
– Constraints:CS101 \= wed9amCapacity constraints: atmost(3,
[OS102,DB103..],wed9am)Lecturer constraints:
alldifferent([CS101,DB103,…])
Constraint optimization• CSP + objective function
– E.g. objective is Profit = Income - Costs
• Find assignment of vals to vars that:– Satisfies constraints– Maximizes (minimizes) objective
• Often solved as sequence of satisfaction problems
Profit > 0, Profit > Ans1, Profit > Ans2, …
Constraint programming v. Constraint logic programming
• Constraints declaratively specify problem– Logic programming natural approach
Assert constraints, call “labelling” strategy (backtracking search predicate)
• Imperative & functional toolkits• C++, Java, CAML, …
Constraints• Constraints are tuples <S,R> where
– S is the scope, [X1,X2, … Xm]• list of variables to which constraint applies
– R is relation specifying allowed values (goods)
• Subset of D_X1 x D_X2 x … x D_Xm• May be specified intensionally or extensionally
Constraints• Extensional specification
– List of goods (or for tight constraints, nogoods)
• Intensional specification– X1 =/= X2– 5*X1 + 6*X2 < X3– alldifferent([X1,X2,X3,X4]), …
Binary v non-binary• Binary constraint
– Scope covers 2 variables– E.g. not-equals constraint: X1 =/= X2.– E.g. ordering constraint: X1 < X2
• Non-binary constraint– Scope covers 3 or more variables– E.g. alldifferent(X1,X2,X3).– E.g. tour(X1,X2,X3,X4).
“Non-binary constraints” usually do not include unary constraints!
Constraint graph• Nodes = variables• Edge between 2 nodes
iff constraint between 2 associated variables– Few constraints, sparse
constraint graph– Lots of constraints,
dense constraint graph
Some non-binary examples
• Timetabling– Variables: Lecture1, Lecture2, …– Values: time1, time2, …– Constraint that lectures taught by same
lecturer do not conflict:
alldifferent(Lecture1,Lecture5,…).
Some non-binary examples
• Scheduling– Variables: Job1. Job2, …– Values: machine1, machine2, …– Constraint on number of jobs on each
machine:
atmost(2,[Job1,Job2,…],machine1),
atmost(1,[Job1,Job2,…],machine2).
Why use non-binary constraints?
• Binary constraints are NP-complete– Any non-binary constraint can be
represented using binary constraints– E.g. alldifferent(X1,X2,X3) is “equivalent”
to X1 =/= X2, X1 =/= X3, X2 =/= X3
• In theory therefore they’re not needed– But in practice, they are!
Modelling with non-binary constraints
• Benefits include:– Compact, declarative specifications
(discussed next)
– Efficient constraint propagation(discussed second)
Modelling with non-binary constraints
Consider writing your own alldifferent constraint:
alldifferent([]).
alldifferent([Head|Tail]):-
onediff(Head,Tail),
alldifferent(Tail).
onediff(El,[]).
onediff(El,[Head|Tail]):-
El #\= Head,
onediff(El,Tail).
Modelling with non-binary constraints
• It’s possible but it’s not very pleasant!
• Nor is it very compact– alldifferent([X1,…Xn]) expands into n(n-1)/2 binary not-
equals constraints, Xi \= Xj
– one non-binary constraint or O(n^2) binary constraints?
And there exist very efficient algorithms for reasoning efficiently with many specialized non-binary constraints
Constraint solvers• Two main approaches
– Systematic, tree search algorithms– Local search or repair based procedures
• Other more exotic possibilities– Hybrid algorithms– Quantum algorithms
Systematic solvers• Tree search
– Assign value to variable– Deduce values that must be removed from future/unassigned
variables• Propagation to ensure some level of consistency
– If future variable has no values, backtrack else repeat
• Number of choices– Variable to assign next, value to assign
Some important refinements like nogood learning, non-chronological backtracking, …
Local search• Repair based methods
– Generate complete assignment– Change value to some variable in a violated
constraint
• Number of choices– Violated constraint, variable within it, …
Unable to exploit powerful constraint propagation techniques
Constraint propagation• Heart of constraint programming• Most often enforce arc-consistency (AC)
– A binary constraint r(X1,X2) is AC iff for every value for X1, there is a consistent value (often
called support) for X2 and vice versa
– A problem is AC iff every constraint is AC
Enforcing arc-consistency
• X2 \= X3 is AC• X1 \= X2 is not AC
– X2=1 has no support so can this value can be pruned
• X2 \= X3 is now not AC– No support for X3=2 – This value can also be
pruned
Problem is now AC
{1}
{1,2} {2,3}
\=
\=
X1
X3X2
Enforcing arc-consistency• Remove all values that are not AC
(i.e. have no support)
• May remove support from other values (often queue based algorithm)
• Best AC algorithms (AC7, AC-2000) run in O(ed^2)– Optimal if we know nothing else about the
constraints
Properties of AC• Unique maximal AC
subproblem– Or problem is
unsatisfiable
• Enforcing AC can process constraints in any order– But order does affect
(average-case) efficiency
Non-binary constraint propagation
• Most popular is generalized arc-consistency (GAC)– A non-binary constraint is GAC iff for every value
for a variable there are consistent values for all other variables in the constraint
– We can again prune values that are not supported
• GAC = AC on binary constraints
GAC on alldifferent• alldifferent(X1,X2,X3)
– Constraint is not GAC– X1=2 cannot be
extended• X2 would have to be 3• No value left then for
X3
– X1={1} is GAC
{1,2}
{2,3}{2,3}
X1
X2 X3
Enforcing GAC• Enforcing GAC is expensive in general
– GAC schema is O(d^k)On k-ary constraint on vars with domains of size d
• Trick is to exploit semantics of constraints– Regin’s all-different algorithm– Achieves GAC in just O(k^3/2 d)
On k-ary all different constraint with domains of size dBased on finding matching in “value graph”
Other types of constraint propagation
• (i,j)-consistency [due to Freuder, JACM 85]– Non-empty domains– Any consistent instantiation for i variables can be
extended to j others
• Describes many different consistency techniques
(i,j)-consistency • Generalization of arc-consistency
– AC = (1,1)-consistency– Path-consistency = (2,1)-consistency
• Strong path-consistency = AC + PC
– Path inverse consistency = (1,2)-consistency
Enforcing (i,j)-consistency• problem is (1,1)-consistent (AC)• BUT is not (2,1)-consistent (PC)
– X1=2, X2=3 cannot be extended to X3
– Need to add constraints:not(X1=2 & X2=3)
not(X1=2 & X3=3)
• Nor is it (1,2)-consistent (PIC)– X1=2 cannot be extended to X2 &
X3 (so needs to be deleted)
{1,2}
{2,3} {2,3}
\=
\=
X1
X3X2
\=
Other types of constraint propagation
• Singleton arc-consistency (SAC)– Problem resulting from instantiating any variable
can be made AC
• Restricted path-consistency (RPC)– AC + if a value has just one support then any third
variable has a consistent value
• …
Comparing local consistencies
• Formal definition of tightness introduced by Debruyne & Bessiere [IJCAI-97]
• A-consistency is tighter than B-consistency iffIf a problem is A-consistent -> it is B-consistent
We write A >= B
Properties• Partial ordering
– reflexive A A– transitive A B & B C implies A C
• Defined relations– tighter A > B iff A B & not B A– incomparable A @ B iff neither A B nor B A
Comparison of consistency techniques
• Exercise for the reader, prove the following identities!
Strong PC > SAC > RPC > AC
NB gaps can reduce search exponentially!
Which to choose?• For binary constraints,
AC is often chosen– Space efficient
Just prune domains (cf PC)
– Time efficient
• For non-binary constraints GAC is often chosen– If we can exploit the
constraint semantics to keep it cheap!
Why consider these other consistencies?
• Promising experimental results– Useful pruning for their
additional cost
• Theoretical value– E.g. GAC on non-binary
constraints may exceed SAC on equivalent binary model
Maintaining a local consistency property
• Tree search– Assign value to variable– Enforce some level of local consistency
• Remove values/add new constraints
– If any future variable has no values, backtrack else repeat
• Two popular algorithms– Maintaining arc-consistency (MAC)– Forward checking (only enforce AC on instantiated variable)
Modelling case study: Modelling case study: all interval seriesall interval series
Modelling case study: Modelling case study: all interval seriesall interval series
Results due to Simonis, Puget & Results due to Simonis, Puget & ReginRegin
All interval series• Prob007 at www.csplib.org
• Comes from musical composition– Traced back to Alban Berg– Extensively used by Ernst Krenek
Op.170 “Quaestio temporis”
All interval series• Take the 12 standard pitch classes
– c, c#, d, ..– Represent them by numbers 0, .., 11
• Find a sequence so each occurs once– Each difference occurs once
All interval series• Can generalize to any n (not just 12)
Find Sn, a permutation of [0,n) such that |Sn+1-Sn| are all distinct
• Finding one solution is easy
All interval series• Can generalize to any n (not just 12)
Find Sn, a permutation of [0,n) such that |Sn+1-Sn| are all distinct
• Finding one solution is easy[n,1,n-1,2,n-2,.., floor(n/2)+2,floor(n/2)-1,floor(n/2)+1,floor(n/2)]
Giving the differences [n-1,n-2,..,2,1]
Challenge is to find all solutions!
Basic recipe• Devise basic CSP model
– What are the variables? What are the constraints?
• Introduce auxiliary variables if needed• Consider dual or combined models• Break symmetry• Introduce implied constraints
Basic CSP model• What are the variables?
Basic CSP model• What are the variables?
Si = j if the ith note is j
• What are the constraints?
Basic CSP model• What are the variables?
Si = j if the ith note is j
• What are the constraints? Si in [0,n)
All-different([S1,S2,… Sn])
Forall i<i’ |Si+1 - Si| =/= |Si’+1 - Si’|
Basic recipe• Devise basic CSP model
– What are the variables? What are the constraints?
• Introduce auxiliary variables if needed• Consider dual or combined models• Break symmetry• Introduce implied constraints
Improving basic model• Introduce auxiliary variables?
– Are there any loose or messy constraints we could better (more compactly?) express via some auxiliary variables?
Improving basic model• Introduce auxiliary variables?
– Yes, variables for the pairwise differences
Di = |Si+1 - Si|
• Now post single large all-different constraint
Di in [1,n-1]
All-different([D1,D2,…Dn-1])
Basic recipe• Devise basic CSP model
– What are the variables? What are the constraints?
• Introduce auxiliary variables if needed• Consider dual or combined models• Break symmetry• Introduce implied constraints
Break symmetry• Does the problem have any symmetry?
Break symmetry• Does the problem have any symmetry?
– Yes, we can reverse any sequence
S1, S2, … Sn is an all-inverse series
Sn, …, S2, S1 is also
• How do we eliminate this symmetry?
Break symmetry• Does the problem have any symmetry?
– Yes, we can reverse any sequenceS1, S2, …, Sn is an all-inverse seriesSn, …, S2, S1 is also
• How do we eliminate this symmetry?• Order first and last difference
D1 < Dn-1
Break symmetry• Does the problem have any other
symmetry?
Break symmetry• Does the problem have any other
symmetry?– Yes, we can invert the numbers in any
sequence0, n-1, 1, n-2, … map x onto n-1-x
n-1, 0, n-2, 1, …
• How do we eliminate this symmetry?
Break symmetry• Does the problem have any other
symmetry?– Yes, we can invert the numbers in any
sequence0, n-1, 1, n-2, … map x onto n-1-xn-1, 0, n-2, 1, …
• How do we eliminate this symmetry?S1 < S2
Performance• Basic model is poor• Improved model able to compute all
solutions up to n=14 or so– GAC on all-different constraints very
beneficial– As is enforcing GAC on Di = |Si+1-Si|
This becomes too expensive for large nSo use just bounds consistency (BC) for larger n
Modelling case study: Modelling case study: Langford’s problemLangford’s problem
Modelling case study: Modelling case study: Langford’s problemLangford’s problem
Model due to Barbara SmithModel due to Barbara Smith
Outline• Introduction
– Langford’s problem
• Modelling it as a CSP– Basic model– Refined model
• Experimental Results• Conclusions
Recipe• Create a basic model
– Decide on the variables
• Introduce auxiliary variables– For messy/loose constraints
• Consider dual, combined or 0/1 models
• Break symmetry• Add implied constraints• Customize solver
– Variable, value ordering
Langford’s problem• Prob024 @
www.csplib.org• Find a sequence of 8
numbers– Each number [1,4]
occurs twice– Two occurrences of i are
i numbers apart
• Unique solution– 41312432
Langford’s problem• L(k,n) problem
– To find a sequence of k*n numbers [1,n]
– Each of the k successive occrrences of i are i apart
– We just saw L(2,4)
• Due to the mathematician Dudley Langford– Watched his son build a
tower which solved L(2,3)
Langford’s problem• L(2,3) and L(2,4) have unique solutions• L(2,4n) and L(2,4n-1) have solutions
– L(2,4n-2) and L(2,4n-3) do not– Computing all solutions of L(2,19) took 2.5 years!
• L(3,n)– No solutions: 0<n<8, 10<n<17, 20, ..– Solutions: 9,10,17,18,19, ..
A014552Sequence: 0,0,1,1,0,0,26,150,0,0,17792,108144,0,0,39809640,326721800,
0,0,256814891280,2636337861200
Basic model• What are the variables?
Basic model• What are the variables?
Variable for each occurrence of a numberX11 is 1st occurrence of 1X21 is 1st occurrence of 2..X12 is 2nd occurrence of 1X22 is 2nd occurrence of 2..
• Value is position in the sequence
Basic model• What are the constraints?
– Xij in [1,n*k]– Xij+1 = i+Xij– Alldifferent([X11,..Xn1,X12,..Xn2,..,X1k,..X
nk])
Recipe• Create a basic model
– Decide on the variables
• Introduce auxiliary variables– For messy/loose constraints
• Consider dual, combined or 0/1 models
• Break symmetry• Add implied constraints• Customize solver
– Variable, value ordering
Break symmetry• Does the problem have any symmetry?
Break symmetry• Does the problem have any symmetry?
– Of course, we can invert any sequence!
Break symmetry• How do we break this symmetry?
Break symmetry• How do we break this symmetry?
– Many possible ways– For example, for L(3,9)
• Either X92 < 14 (2nd occurrence of 9 is in 1st half)
• Or X92=14 and X82<14 (2nd occurrence of 8 is in 1st half)
Recipe• Create a basic model
– Decide on the variables
• Introduce auxiliary variables– For messy/loose constraints
• Consider dual, combined or 0/1 models
• Break symmetry• Add implied constraints• Customize solver
– Variable, value ordering
What about dual model?• Can we take a dual view?
What about dual model?• Can we take a dual view?
• Of course we can, it’s a permutation!
Dual model• What are the variables?
– Variable for each position i
• What are the values?
Dual model• What are the variables?
– Variable for each position i
• What are the values?– If use the number at that position, we cannot use
an all-different constraint– Each number occurs not once but k times
Dual model• What are the variables?
– Variable for each position i
• What are the values?– Solution 1: use values from [1,n*k] with the value
i*n+j standing for the ith occurrence of j– Now want to find a permutation of these numbers
subject to the distance constraint
Dual model• What are the variables?
– Variable for each position i
• What are the values?– Solution 2: use as values the numbers [1,n]– Each number occurs exactly k times– Fortunately, there is a generalization of all-different called
the global cardinality constraint (gcc) for this
Global cardinality constraint
• Gcc([X1,..Xn],l,u) enforces values used by Xi to occur between l and u times– All-different([X1,..Xn]) = Gcc([X1,..Xn],1,1)
• Regin’s algorithm enforces GAC on Gcc in O(n^2.d)
Dual model• What are the constraints?
– Gcc([D1,…Dk*n],k,k)– Distance constraints?
Dual model• What are the constraints?
– Gcc([D1,…Dk*n],k,k)– Distance constraints:
• Di=j then Di+j+1=j
Combined model• Primal and dual variables
• Channelling to link them– What do the channelling constraints look
like?
Combined model• Primal and dual variables
• Channelling to link them– Xij=k implies Dk=i
Solving choices?• Which variables to assign?
– Xij or Di
Solving choices?• Which variables to assign?
– Xij or Di, doesn’t seem to matter
• Which variable ordering heuristic?– Fail First or Lex?
Solving choices?• Which variables to assign?
– Xij or Di, doesn’t seem to matter
• Which variable ordering heuristic?– Fail First very marginally better than Lex
• How to deal with the permutation constraint?– GAC on the all-different– AC on the channelling– AC on the decomposition
Solving choices?• Which variables to assign?
– Xij or Di, doesn’t seem to matter
• Which variable ordering heuristic?– Fail First very marginally better than Lex
• How to deal with the permutation constraint?– AC on the channelling is often best for time
Global constraintsGlobal constraintsGlobal constraintsGlobal constraints
Non-exhaustive catalog• Order constraints• Constraints on values• Partitioning constraints• Timetabling constraints• Graph constraints• Scheduling constraints• Bin-packing constraints
Global constraints• It isn’t just all-different!
• Many constraints specialized to application domains– Scheduling– Packing– ..
Order constraints• min(X,[Y1,..,Yn]) and max(X,[Y1,..Yn])
X <= minimum(Y1,..,Yn)
X >= maximum(Y1,..Yn)
Order constraints• min_n(X,n,[Y1,..Ym]) and max_n(X,n,
[Y1,..,Ym)
X is nth smallest value in Y1,..YmX is nth largest value in Y1,..Ym
Value constraints• among(N,[Y1,..,Yn],[val1,..,valm])
N vars in [Y1,..,Yn] take values val1,..valme.g. among(2,[1,2,1,3,1,5],[3,4,5])
Value constraints• among(N,[Y1,..,Yn],[val1,..,valm])
N vars in [Y1,..,Yn] take values val1,..valme.g. among(2,[1,2,1,3,1,5],[3,4,5])
• count(n,[Y1,..,Ym],op,X) where op is =,<,>,=/,<= or >=relation “Yi op X” holds n times
among(n,[Y1,..,Ym],[k])=count(n,[Y1,..,Ym],=,k)
Value constraints• balance(N,[Y1,..,Yn])
N = occurrence of more frequent value - occurrence of least frequent value
E.g balance(2,[1,1,1,3,4,2])
Value constraints• balance(N,[Y1,..,Yn])
N = occurrence of more frequent value - occurrence of least frequent value
E.g balance(2,[1,1,1,3,4,2])
all-different([Y1,..,Yn]) => balance(0,[Y1,..,Yn])
Value constraints• min_nvalue(N,[Y1,..,Yn]) and max_nvalue(N,
[Y1,..,Yn])least (most) common value in Y1,..,Yn occurs N timesE.g. min_nvalue(2,[1,1,2,2,2,3,3,5,5])
Can replace multiple count or among constraints
Value constraints• common(X,Y,[X1,..,Xn],[Y1,..,Ym])
X vars in Xi take a value in Yi
Y vars in Yi take a value in Xi
E.g. common(3,4,[1,9,1,5],[2,1,9,9,6,9])
Value constraints• common(X,Y,[X1,..,Xn],[Y1,..,Ym])
X vars in Xi take a value in YiY vars in Yi take a value in XiE.g. common(3,4,[1,9,1,5],[2,1,9,9,6,9])
among(X,[Y1,..,Yn],[val1,..,valm]) = common(X,Y,[X1,..,Yn],[val1,..,valm])
Value constraints• same([X1,..,Xn],[Y1,..,Yn])
Yi is a permutation of Xi
Value constraints• same([X1,..,Xn],[Y1,..,Yn])
Yi is a permutation of Xi
• used_by([X1,..,Xn],[Y1,..,Ym])all values in Yi are used by vars in Xim>=m
Value constraints• same([X1,..,Xn],[Y1,..,Yn])
Yi is a permutation of Xi
• used_by([X1,..,Xn],[Y1,..,Ym])all values in Yi are used by vars in Xim>=m
• on n values:alldifferent([X1,..,Xn])=same([X1,..,Xn],[1,..,n])
=used_by([X1,..,Xn],[1,..,n])
Partitioning constraints• all-different([X1,..,Xn])
Partitioning constraints• all-different([X1,..,Xn])
• Other flavoursall-different_except_0([X1,..,Xn])Xi=/Xj unless Xi=Xj=0
• 0 is often used for modelling purposes as “dummy” value
– Don’t use this slab– Don’t open this bin ..
Partitioning constraints• all-different([X1,..,Xn])
• Other flavourssymmetric-all-different([X1,..,Xn])Xi=/Xj and Xi=j iff Xj=i
• Very common in practice– Team i plays j iff Team j plays i..
Partitioning constraints• nvalue(N,[X1,..,Xn])
Xi takes N different valuesall-different([X1,..,Xn]) = nvalue(n,[X1,..,Xn)
Partitioning constraints• nvalue(N,[X1,..,Xn])
Xi takes N different valuesall-different([X1,..,Xn]) = nvalue(n,[X1,..,Xn)
• gcc([X1,..,Xn],Lo,Hi)values in Xi occur between Lo and Hi timesall-different([X1,..,Xn])=gcc([X1,..,Xn],1,1)
Timetabling constraints• change(N,[X1,..,Xn]),op) where op is
{=,<,>,<=,>=,/=}“Xi op Xi+1” holds N timesE.g. change(3,[4,4,3,4,1],/=)
• You may wish to limit the number of changes of classroom, shifts, …
Timetabling constraints• longest_changes(N,[X1,..,Xn]),op) where op
is {=,<,>,<=,>=,/=}longest sequence “Xi op Xi+1” is of length NE.g. longest_changes(2,[4,4,4,3,3,2,4,1,1,1],=)
• You may wish to limit the length of a shift without break, …
Graph constraints• Tours in graph a often represented by
the successors:
• [X1,..,Xn] means from node i we go to node Xi
Graph constraints• Tours in graph a often represented by
the successors:• [X1,..,Xn] means from node i we go to
node Xi• E.g. [2,1,5,3,4] represents the 2 cycles
(1)->(2)->(1) and (3)->(5)->(4)->(3)
Graph constraints• cycle(N,[X1,..,Xn])
there are N cycles in Xi
e.g. cycle(2,[2,1,5,3,4]) as we have the 2 cycles(1)->(2)->(1) and (3)->(5)->(4)->(3)
Useful for TSP like problems (e.g. sending engineers out to repair phones)
Scheduling constraints• cummulative([S1,..,Sn],[D1,..,Dn],[E1,..,En],
[H1,..,Hn],L)• schedules n (concurrent) jobs, each with a
height Hi• ith job starts at Si, runs for Di and ends at Ei
– Ei=Si+Di
• at any time, accumulated height of running jobs is less than L
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Scheduling constraints• coloured_cummulative([S1,..,Sn],[D1,..,Dn],
[E1,..,En],[C1,..,Cn],L)• schedules n (concurrent) each with a colour
Ci• no more than L colours running at any one
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Scheduling constraints• cycle_cummulative(m,[S1,..,Sn],
[D1,..,Dn],[E1,..,En],[H1,..,Hn],L)• schedules n (concurrent) jobs, each
with a height Hi onto a cyclic schedule of length m
Scheduling constraints• cummulatives([M1,,,Mn],[S1,..,Sn],[D1,..,Dn],
[E1,..,En],[H1,..,Hn],[L1,..,Lm])• schedules n (concurrent) jobs, each with a
height Hi onto one of m machines• ith runs on Mi• accumulated height of running jobs on
machine i <= Li
Scheduling constraints• cummulatives([M1,..Mn],[S1,..,Sn],
[D1,..,Dn],[E1,..,En],[H1,..,Hn],[L1,..,Lm])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Machine 1
Machine 2
Scheduling constraints• coloured_cummulatives([M1,,,Mn],[S1,..,Sn],
[D1,..,Dn],[E1,..,En],[C1,..,Cn],[L1,..,Lm])• schedules n (concurrent) jobs, each with a
colour i onto one of m machines• ith runs on Mi• number of colours of running jobs on
machine i <= Li
Bin-packing constraints• bin_packing(capacity,[B1,..,Bn],
[w1,..,wn])
for each bin j, sum_Bi=j wi <= capacity
Bin-packing constraints• bin_packing(capacity,[B1,..,Bn],
[w1,..,wn])
for each bin j, sum_Bi=j wi <= capacity
special case of cummulative with task durations=1
Misc constraints• element(Index,[a1,..,an],Var)
Var=a_Index
constraint programming’s answer to arrays!
e.g. element(Item,[10,23,12,15],Cost)
Modelling: set variablesModelling: set variablesModelling: set variablesModelling: set variables
Motivation• Sets are useful when
– We don’t know how many objects we will have • e.g. set of items go in a bin
– We have symmetrical objects• e.g. items are symmetric and we don’t want to consider
all their permutations
And in many other situations!
Outline• Representing set variables
– Bounds– Characteristic functions
• Constraining set variables– Primitive constraints– Global constraints
Set variables• Representing sets
– Domain of values = powerset – Exponential space would be needed to represent this
extensional
Set variables• Representing sets
– Domain of values = powerset – Exponential space would be needed to represent this
extensional– Compromise: just represent upper and lower bound
• E.g. {} subseteq X subseteq {1,2}
• X in {{},{1},{2},{1,2}}
Set variables• Representing sets
– Domain of values = powerset
– Exponential space would be needed to represent this extensional
– Compromise: just represent upper and lower bound• E.g. {} subseteq X subseteq {1,2}• X in {{},{1},{2},{1,2}}
– Tradeoff• Cannot represent disjunction• E.g. X is {1} or {2} but not {1,2}
Alternative representation• Characteristic function
– i in X iff Xi=1
Alternative representation• Characteristic function
– i in X iff Xi=1– E.g. {1} subseteq X subseteq {1,2,3}
Alternative representation• Characteristic function
– i in X iff Xi=1– E.g. {1} subseteq X subseteq {1,2,3}
• X1=1• X2 in {0,1}, X3 in {0,1} • X4=X5=..=0
Primitive constraints• X subset Y• X subseteq Y• a in X• X = Y intersect Z• X = Y union Z• X = Y - Z• X = {}
Bounds consistency• Analogous to bounds consistency on
ordered finite domains
• Given constraint, C over X1,..,Xn
• C is BC if for each Xi, – a in glb(Xi) iff a is in some solution – a in lub(Xi) iff a is in all solutions
Bounds consistency• Lub(A union B) => Lub(A) union Lub(B)
• Glb(A union B) => Glb(A) union Glb(B)
Bounds consistency• Lub(A union B) => Lub(A) union Lub(B)
• Glb(A union B) => Glb(A) union Glb(B)– This last rule is a safe approximation– Glb(A union B) superseteq Glb(A) union Glb(B)
Bounds consistency• Lub(A union B) => Lub(A) union Lub(B)
• Glb(A union B) => Glb(A) union Glb(B)
• Lub(A intersect B) => Lub(A) intersect Lub(B)
• Glb(A intersect B) => Glb(A) intersect Glb(B)
Bounds consistency• Lub(A union B) => Lub(A) union Lub(B)
• Glb(A union B) => Glb(A) union Glb(B)
• Lub(A intersect B) => Lub(A) intersect Lub(B)
• Glb(A intersect B) => Glb(A) intersect Glb(B)– The third rule is a safe approximation– Lub(A intersect B) subseteq
Lub(A) intersect Lub(B)
Bounds consistency• Lub(A union B) => Lub(A) union Lub(B)
• Glb(A union B) => Glb(A) union Glb(B)
• Lub(A intersect B) => Lub(A) intersect Lub(B)
• Glb(A intersect B) => Glb(A) intersect Glb(B)
• A subseteq B => Lub(A) subseteq Lub(B),
Glb(A) subseteq Glb(B)
Bounds consistency• Lub(A union B) => Lub(A) union Lub(B)
• Glb(A union B) => Glb(A) union Glb(B)
• Lub(A intersect B) => Lub(A) intersect Lub(B)
• Glb(A intersect B) => Glb(A) intersect Glb(B)
• A subseteq B => Lub(A) subseteq Lub(B),
Glb(A) subseteq Glb(B)
• A = B => A subseteq B, B subseteq A
…
Bounds consistency• Apply these rules exhaustively
• This computes safe approximations– Lub’s are correct or too large– Glb’s are correct or too small
• If a set of constraints have a solution– Exists unique lub and glb for the set variables– That satisfy these rules
Intervals v Characteristic functions
• It doesn’t really matter!
• TheoremA set of constraints in normal form is BC iff
the characteristic function representation is AC
Intervals v Characteristic functions
• It doesn’t really matter!
• TheoremA set of constraints in normal form is BC iff the
characteristic function representation is AC
NB characteristic function is 0/1 model so AC is the same as bounds consistency!
Global constraints• All-different constraint
– No two vars take same value
• Permutation constraint– Special case of all-different– All values are used
Global constraints• All-different constraint
– No two vars take same value
• Disjoint constraint– No two set variables intersect
Global constraints• All-different constraint
– No two vars take same value
• Disjoint constraint– No two set variables intersect
• Permutation constraint– All-different, & all values are used
Global constraints• All-different constraint
– No two vars take same value
• Disjoint constraint– No two set variables intersect
• Permutation constraint– All-different, & all values are used
• Partition constraint– Disjoint, & all values are used
Global constraints• Disjoint([X1,..,Xn]) decomposes into
Xi intersect Xj = {}
Just like the all-different constraint!
Global constraints• Disjoint([X1,..,Xn]) decomposes into
Xi intersect Xj = {}
Just like the all-different constraint!
But decomposition does not hurt!
Global constraints• Disjoint([X1,..,Xn]) decomposes into
Xi intersect Xj = {}
Just like the all-different constraint!
But decomposition does not hurt!
Theorem
BC on disjoint([X1,..,Xn]) =
BC on decomposed model
Global constraints• Partition([X1,..,Xn],S) decomposes into
Xi intesect Xj = {}
X1 union X2=Y1
X3 union Y1 = Y2
…
Xn union Yn-2 = S
Global constraints• Partition([X1,..,Xn],S) decomposes into
Xi intesect Xj = {}
X1 union X2=Y1
X3 union Y1 = Y2
…
Xn union Yn-2 = S
Decomposition does not hurt!
Global constraints• Partition([X1,..,Xn],S) decomposes
Decomposition again does not hurt!
TheoremBC on partition([X1,..,Xn],S) =BC on the decomposed model
Global constraints• Non-empty-partition([X1,..,Xn],S)
• Decomposes as before
• With additional constraint:– |Xi| > 0
Global constraints• Non-empty-partition([X1,..,Xn],S)
Decomposition now hurts!
Global constraints• Non-empty-partition([X1,..,Xn],S)
Decomposition now hurts!TheoremBC on non-empty-partition([X1,..,Xn],S) is strictly stronger than BC on decompostion
Modelling with set constraints
• Ternary Steiner problem– n(n-1)/6 sets, Si – Each a subset of {1,..,n}– |Si| = 3
Nb n(n-1)/6 = nC2 / 3
Modelling with set constraints
• Ternary Steiner problem– n(n-1)/6 sets, Si – Each a subset of {1,..,n}– |Si| = 3– |Si intersect Sj| <=1
Ternary Steiner problem• Only soluble if n mod 6 = 1 or 3 [Kirkman 1847]
– S3 = {{1,2,3}}– S7 = {{1,2,4},{2,3,5},{3,4,6},{4,5,7},{5,6,1},{6,7,2},{7,1,3}}
• Number of non-isomorphic triples– 1,1,2,80,>1.1*10^9 [Colbourn & Dinitz 1996]– AO30139
Ternary Steiner problem• Simple and elegant set variable model
Let t=n(n-1)/6For all j in [1,t] . Sj subset {1,..,n} and |Sj|=3Atmost1([S1,..,St])
Ternary Steiner problem• Simple and elegant set variable model
Let t=n(n-1)/6For all j in [1,t] . Sj subset {1,..,n} and |Sj|=3Atmost1([S1,..,St])
For all j<k in [1,t] . |Sj intersect Sk| <=1
What’s wrong with this model?
Ternary Steiner problem• Simple and elegant set variable model
Let t=n(n-1)/6For all j in [1,t] . Sj subset {1,..,n} and |Sj|=3Atmost1([S1,..,St])
What’s wrong with this model?Numbers and Si are still symmetric.How do we deal with this?
Ternary Steiner problem– Breaking symmetry
• Make some initial assignments• S1={1,2,3}, S2={2,4,5}, …• Until all numbers named once
Ternary Steiner problem– Breaking symmetry
• Order sets S1<S2<..<St • e.g. multiset ordering
Conclusions• Set variables are useful for modelling
– But they still can leave symmetry!
• Constraints on set variables– Primitive constraints– Global constraints
• Decomposition hurts in general as soon as we add cardinality conditions
Recipe• Create a basic model
– Decide on the variables
• Introduce auxiliary variables– For messy/loose constraints
• Consider dual, combined or 0/1 models
• Break symmetry• Add implied constraints• Customize solver
– Level of consistency
– Variable, value ordering
Solving choices• Level of consistency
– Binary v non-binary constraints– GAC, BC …
Solving choices• Branching heuristics
– Variable ordering (fail first)• Smallest domain, max degree, …
– Value ordering (succed first)• Max promise, …
Recipe• Create a basic model
– Decide on the variables
• Introduce auxiliary variables– For messy/loose constraints
• Consider dual, combined or 0/1 models
• Break symmetry• Add implied constraints• Customize solver
– Level of consistency– Variable, value ordering