Download presentation
Presentation is loading. Please wait.
Published byAdrian Cadwell Modified over 10 years ago
1
Modelling & Solving with Constraints Prof. Toby Walsh University College Cork/Uppsala University
2
Overview Introduction to constraint programming –Constraint propagation –Backtracking search Modelling case studies –Simple recipe Solving with constraints –Global constraints –Set variables –Branching heuristics
3
Resources Course links –www.cs.york.ac.uk/~tw/Links/csps/www.cs.york.ac.uk/~tw/Links/csps/ Benchmark problems –www.csplib.orgwww.csplib.org Constraints solvers –LP based like ECLIPSE, Java based solvers like JCL, …
4
Constraint programming “Dream” of declarative programming –State the constraints –Solver finds a solution Method of choice for many hard combinatorial problems –Scheduling, assignment, routing, …
5
Constraints are everywhere! No meetings before 10am Network traffic < 100 Gbytes/sec PCB width < 21cm Salary > 45k Euros …
6
Constraint satisfaction Constraint satisfaction problem (CSP) is a triple 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
7
Example CSP Course timetabling –Variable for each course CS101, OS102.. –Domain are possible times wed9am, fri10am,.. –Constraints: CS101 \= wed9am Capacity constraints: atmost(3,[OS102,DB103..],wed9 am) Lecturer constraints: alldifferent([CS101,DB103,…])
8
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, …
9
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, …
10
Constraints Constraints are tuples 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
11
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]), …
12
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!
13
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
14
Some non-binary examples Timetabling –Variables: Lecture1, Lecture2, … –Values: time1, time2, … –Constraint that lectures taught by same lecturer do not conflict: alldifferent(Lecture1,Lecture5,…).
15
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).
16
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!
17
Modelling with non-binary constraints Benefits include: –Compact, declarative specifications (discussed next) –Efficient constraint propagation (discussed second)
18
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).
19
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
20
Constraint solvers Two main approaches –Systematic, tree search algorithms –Local search or repair based procedures Other more exotic possibilities –Hybrid algorithms –Quantum algorithms
21
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, …
22
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
23
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
24
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
25
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
26
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
27
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
28
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} X1 X2 X3
29
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 d Based on finding matching in “value graph”
30
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
31
(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
32
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} \= X1 X3X2 \=
33
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 …
34
Comparing local consistencies Formal definition of tightness introduced by Debruyne & Bessiere [IJCAI-97] A-consistency is tighter than B-consistency iff If a problem is A-consistent -> it is B-consistent We write A >= B
35
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
36
Comparison of consistency techniques Exercise for the reader, prove the following identities! Strong PC > SAC > RPC > AC NB gaps can reduce search exponentially!
37
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!
38
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
39
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)
40
Modelling case study: all interval series Results due to Simonis, Puget & Regin
41
All interval series Prob007 at www.csplib.orgwww.csplib.org Comes from musical composition –Traced back to Alban Berg –Extensively used by Ernst Krenek Op.170 “Quaestio temporis”
42
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
43
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
44
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!
45
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
46
Basic CSP model What are the variables?
47
Basic CSP model What are the variables? Si = j if the ith note is j What are the constraints?
48
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’|
49
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
50
Improving basic model Introduce auxiliary variables? –Are there any loose or messy constraints we could better (more compactly?) express via some auxiliary variables?
51
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])
52
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
53
Break symmetry Does the problem have any symmetry?
54
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?
55
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? Order first and last difference D1 < Dn-1
56
Break symmetry Does the problem have any other symmetry?
57
Break symmetry Does the problem have any other symmetry? –Yes, we can invert the numbers in any sequence 0, n-1, 1, n-2, … map x onto n-1-x n-1, 0, n-2, 1, … How do we eliminate this symmetry?
58
Break symmetry Does the problem have any other symmetry? –Yes, we can invert the numbers in any sequence 0, n-1, 1, n-2, … map x onto n-1-x n-1, 0, n-2, 1, … How do we eliminate this symmetry? S1 < S2
59
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 n So use just bounds consistency (BC) for larger n
60
Modelling case study: Langford’s problem Model due to Barbara Smith
61
Outline Introduction –Langford’s problem Modelling it as a CSP –Basic model –Refined model Experimental Results Conclusions
62
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
63
Langford’s problem Prob024 @ www.csplib.org 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
64
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)
65
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,.. A014552 Sequence: 0,0,1,1,0,0,26,150,0,0,17792,108144,0,0,39809640,326721800, 0,0,256814891280,2636337861200
66
Basic model What are the variables?
67
Basic model What are the variables? Variable for each occurrence of a number X11 is 1st occurrence of 1 X21 is 1st occurrence of 2.. X12 is 2nd occurrence of 1 X22 is 2nd occurrence of 2.. Value is position in the sequence
68
Basic model What are the constraints? –Xij in [1,n*k] –Xij+1 = i+Xij –Alldifferent([X11,..Xn1,X12,..Xn2,..,X1k,..X nk])
69
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
70
Break symmetry Does the problem have any symmetry?
71
Break symmetry Does the problem have any symmetry? –Of course, we can invert any sequence!
72
Break symmetry How do we break this symmetry?
73
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)
74
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
75
What about dual model? Can we take a dual view?
76
What about dual model? Can we take a dual view? Of course we can, it’s a permutation!
77
Dual model What are the variables? –Variable for each position i What are the values?
78
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
79
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
80
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
81
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)
82
Dual model What are the constraints? –Gcc([D1,…Dk*n],k,k) –Distance constraints?
83
Dual model What are the constraints? –Gcc([D1,…Dk*n],k,k) –Distance constraints: Di=j then Di+j+1=j
84
Combined model Primal and dual variables Channelling to link them –What do the channelling constraints look like?
85
Combined model Primal and dual variables Channelling to link them –Xij=k implies Dk=i
86
Solving choices? Which variables to assign? –Xij or Di
87
Solving choices? Which variables to assign? –Xij or Di, doesn’t seem to matter Which variable ordering heuristic? –Fail First or Lex?
88
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
89
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
90
Global constraints
91
Non-exhaustive catalog Order constraints Constraints on values Partitioning constraints Timetabling constraints Graph constraints Scheduling constraints Bin-packing constraints
92
Global constraints It isn’t just all-different! Many constraints specialized to application domains –Scheduling –Packing –..
93
Order constraints min(X,[Y1,..,Yn]) and max(X,[Y1,..Yn]) X <= minimum(Y1,..,Yn) X >= maximum(Y1,..Yn)
94
Order constraints min_n(X,n,[Y1,..Ym]) and max_n(X,n,[Y1,..,Ym) X is nth smallest value in Y1,..Ym X is nth largest value in Y1,..Ym
95
Value constraints among(N,[Y1,..,Yn],[val1,..,valm]) N vars in [Y1,..,Yn] take values val1,..valm e.g. among(2,[1,2,1,3,1,5],[3,4,5])
96
Value constraints among(N,[Y1,..,Yn],[val1,..,valm]) N vars in [Y1,..,Yn] take values val1,..valm e.g. among(2,[1,2,1,3,1,5],[3,4,5]) count(n,[Y1,..,Ym],op,X) where op is =,,=/, = relation “Yi op X” holds n times among(n,[Y1,..,Ym],[k])= count(n,[Y1,..,Ym],=,k)
97
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])
98
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])
99
Value constraints min_nvalue(N,[Y1,..,Yn]) and max_nvalue(N,[Y1,..,Yn]) least (most) common value in Y1,..,Yn occurs N times E.g. min_nvalue(2,[1,1,2,2,2,3,3,5,5]) Can replace multiple count or among constraints
100
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])
101
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]) among(X,[Y1,..,Yn],[val1,..,valm]) = common(X,Y,[X1,..,Yn],[val1,..,valm])
102
Value constraints same([X1,..,Xn],[Y1,..,Yn]) Yi is a permutation of Xi
103
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 Xi m>=m
104
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 Xi m>=m on n values: alldifferent([X1,..,Xn])=same([X1,..,Xn],[1,..,n]) =used_by([X1,..,Xn],[1,..,n])
105
Partitioning constraints all-different([X1,..,Xn])
106
Partitioning constraints all-different([X1,..,Xn]) Other flavours all-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..
107
Partitioning constraints all-different([X1,..,Xn]) Other flavours symmetric-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..
108
Partitioning constraints nvalue(N,[X1,..,Xn]) Xi takes N different values all-different([X1,..,Xn]) = nvalue(n,[X1,..,Xn)
109
Partitioning constraints nvalue(N,[X1,..,Xn]) Xi takes N different values all-different([X1,..,Xn]) = nvalue(n,[X1,..,Xn) gcc([X1,..,Xn],Lo,Hi) values in Xi occur between Lo and Hi times all-different([X1,..,Xn])=gcc([X1,..,Xn],1,1)
110
Timetabling constraints change(N,[X1,..,Xn]),op) where op is {=,, =,/=} “Xi op Xi+1” holds N times E.g. change(3,[4,4,3,4,1],/=) You may wish to limit the number of changes of classroom, shifts, …
111
Timetabling constraints longest_changes(N,[X1,..,Xn]),op) where op is {=,, =,/=} longest sequence “Xi op Xi+1” is of length N E.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, …
112
Graph constraints Tours in graph a often represented by the successors: [X1,..,Xn] means from node i we go to node Xi
113
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)
114
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)
115
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
116
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
117
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
118
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
119
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
120
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
121
Bin-packing constraints bin_packing(capacity,[B1,..,Bn],[w1,..,w n]) for each bin j, sum_Bi=j wi <= capacity
122
Bin-packing constraints bin_packing(capacity,[B1,..,Bn],[w1,..,w n]) for each bin j, sum_Bi=j wi <= capacity special case of cummulative with task durations=1
123
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)
124
Modelling: set variables
125
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!
126
Outline Representing set variables –Bounds –Characteristic functions Constraining set variables –Primitive constraints –Global constraints
127
Set variables Representing sets –Domain of values = powerset –Exponential space would be needed to represent this extensional
128
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}}
129
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}
130
Alternative representation Characteristic function –i in X iff Xi=1
131
Alternative representation Characteristic function –i in X iff Xi=1 –E.g. {1} subseteq X subseteq {1,2,3}
132
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
133
Primitive constraints X subset Y X subseteq Y a in X X = Y intersect Z X = Y union Z X = Y - Z X = {}
134
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
135
Bounds consistency Lub(A union B) => Lub(A) union Lub(B) Glb(A union B) => Glb(A) union Glb(B)
136
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)
137
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)
138
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)
139
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)
140
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 …
141
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
142
Intervals v Characteristic functions It doesn’t really matter! Theorem A set of constraints in normal form is BC iff the characteristic function representation is AC
143
Intervals v Characteristic functions It doesn’t really matter! Theorem A 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!
144
Global constraints All-different constraint –No two vars take same value Permutation constraint –Special case of all-different –All values are used
145
Global constraints All-different constraint –No two vars take same value Disjoint constraint –No two set variables intersect
146
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
147
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
148
Global constraints Disjoint([X1,..,Xn]) decomposes into Xi intersect Xj = {} Just like the all-different constraint!
149
Global constraints Disjoint([X1,..,Xn]) decomposes into Xi intersect Xj = {} Just like the all-different constraint! But decomposition does not hurt!
150
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
151
Global constraints Partition([X1,..,Xn],S) decomposes into Xi intesect Xj = {} X1 union X2=Y1 X3 union Y1 = Y2 … Xn union Yn-2 = S
152
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!
153
Global constraints Partition([X1,..,Xn],S) decomposes Decomposition again does not hurt! Theorem BC on partition([X1,..,Xn],S) = BC on the decomposed model
154
Global constraints Non-empty-partition([X1,..,Xn],S) Decomposes as before With additional constraint: –|Xi| > 0
155
Global constraints Non-empty-partition([X1,..,Xn],S) Decomposition now hurts!
156
Global constraints Non-empty-partition([X1,..,Xn],S) Decomposition now hurts! Theorem BC on non-empty-partition([X1,..,Xn],S) is strictly stronger than BC on decompostion
157
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
158
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
159
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
160
Ternary Steiner problem Simple and elegant set variable model Let t=n(n-1)/6 For all j in [1,t]. Sj subset {1,..,n} and |Sj|=3 Atmost1([S1,..,St])
161
Ternary Steiner problem Simple and elegant set variable model Let t=n(n-1)/6 For all j in [1,t]. Sj subset {1,..,n} and |Sj|=3 Atmost1([S1,..,St]) For all j<k in [1,t]. |Sj intersect Sk| <=1 What’s wrong with this model?
162
Ternary Steiner problem Simple and elegant set variable model Let t=n(n-1)/6 For all j in [1,t]. Sj subset {1,..,n} and |Sj|=3 Atmost1([S1,..,St]) What’s wrong with this model? Numbers and Si are still symmetric. How do we deal with this?
163
Ternary Steiner problem –Breaking symmetry Make some initial assignments S1={1,2,3}, S2={2,4,5}, … Until all numbers named once
164
Ternary Steiner problem –Breaking symmetry Order sets S1<S2<..<St e.g. multiset ordering
165
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
166
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
167
Solving choices Level of consistency –Binary v non-binary constraints –GAC, BC …
168
Solving choices Branching heuristics –Variable ordering (fail first) Smallest domain, max degree, … –Value ordering (succed first) Max promise, …
169
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.