Prepare for your exams

Study with the several resources on Docsity

Earn points to download

Earn points by helping other students or get them with a premium plan

Guidelines and tips

Prepare for your exams

Study with the several resources on Docsity

Earn points to download

Earn points by helping other students or get them with a premium plan

Community

Ask the community

Ask the community for help and clear up your study doubts

University Rankings

Discover the best universities in your country according to Docsity users

Free resources

Our save-the-student-ebooks!

Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors

Constraint Satisfaction and Software Optimization

Typology: Study notes

2010/2011

1 / 101

Download Constarint Programming, Lecture Notes - Computer Science 9 and more Study notes Computer Numerical Control in PDF only on Docsity! Alan M. Frisch Artificial Intelligence Group Department of Computer Science University of York Modelling with Constraints Formulating Concrete Models: 1 Constraint Programming Last updated: 3 December 2010 Ian Miguel School of Computing University of St Andrews 2 Recap • We saw how problems can be specified as abstract constraint models in which the domain elements are combinatorial types such as: – sequence, permutation – set, multiset – partition – relation, function • Existing constraint solvers/toolkits do not support these (though many support sets) 5 Mapping Abstract Models to Concrete Models • Process is driven by a set of rules or patterns that are based on the types of the decision variables. • Each decision variable of an abstract type is mapped to a collection of decision variables of concrete types, usually with some constraints on this collection. 6 Mapping Abstract Models to Concrete Models • First we consider, where τ is atomic – sequences of τ, including permutations – sets of τ – multisets of τ – relations on τ1x τ2 – functions on from τ1 to τ2, including surjective, injective and bijective functions 7 Mapping Abstract Models to Concrete Models • Then we consider nested types. • Examples: ‒ sequence of set of τ ‒ set of set of τ ‒ multiset of partition of τ 10 Where Does the Sequence Pattern Occur? • Communications: • Low Autocorrelation Binary Sequences (CSPLib 5). • Mathematics: • Langford’s Problem (CSPLib 24). • Error-Correcting Codes (CSPLib 36). • Puzzles: • Magic Sequences (CSPLib 19). 11 Fixed-length Sequences • Problems of the form: • Given n, • Find a sequence of objects of length n, • Such that … 12 Fixed-length Sequences • Example (Magic Sequence, CSPLib 19): • Given n, a non-negative integer • Find a sequence S of integers s0, …, sn each of which is between 0 and n • Such that there are si occurrences of i in S for each i in 0, …, n. 15 Bounded-length Sequences • Problems of the form: • Given n, • Find a sequence of objects of length at most n, • Such that … 16 Bounded-length Sequences • Example (Kiselman Semigroup Problem): • Given n, .a positive integer • Find a sequence of at most 2n/2+1-2 integers drawn from 1..n • Such that between every pair of occurrences of an integer i there exists an integer greater than i and an integer less than i. 17 Bounded-length Sequences Again, we can use an array indexed 1.. 2n/2+1-2: 1..n 1..n 1..n 1..n 1 2 3 4 1..n 2n/2+1-2 … S’: Problem: What if a solution has length less than 2n/2+1-2? Example: The empty sequence is always a solution to this problem. Given n a positive integer Find a sequence of at most 2n/2+1-2 integers drawn from 1..n Such that between every pair of occurrences of an integer I there exists an integer greater than i and an integer less than i. 20 Bounded-length Sequences So, for n = 4 and the solution sequence 1,2 is represented by multiple solutions to the model. 1 2 0 0 0 0 1 2 3 4 S’: Adding the dummy value has created equivalence classes of symmetric assignments. Notice that these symmetries are not in the abstract model. We introduced them into the concrete model. 5 6 1 0 0 2 0 0 1 2 3 4 5 6 S’: 21 Bounded-length Sequences • Solution: choose a canonical element from each class and add a constraint to rule out all other elements. • In this case we can chose the canonical element to be the one in which all 0’s appear at the end of the sequence. All other assignments can be excluded by the constraint • Forall i in 1.. 2n/2+1-3 . (S’[i] = 0) (S’[i + 1] = 0) 1 2 0 0 0 0 1 2 3 4 S’: 5 6 1 0 0 2 0 0 1 2 3 4 5 6 S’: 22 Something to Note • It is very common when modelling an abstract object to introduce equivalences during modelling. • Need to be aware of this happening, and of the measure used to counter it. 25 Permutations Example: The Car Sequencing Problem (CSPLib 1) • Given a set of cars that must be manufactured • Find an order in which they should go down the conveyor belt such that… Figure from Foundations of Constraint Satisfaction [Tsang, 1993] 26 Permutations: First Viewpoint • Assume that the elements of the permutation are distinct. • First viewpoint is as fixed-length sequence. If permutation contains elements a, …, f: a..f a..f a..f a..f a..f a..f 1 2 3 4 Perm1: 5 6 Constraint: All-different(Perm1) 27 Permutations: Second Viewpoint • Alternatively, we know the elements that appear in the sequence, so we can index by those elements: 1..6 1..6 1..6 1..6 1..6 1..6 a b c d Perm2 e f Constraint: All-different(Perm2) 2 1 4 3 5 6 a b c d Perm2 e f • Domain values represent the position in the sequence an element is in. So “badcef” would be: 30 Permutations: Which Viewpoint to Use? • Depends on the constraints on the permutation. • Example: The first three letters of the sequence must form an English word. a..f a..f a..f a..f a..f a..f 1 2 3 4 Perm1 5 6 • Impose a table constraint on the first three variables in Perm1 that allows “bad”, “cad”, “fad”, … • A table constraint is one where the satisfying tuples are given explicitly (extensionally). 31 Permutations: Which Viewpoint to Use? • Depends on the constraints on the permutation. • Example: The first three letters of the sequence must form an English word. Horrible: (Perm2[a] = 1 ∧ Perm2[c] = 2 ∧ Perm2[e] = 3) ∨ ( . . . ) : : 1..6 1..6 1..6 1..6 1..6 1..6 a b c d Perm2 e f 32 Sequences: Summary • Fixed-length. • Bounded-length. • Permutations, two viewpoints • Try some of the problems from CSPLib! 35 So Why Bother? • Not all constraint systems provide sets: e.g. Minion • Those that do rarely (if ever!) allow quantification over decision variables: Example: Find a set S of n integers Such that for any x and y in S, x+y≠k • Sometime we need domains of non-atomic sets: e.g. set of set of integers • Different representations are best in different cases • Building sets into a solver requires an understanding of how to represent them 36 Where does the Set Pattern Occur? • Packing Problems: • Can often represent a container (e.g. a bin) as a set of objects. See also: • Steel Mill Slab Design (CSPLib 38) • Rack Configuration (CSPLib 31) 37 Where does the Set Pattern Occur? • Scheduling: • E.g. Progressive Party Problem (CSPLib 13) • Timetable a party at a yacht club. • Certain boats designated hosts, crews of remaining boats in turn visit the host boats for successive half-hour periods. • View a boat as a set of crews. See also: Social Golfers (CSPLib 10) 40 Fixed-cardinality Sets: Explicit Representation • Given n and s, Find a set of n digits that sum to s. • Introduce E, an array of decision variables indexed by 1..n. Domain of each is 0..9: 0..9 0..9 0..9 0..9 … 0..9 1 3 2 4 n • Constraints: E AllDifferent(E). Sum(E) = s. 41 Fixed-cardinality Sets: Explicit Representation • So the set {1, 3, 5, 7} might be represented: 1 3 5 7 1 3 2 4 • However, it might also be represented: E 7 3 5 1 1 3 2 4 E • Once again, a modelling step has introduced equivalence classes of assignments. 42 Fixed-cardinality Sets: Explicit Representation • So the set {1, 3, 5, 7} might be represented: 1 3 5 7 1 3 2 4 E 7 3 5 1 1 3 2 4 E • Again can choose a canonical element from each class. • Obvious choice is to require ascending order. • So, we can replace AllDifferent(E) with E[1] < E[2] < E[3] < E[4] 45 Explicit vs. Occurrence Representations • What if we want to say: “If 5 is in the set then so is 4”? • In occurrence Rep: 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 O 0 5 6 7 8 9 O[5] = 1 → O[4] = 1 1 46 Explicit vs. Occurrence Representations • What if we want to say: • “The difference between every pair of elements is not equal to some variable d”? • In explicit representation: 0..9 0..9 0..9 0..9 … 0..9 1 3 2 4 n E Forall i in 1..n . Forall j in i+1..n . E[j] - E[i] ≠ d Notice how ascending order is exploited here 47 Explicit vs. Occurrence Representations • What if we want to say: • “The difference between every pair of elements is not equal to a variable d”? • Occurrence Rep: 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 O 0 5 6 7 8 9 Add constraints of the form: • d = 1 → ((O[0] × O[1]) + (O[1] × O[2]) + … = 0) • d = 2 → … 50 Modelling the Golomb Ruler • Objective: Minimise the maximum tick • Minimise(T[n])
Again, exploiting ascending order. 0.. 2n-1 0.. 2n-1 0.. 2n-1 0.. 2n-1 0.. 2n-1 1 3 2 4 n T … 51 Modelling the Golomb Ruler • A Challenge: • Can you see how to model this problem using the occurrence representation? 52 Bounded-cardinality Sets: Occurrence • Given n and s, Find a set of at most n digits that sum to s. 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 O 0 5 6 7 8 9 • Constraints: Sum(O) ≤ n. O[1] + 2O[2] + 3O[3] + … + 9O[9] = s. 55 Decomposing Complex Constraints • Complex constraints can be decomposed to simple constraints, each of which has at most one operator and possibly one equality sign. • For example (A U B) ⊆ (C ∩ D) can be decomposed into three constraints X = A U B Y = C ∩ D X ⊆ Y 56 Set Intersection: Occurrence Rep • Model A ∩ B = C. • A, B are sets of digits (cardinality 5). 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 OA 0 5 6 7 8 9 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 OB 0 5 6 7 8 9 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 OC 0 5 6 7 8 9 Constraint: OC[i] = OA[i] × OB[i] (for each i in 0..9) Sum(OA) = 5 Sum(OB) = 5 Sum? 57 Set Intersection: Explicit Rep • Model A ∩ B = C. • A, B are sets of digits (cardinality 5). 0..9 0..9 0..9 0..9 0..9 1 3 2 4 EA 5 0..9 0..9 0..9 0..9 0..9 1 3 2 4 EB 5 0..9 0..9 0..9 0..9 0..9 1 3 2 4 EC 5 What does the intersection constraint look like? Constraints: EA, EB in ascending order. Sc in non-decreasing order For i in 1..n-1. SC[i+1] ≠0 → EC[i] < EC[i+1] 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 SC 5 60 Subset: Occurrence Rep • Model A ⊆ B. • B is a set of digits (cardinality 5). 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 OA 0 5 6 7 8 9 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 OB 0 5 6 7 8 9 Sum(OB) = 5 Subset constraint: OA[i] ≤ OB[i] (foreach i in 0..9) Sum? 61 Subset: Explicit Rep • Model A ⊆ B. • B is a set of digits (cardinality 5). 0..9 0..9 0..9 0..9 0..9 1 3 2 4 EB 5 0..9 0..9 0..9 0..9 0..9 1 3 2 4 EA 5 What does the subset constraint look like? 0, 1 0, 1 0, 1 0, 1 0, 1 1 3 2 4 SA 5 Constraints: EB in ascending order. SA in non-decreasing order For i in 1..n-1. SA[i+1] ≠0 → EA[i] < EA[i+1] 62 Combined Representations • Obviously, can have combinations of the two representations • For example the intersection of two occurrence representations is an explicit representation. • Constraints on original sets have to be modelled appropriately. 65 Multisets • A collection of distinct objects. • Repetition allowed. • Not arranged in any particular order. • {1, 2, 3, 1}. • {red, green, blue, green, red}. • Characterized by the number of times each object occurs in the multiset • Sometimes referred to as “bags” 66 Finite Domains of Multisets • Let S be a finite set of size n. • Domain “set of S” – comprises every set whose elements are members of S. – this domain is the power set of S and its size is 2n . • Domain “multiset of S” – comprises every finite multiset whose elements are all members of S. – this domain is infinite (if S is non-empty). – If S={1}, multiset of S = { {}, {1}, {1,1}, {1,1,1}, … } • To ensure that “multiset of S” is finite we can bound either the total number of occurrences in the multiset or bound the number of occurrences of each value. 67 Where does the Multiset Pattern Occur? • In many places that the set pattern occurs. For example, in some packing problems (e.g. Vellino’s problem) it is appropriate to view the containers as multisets: Relations 71 Relations • An nary-relation can be seen as – a set of n-tuples, or – Mapping of n-tuples to truth values, that is the characteristic function of the set of tuples. • Aside: Constraints are relations. • Example: • Set P is {Bill, Bert, Tom}. • Binary relation likes on P x P might assign <Bill, Bert> and <Bert, Tom> true (to mean Bill likes Bert and Bert likes Tom), and false to the other combinations. 72 Where Does the Relation Pattern Occur? • Combinatorial Design: • BIBDs (CSPLib 28) • Cellular Frequency Assignment • Assignment of frequencies to transmitters (see Van Hentenryck ’99) • Rostering • Assignment of staff to shifts. 75 Relations: Projection • Find a relation R between sets A = {1, 2, 3} and B = {2, 3, 4} such that each element of A is related to at least one element of B.” • So projection of R onto each element of A has size at least 1. 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 0, 1 1 2 3 3 4 2 A B Constraint: Summation on the columns 76 Relations: Occurrence Representation • What about k-ary relations? k-dimensional matrices 0/1 77 Relations: Other Representations • Consider binary case, A × B. (A = {1, 2, 3}, B = {2, 3, 4}). • Introduce a matrix indexed by elements of A and by 1 .. |B|: {0,2,3,4} {0,2,3,4} {0,2,3,4} {0,2,3,4} {0,2,3,4} {0,2,3,4} {0,2,3,4} {0,2,3,4} {0,2,3,4} 1 2 3 A 1 2 3 Why did I add 0? Do I need to add constraints? 1..|B| 80 BIBD: Specification • Given v, b, r, k, λ : positive integers • Let objects be a set of size v and blocks be a set of size b • Find BIBD, a relation on objects X blocks • Such that – ∀(block in blocks) (Σ(object in objects) <object,block> ∈ BIBD) = k – ∀(object in objects) (Σ(block in blocks) <object,block> ∈ BIBD) = r – ∀(o1 in objects) ∀(o2 in objects) o1 ≠ o2 → (Σ(block in blocks) <o1,block> ∈ BIBD ∧ <o2,block> ∈ BIBD) = λ 81 BIBD: Occurrence Model • Constraints can now be stated easily on rows and columns. 0/1 Blocks Objects 82 BIBD: Occurrence Model • Each block contains k distinct objects. – ∀(block in blocks) (Σ(object in objects) <object,block> ∈ BIBD) = k • Concrete constraint: Each column sums to k 0/1 0/1 0/1 0/1 0/1 0/1 0/1 Blocks Objects 85 BIBD: Example • 〈7, 7, 3, 3, 1〉 0 0 0 0 1 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 1 Blocks Objects … but did we introduce equivalence classes of assignments here? Functions 87 Functions • A function f is a binary relation on two sets:" • a domain and a codomain." • Has the property that each element of the domain is related to at most one element of the codomain:" • its image." • We write f(x) = y to mean that the image of x under the function f is y." a b c d 1 2 3 4 domain! codomain! 90 Where Does the Function Pattern Occur? • Warehouse Location. • Find a function from stores to warehouses to indicate which store is supplied by which warehouse. 91 Total Functions: Explicit • In a total function, every element of the domain has an image in the codomain. • Use a variable per element of the domain, domain of each is the codomain. a b c d 1 2 3 domain! codomain! 1,2,3 1,2,3 1,2,3 1,2,3 a b c TotalFn d 92 Total Functions: Occurrence • In a total function, every element of the domain has an image in the codomain. • Use a 2d array of 0/1 variables, indexed by the domain and codomain. a b c d 1 2 3 domain! codomain! 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 a b c TotalFn d 1 2 3 Sum of each col is 1 95 Injections: Explicit • The images of two distinct elements of the domain under an injective function are distinct" • If total, just need to add a constraint to our basic model of a function: allDifferent(injection)" a b c d 1 2 3 4 domain! codomain! 1,2,3,4 1,2,3,4 1,2,3,4 1,2,3,4 a b c Injection d 96 Injections: Occurrence • The images of two distinct elements of the domain under an injective function are distinct" • If total, just need to add constraints to our basic model of a function:" a b c d 1 2 3 4 domain! codomain! Injection 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 a b c d 1 2 3 Sum of each col: 1 Sum of each row: ≤1 4 97 Partial Injections • The images of two distinct elements of the domain under an injective function are distinct" • Explicit model is messy, occurrence model is easy:" a b c d 1 2 3 4 domain! codomain! Injection 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 a b c d 1 2 3 Sum of each col: ≤1 Sum of each row: ≤1 4