Non-binary constraints Toby Walsh Cork Constraint Computation Center [email protected].

92
Non-binary constraints Toby Walsh Cork Constraint Computation Center [email protected]

Transcript of Non-binary constraints Toby Walsh Cork Constraint Computation Center [email protected].

Page 1: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Non-binary constraints

Toby Walsh

Cork Constraint Computation Center

[email protected]

Page 2: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Global constraints

Toby Walsh

Cork Constraint Computation Center

[email protected]

Page 3: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Non-exhaustive catalog

Order constraints Constraints on values Partitioning constraints Timetabling constraints Graph constraints Scheduling constraints Bin-packing constraints

Page 4: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Global constraints

It isn’t just all-different!

Many constraints specialized to application domains Scheduling Packing ..

Page 5: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Order constraints

min(X,[Y1,..,Yn]) and max(X,[Y1,..Yn])X <= minimum(Y1,..,Yn)X >= maximum(Y1,..Yn)n.b. min is relational not functional

Page 6: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Order constraints

min(X,[Y1,..,Yn]) and max(X,[Y1,..Yn])

X <= minimum(Y1,..,Yn)

X <= maximum(Y1,..Yn)

n.b. min is relational not functional

min_mod(X,[Y1,..,Yn],m) and max_mod(X,[Y1,..Yn],m)

X mod m <= minimum(Y1 mod m,..,Yn mod m)

X mod m <= maximum(Y1 mod m,..,Yn mod m)

Page 7: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 8: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

min_n(X,1,[Y1,..,Ym]) => min(X,[Y1,..,Yn])max_n(X,1,[Y1,..,Ym]) => max(X,[Y1,..,Ym])

n.b. min_n is functional but min is relational (any X smaller than min will do!

Page 9: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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])

Page 10: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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 =,<,>,=/,<= or >=

relation “Yi op X” holds n times

E.g. among(n,[Y1,..,Ym],[k]) = count(n,[Y1,..,Ym],=,k)

Page 11: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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])

Page 12: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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])

Page 13: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 14: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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])

Page 15: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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])

Page 16: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Value constraints

same([X1,..,Xn],[Y1,..,Yn])Yi is a permutation of Xi

Page 17: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 18: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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])

Page 19: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Partitioning constraints

all-different([X1,..,Xn])

Page 20: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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 ..

Page 21: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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..

Page 22: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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.. Regin has proposed very efficient algorithm

Page 23: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Partitioning constraints

nvalue(N,[X1,..,Xn])

Xi takes N different values

all-different([X1,..,Xn]) = nvalue(n,[X1,..,Xn)

Page 24: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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)

Page 25: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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, …

Page 26: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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, …

Page 27: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

Tours in graph a often represented by the successors:

[X1,..,Xn] means from node i we go to node Xi

Page 28: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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)

Page 29: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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)

Page 30: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

derangement(N,[X1,..,Xn])

there are no length 1 cycles in Xi

Page 31: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

derangement(N,[X1,..,Xn])

there are no length 1 cycles in Xi

e.g. derangement([2,1,5,3,4]) as the 2 cycles

(1)->(2)->(1) and (3)->(5)->(4)->(3) have length 2

and 3

Page 32: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

derangement(N,[X1,..,Xn])

there are no length 1 cycles in Xi

e.g. derangement([2,1,5,3,4]) as the 2 cycles

(1)->(2)->(1) and (3)->(5)->(4)->(3) have length 2

and 3

e.g. derangement([3,1,2,4]) as the 2 cycles (1)->(3)->(2)->(1) and (4)->(4) are of length 3 and 1

Page 33: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

We may also wish to represent predecessors For good labelling heuristics For ease of statement of constraints

[X1,..,Xn] means we came to node i from node Xi

Page 34: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

We may also wish to represent predecessors For good labelling heuristics For ease of statement of constraints

[X1,..,Xn] means we came to node i from node Xi E.g. [2,1,4,5,3] now represents the 2 cycles

(1)->(2)->(1) and (3)->(5)->(4)->(3)

Page 35: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

We may also wish to represent predecessors For good labelling heuristics For ease of statement of constraints

[X1,..,Xn] means we came to node i from node Xi E.g. [2,1,4,5,3] now represents the 2 cycles

(1)->(2)->(1) and (3)->(5)->(4)->(3) In successor notation this was [2,1,5,3,4]

Page 36: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

We may also wish to represent predecessors For good labelling heuristics For ease of statement of constraints

[X1,..,Xn] means we came to node i from node Xi E.g. [2,1,4,5,3] now represents the 2 cycles

(1)->(2)->(1) and (3)->(5)->(4)->(3) In successor notation this was [2,1,5,3,4]

How do we know they are the same?

Page 37: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

We may also wish to represent predecessors For good labelling heuristics For ease of statement of constraints

[X1,..,Xn] means we came to node i from node Xi E.g. [2,1,4,5,3] now represents the 2 cycles

(1)->(2)->(1) and (3)->(5)->(4)->(3) In successor notation this was [2,1,5,3,4]

How do we know they are the same? With a channelling constraint!

Page 38: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

Inverse([X1,..,Xn],[Y1,..,Yn])

Where [X1,..,Xn] is a successor representation of a path, and [Y1,..,Yn] is a predecessor

Thus inverse([2,1,4,5,3],[2,1,5,3,4])

Page 39: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Graph constraints

Inverse([X1,..,Xn],[Y1,..,Yn])

Where [X1,..,Xn] is a successor representation of a path, and [Y1,..,Yn] is a predecessor

Thus inverse([2,1,4,5,3],[2,1,5,3,4])

And inverse([2,1,5,3,4],[2,1,4,5,3])

Page 40: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 41: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 42: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 43: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 44: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 45: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 46: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Bin-packing constraints

bin_packing(capacity,[B1,..,Bn],[w1,..,wn])

for each bin j, sum_Bi=j wi <= capacity

Page 47: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

Page 48: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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

unlike normal bin-packing, does not minimize the number of open bins

Page 49: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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!

unlike normal bin-packing, does not minimize the number of open bins

how could we do this?

Page 50: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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!

unlike normal bin-packing, does not minimize the number of open bins

Bi=j implies Open_j=1

Minimize sum_j Open_j

Page 51: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

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)

Page 52: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC algorithms

Page 53: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC-Schema

A generic framework for achieving AC for any kind of constraint (can be non binary).Bessiere and Regin, IJCAI’97

You just have to say how to compute a solution. Works incrementality (notion of support).

Page 54: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC-Schema: instantiation

List of allowed tuples List of forbidden tuples Predicates Any OR algorithm Solver re-entrace

Page 55: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC-Schema

Idea: tuple = solution of the constraintsupport = valid tuple- while the tuple is valid: do nothing

- if the tuple is no longer valid, then search for a new support for the values it contains

a solution (support) can be computed by any algorithm

Page 56: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Example

X(C)={x1,x2,x3} D(xi)={a,b} T(C)={(a,a,a),(a,b,b),(b,b,a),(b,b,b)}

Page 57: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Example

X(C)={x1,x2,x3} D(xi)={a,b} T(C)={(a,a,a),(a,b,b),(b,b,a),(b,b,b)} Support for (x1,a): (a,a,a) is computed and (a,a,a) is added to S(x2,a) and

S(x3,a), (x1,a) in (a,a,a) is marked as supported.

Page 58: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Example

X(C)={x1,x2,x3} D(xi)={a,b} T(C)={(a,a,a),(a,b,b),(b,b,a),(b,b,b)} Support for (x1,a): (a,a,a) is computed and (a,a,a) is added to S(x2,a) and

S(x3,a), (x1,a) in (a,a,a) is marked as supported. Support for (x2,a): (a,a,a) is in S(x2,a) it is valid, therefore it is a support.

(Multidirectionnality). No need to compute a solution

Page 59: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Example

X(C)={x1,x2,x3} D(xi)={a,b} T(C)={(a,a,a),(a,b,b),(b,b,a),(b,b,b)} Support for (x1,a): (a,a,a) is computed and (a,a,a) is added to S(x2,a) and

S(x3,a), (x1,a) in (a,a,a) is marked as supported. Value a is removed from x1, then all the tuples in S(x1,a) are no longer valid:

(a,a,a) for instance. The validity of the values supported by this tuple must be reconsidered.

Page 60: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Example

X(C)={x1,x2,x3} D(xi)={a,b} T(C)={(a,a,a),(a,b,b),(b,b,a),(b,b,b)} Support for (x1,a): (a,a,a) is computed and (a,a,a) is added to S(x2,a) and

S(x3,a), (x1,a) in (a,a,a) is marked as supported. Support for (x1,b): (b,b,a) is computed, and updated ...

Page 61: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC-Schema: complexity

CC complexity to check consistency (seek in table, call to OR algorithm): seek for a Support costs CC

n variables, d values:for each value: CCfor all values: O(ndCC)

For any OR algorithm which is able to compute a solution, Arc consistency can be achieved in O(ndCC).

Page 62: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Table Constraint: An example Configuration problem:

5 types of components: {glass, plastic, steel, wood, copper}3 types of bins: {red, blue, green} whose capacity is red 5, blue 5, green 6Constraints:- red can contain glass, cooper, wood- blue can contain glass, steel, cooper- green can contain plastic, copper, wood- wood require plastic; glass exclusive copper- red contains at most 1 of wood- green contains at most 2 of woodFor all the bins there is either no plastic or at least 2 plasticGiven an initial supply of 12 of glass, 10 of plastic, 8 of steel, 12 of wood and 8 of copper; what is the minimum total number of bins?

Page 63: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Table Constraint: results

#bk timestandard model 1,361,709 430Table Constraint 12,659 9.7

Page 64: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Semantics of a constraint

Speed-up the search for a support (solution which contain a value (x,a))

Page 65: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Exploiting constraint semantics

Speed-up the search for a support (solution which contain a value (x,a)):x < y, D(x)=[0..10000], D(y)=[0..10000]support for (x,9000): immediate any value greater than 9000 in D(y)

Page 66: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Semantics of a constraint

Design of ad-hoc filtering algorithm:x < y : (a) max(x) = max(y) -1(b) min(y) = min(x) +1

Page 67: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Exploiting semantics

Design of ad-hoc filtering algorithm:x < y : (a) max(x) = max(y) -1(b) min(y) = min(x) +1

Triggering of the filtering algorithm:no possible pruning of D(x) while max(y) is not modified

no possible pruning of D(y) while min(x) is not modified

Page 68: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Regin’s alldiff algorithm

Construct the value graph

Page 69: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

The value graph:

D(x1)={1,2}D(x2)={2,3}D(x3)={1,3}D(x4)={3,4}D(x5)={2,4,5,6}D(x6)={5,6,7}

Page 70: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Alldiff constraint

Construct a flow network

Page 71: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Flow network

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

Default orientation

Page 72: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Flow network

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

t s

Default orientation

Page 73: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Flow network

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

t s

Default orientation

Page 74: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Flow network

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

t s

(6,6)

(0,1)

(0,1)

(1,1)

Default orientation

Page 75: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Alldiff constraint

Construct a flow network Compute a feasible flow

Page 76: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Flow network

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

t s

(6,6)

(0,1)

(0,1)

(1,1)

Default orientation

Page 77: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

A feasible flow

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

t s

(6,6)

(0,1)

(0,1)

(1,1)

Default orientation

Page 78: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Alldiff constraint

Construct a flow network Compute a feasible flow Construct the residual graph

Reverse direction of arcs on feasible flow (that is, of red arcs)

Page 79: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

A feasible flow

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

t s

(6,6)

(0,1)

(0,1)

(1,1)

Default orientation

Page 80: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Residual graph

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

orientation

Page 81: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Alldiff constraint

Construct a flow network Compute a feasible flow Construct the residual graph Compute the strongly connected components

Strongly connected component = maximal subgraph such that if directed path u to v, also from v to u

Page 82: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Alldiff constraint

Construct a flow network Compute a feasible flow Construct the residual graph Compute the strongly connected components

Strongly connected component = maximal subgraph such that if directed path u to v, also from v to u

Exists O(N) algorithm due to Tarjan (1972) = O(n+d)

Page 83: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Residual graph

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

orientation

Page 84: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Residual graph

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

orientation

Page 85: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Alldiff constraint

Construct a flow network Compute a feasible flow Construct the residual graph Compute the strongly connected components

Remove arcs of flow value 0 (i.e. black arcs) with ends belonging to two different components

Page 86: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Residual graph

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

orientation

Page 87: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Residual graph

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

orientation

Page 88: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC for all-different

TheoremAn arc is deleted from residual graph iff the associated

value is not GAC

Page 89: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Residual graph

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

s

orientation

Page 90: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

GAC subproblem

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

The value graph:

D(x1)={1,2}D(x2)={2,3}D(x3)={1,3}D(x4)={4}D(x5)={5,6}D(x6)={5,6,7}

Page 91: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Before!

x1

x2

x3

x4

x5

x6

1

2

3

4

5

6

7

The value graph:

D(x1)={1,2}D(x2)={2,3}D(x3)={1,3}D(x4)={3,4}D(x5)={2,4,5,6}D(x6)={5,6,7}

Page 92: Non-binary constraints Toby Walsh Cork Constraint Computation Center tw@4c.ucc.ie.

Conclusions

Vast range of global constraints Cover all the areas that CP cover Some very generic (e.g. all-different) Some very specific (coloured cummulative ..)

GAC algorithms Generic (GAC-schema) Specific (GAC alldifferent)