global constraints: generalised arc consistency

68
1 Global Constraints: Generalised Arc Consistency It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling of a problem Exploitation of specialised algorithms that take the semantics of the constraint for efficient propagation, achieving generalised arc consistency. The generalised arc consistency (GAC) criterion sees that no value remains in the domain of a variable with no support in values of each of the other variables participating in the “global” constraint. Example: all_diff ([A 1 , A 2 , ..., A n ] Constrain a set of n variables to be all different among themselves

Upload: fahim

Post on 23-Jan-2016

61 views

Category:

Documents


0 download

DESCRIPTION

Global Constraints: Generalised Arc Consistency. It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling of a problem - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Global Constraints: Generalised Arc Consistency

1

Global Constraints: Generalised Arc Consistency

• It is often important to define n-ary “global” constraints, for at least

two reasons

• Ease the modelling of a problem

• Exploitation of specialised algorithms that take the semantics of

the constraint for efficient propagation, achieving generalised arc

consistency.

• The generalised arc consistency (GAC) criterion sees that no value

remains in the domain of a variable with no support in values of each

of the other variables participating in the “global” constraint.

Example: all_diff ([A1, A2, ..., An]

Constrain a set of n variables to be all different among themselves

Page 2: Global Constraints: Generalised Arc Consistency

2

Global Constraints: all_diff

• The constraint definition based on binary difference constraints ()

does not pose any problems as much as modelling is concerned. For

example, it may be being defined recursively in CLP.

• However, constraint propagation based on binary constraints alone

does not provide in general much propagation.

• As seen before, arc consistency is not any better than node

consistency, and higher levels of consistency are in general too

costly and do not take into account the semantics of the all_different

constraint.

one_diff(_,[]).one_diff(X,[H|T]):-

X #\= H, one_diff(X,T).

all_diff([]).all_diff([H|T]):-

one_diff(H,T),all_diff(T).

Page 3: Global Constraints: Generalised Arc Consistency

3

Global Constraints: all_diff

Example:

X1: 1,2,3 X6: 1,2,3,4,5,6,7,8,9

X2: 1,2,3,4,5,6 X7: 1,2,3,4,5,6,7,8,9

X3: 1,2,3,4,5,6,7,8,9 X8: 1,2,3

X4: 1,2,3,4,5,6 X9: 1,2,3,4,5,6

X5: 1,2,3

• It is clear that constraint propagation based on maintenance of node-,

arc- or even path-consistency would not eliminate any redundant

label.

• Yet, it is very easy to infer such elimination with a global view of the

constraint!

Page 4: Global Constraints: Generalised Arc Consistency

4

Global Constraints: all_diff

• Variables X1, X5 and X8 may only take values 1, 2 and 3. Since there

are 3 values for 3 variables, these must be assigned these values

which must then be removed from the domain of the other variables.

• Now, variables X2, X4 and X9 may only take values 4, 5 e 6, that

must be removed from the other variables domains.

X1: 1,2,3 X2: 1,2,3,4,5,6 X3: 1,2,3,4,5,6,7,8,9

X4: 1,2,3,4,5,6 X5: 1,2,3 X6: 1,2,3,4,5,6,7,8,9

X7: 1,2,3,4,5,6,7,8,9 X8: 1,2,3 X9: 1,2,3,4,5,6

X1: 1,2,3 X2: 1,2,3,4,5,6 X3: 1,2,3,4,5,6,7,8,9

X4: 1,2,3,4,5,6 X5: 1,2,3 X6: 1,2,3,4,5,6,7,8,9

X7: 1,2,3,4,5,6,7,8,9 X8: 1,2,3 X9: 1,2,3,4,5,6

Page 5: Global Constraints: Generalised Arc Consistency

5

Global Constraints: all_diff

• In this case, these prunings could be obtained, by maintaining

(strong) 4-consistency.

• For example, analysing variables X1, X2, X5 and X8, it would be

“easy” to verify that from the d4 potential assignments of values to

them, no assignment would include X2=1, X2=2, nor X2=3, thus

leading to the prunning of X2 domain.

• However, such maintenance is usually very expensive,

computationally. For each combination of 4 variables, d4 tuples

whould be checked, with complexity O(d4).

• In fact, in some cases, n-strong consistency would be required, so its

naïf maintenance would be exponential on the number of variables,

exactly what one would like to avoid in search!

Page 6: Global Constraints: Generalised Arc Consistency

6

Global Constraints: all_diff

• However, taking the semantics of this constraint into account, an

algorithm based on quite a different approach allows the prunings to

be made at a much lesser cost, achieving generalised arc

consistency.

• Such algorithm (see [Regi94]), is grounded on graph theory, and

uses the notion of graph matching.

• To begin with, a bipartite graph is associated to an all_diff

constraints. The nodes of the graphs are the variables and all the

values in their domains, and the arcs associate each variable with the

values in its domain.

• In polinomial time, it is possible to eliminate, from the graph, all arcs

that do not correspond to possible assignments of the variables.

Page 7: Global Constraints: Generalised Arc Consistency

7

Global Constraints: all_diff

Key Ideas:

• For each variable-value pair, there is an arc in the bipartite graph.

• A matching, corresponds to a subset of arcs that link some variable

nodes to value nodes, different variables being connected to different

values.

• A maximal matching is a matching that includes all the variable

nodes.

• For any solution of the all_diff constraint there is one and only one

maximal matching.

Page 8: Global Constraints: Generalised Arc Consistency

8

Global Constraints: all_diff

Example: A,B:: 1..2, C:: 1..3, D:: 2..5, E:: 3..6,

all_diff([A,B,C,D,E]).

A

B

C

D

E

1

2

3

4

5

6

A = 1

B = 2

C = 3

D = 4

E = 5

Maximal Matching

Page 9: Global Constraints: Generalised Arc Consistency

9

Global Constraints: all_diff

• The propagation (domain filtering) is done according to the following

principles:

1.If an arc does not belong to any maximal matching, then it does

not belong to any all_diff solution.

2.Once determined some maximal matching, it is possible to

determine whether an arc belongs or not to any maximal matching.

3.This is because, given a maximal matching, an arc belongs to any

maximal matching iff it belongs:

a)To an alternating cycle; or

b)To an even alternating path, starting at a free node.

Page 10: Global Constraints: Generalised Arc Consistency

10

Global Constraints: all_diff

Example: For the maximal matching (MM) shown

• 6 is a free node;

• 6-E-5-D-4 is an even alternating path,

alternating arcs from the MM (E-5, D-4) with

arcs not in the MM (D-5, E-6);

• A-1-B-2-A is an alternating cycle;

• E-3 does not belong to any alternating cycle

• E-3 does not belong to any even alternating

path starting in a free node (6)

• E-3 may be filtered out!

A

B

C

D

E

1

2

3

4

5

6

Page 11: Global Constraints: Generalised Arc Consistency

11

Global Constraints: all_diff

• Compaction

– Before this analysis, the graph may be “compacted”, aggregating,

into a single node, “equivalent nodes”, i.e. those belonging to

alternating cycles.

– Intuitively, for any solution involving these variables and values, a

different solution may be obtained by permutation of the

corresponding assignments.

– Hence, the filtering analysis may be made based on any of these

solutions, hence the set of nodes can be grouped in a single one.

Page 12: Global Constraints: Generalised Arc Consistency

12

Global Constraints: all_diff

• A-1-B-2-A is an alternating cycle;

• By permutation of variables A and B, the

solution <A,B,C,D,E> = <1,2,3,4,5>

becomes <A,B,C,D,E> = <2,1,3,4,5>

• Hence, nodes A e B, as well as nodes 1

and 2 may be grouped together (as may the

nodes D/E and 4/5).

A/B

C

4/5

3

6

D/E

1/2

A

B

C

D

E

1

2

3

4

5

6

With these grouping the graph

becomes much more compact

Page 13: Global Constraints: Generalised Arc Consistency

13

Global Constraints: all_diff

Analysis of the compacted graph shows that

A/B

C

4/5

3

6

D/E

1/2

A/B

C

4/5

3

6

D/E

1/2

• Arc D/E - 3 may be filtered out

(notice that despite belonging to

cycle D/E - 3 - C - 1/2 - D/E, this

cycle is not alternating.

• Arcs D/E - 1/2 and C - 1/2 may

also be filtered.

The compact graph may thus be

further simplified to

Page 14: Global Constraints: Generalised Arc Consistency

14

Global Constraints: all_diff

By expanding back the simplified compact graph, one gets the graph on

the right that

A/B

C

4/5

3

6

D/E

1/2

Which immediately sets C=3 and, more generaly, filters the initial domains to

A,B :: 1..2, C:: 1,2,3, D:: 2,3,4,5, E:: 3,4,5,6

A

B

C

D

E

1

2

3

4

5

6

Page 15: Global Constraints: Generalised Arc Consistency

15

Global Constraints: all_diff

• Upon elimination of some labels (arcs), possibly due to other

constraints, the all_diff constraint propagates such prunings,

incrementally. There are 3 situations to consider:

1. Elimination of a vital arc (the only arc connecting a variable

node with a value node): The constraint cannot be satisfied.

A

B

C

D

E

1

2

3

4

5

6

A

B

C

D

E

1

2

3

4

5

6

?

Page 16: Global Constraints: Generalised Arc Consistency

16

Global Constraints: all_diff

2. Elimination of a non-vital arc which is a member to the maximal

matching

– Determine a new maximal matching and restart from there.

A

B

C

D

E

1

2

3

4

5

6

A

B

C

D

E

1

2

3

4

5

6

Arc A-4 does not belong to the even alternating path started in node 6.

D-5 also leaves this path, but it still belongs to an alternating cycle.

Page 17: Global Constraints: Generalised Arc Consistency

17

Global Constraints: all_diff

3. Elimination of a non-vital arc which is not a member to the maximal

matching

– Eliminate the arcs that do not belong any more to an alternating

cycle or path.

A new maximal matching includes arcs D-5 and E-6. In this matching, arcs

E-4 and E-5 do not belong to even alternating paths or alternating cycles.

A

B

C

D

E

1

2

3

4

5

6

A

B

C

D

E

1

2

3

4

5

6

Page 18: Global Constraints: Generalised Arc Consistency

18

Global Constraints: all_diff

Time Complexity:

Assuming n variables, each of which with d values, and where D is

the cardinality of the union of all domains,

1. It is possible to obtain a maximal matching with an algorithm of

time complexity O(dnn).

2. Arcs that do not belong to any maximal matching may be

removed with time complexity O( dn+n+D).

3. Taking into account these results, we obtain complexity of

O(dn+n+D+dnn). Since D < dn, the total time complexity of the

algorithm is dominated by the last term, thus becoming

O(dnn).

which is much better than the poor result with a naïf analysis.

Page 19: Global Constraints: Generalised Arc Consistency

19

Global Constraints: all_diff

Availability:

1. The all_diff constraint first appeared in the CHIP system (algorithm?).

2. The described implementation is incorporated into the ILOG system,

and avalable as primitive IlcAllDiff.

3. This algorithm is also implemented in SICStus, through buit-in

constraint all_distinct/1.

4. Other versions of the constraint, namely all _different/2, are also

available, possibly using a faster algorithm but with less pruning,

where the 2nd argument controls the available pruning options.

Page 20: Global Constraints: Generalised Arc Consistency

20

Global Constraints: all_diff

Availability:

1. The all_diff constraint first appeared in the CHIP system (algorithm?).

2. The described implementation is incorporated into the ILOG system,

and avalable as primitive IlcAllDiff.

3. This algorithm is also implemented in SICStus, through buit-in

constraint all_distinct/1.

4. Other versions of the constraint, namely all _different/2, are also

available, possibly using a faster algorithm but with less pruning,

where the 2nd argument controls the available pruning options.

Page 21: Global Constraints: Generalised Arc Consistency

21

Global Constraints: all_diff

Example: Sudoku

• Cuts in green are all that are found by maïve all_diff.

• Global all_diff finds all values without backtracking.

• The first cuts are illustrated in the figure.

(where the indices show a possible order in which the cuts are made)

sudoku_cp.pl

Files vh1 e vh2

8 4 6 4 3 5

6 12 37 7 1 8

3 9 8 6

2 1 12 258 6 9 347 5 7

9 4 2 258 6 6 347 5 1

5 258 6 1 347 5 3

6 7 1 1 378 9 2 4

4 2 11 6 378 9 9 3

13 10 8 9 37 7 4 8 5 6 237 10127 10

Page 22: Global Constraints: Generalised Arc Consistency

22

Global Constraints: Assignment

• The all_diff constraint is typically applicable to problems where it is

intended that different tasks are executed by different agents (or use

different resources).

• However, tasks and resources are treated differently. Some are

variables and the others the domains of these variables. For

example, denoting 4 tasks/resources by variablesTi / Rj, one would

have to chose either one of the specifications below

T1 in 1..3, T2 in 2..4, T3 in 1..4, T4 in 1..3.

or

R1 in {1,3,4}, R2 in 1..4, R3 in 1..4, R4 in {2,3}

• Hence, other constraints could be specified either on tasks or on

resources, but not easily involving both types of objects

Page 23: Global Constraints: Generalised Arc Consistency

23

Global Constraints: Assignment

• Such “unfairness” may be overcome by treating both tasks and

resources in a similar way, namely modelling both with distinct

variables.

• These variables still have to adhere to the constraint that different

tasks are performed in different resources.

• Also, if a task j is assigned to resource i, then resource i is assigned

to task j. Denoting tasks by Tj and resources by Ri, the following

condition must stand for any i, j 1..n

Ri = j Tj = i

• This is the goal of global constraint assignment/2, available in

SICStus, that uses the same propagation technique of all_diff.

Page 24: Global Constraints: Generalised Arc Consistency

24

Global Constraints: Assignment

Example:

A1:1..2, A2::1..2, A3::1..3, A4::2..5, A5:: 3..5,

B1:1..3, B2::1..4, B3::3..5, B4::4..5, B5:: 4..5,

assignment([A1,A2,A3,A4,A5], [B1,B2,B3,B4,B5]).

A = 1

B = 2

C = 3

D = 4

E = 5

A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

maximalmatching

Page 25: Global Constraints: Generalised Arc Consistency

25

Global Constraints: Assignment

Since the assignment constraint imposes a maximal matching in the

bipartite graph of tasks Ai and Resources Bj and resources, the same

filtering techniques of the all_diff constraint can be used. Hence the initial

domains are are filtered to

A1:1..2, A2::1..2, A3:: 3 , A4::4..5, A5:: 4..5.

B1:1..2, B2::1..2, B3:: 3 , B4::4..5, B5:: 4..5.

A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

Page 26: Global Constraints: Generalised Arc Consistency

26

Global Constraints: Circuit

• The previous global constraints may be regarded as imposing a

certain “permutation” on the variables.

• In many problems, such permutation is not a sufficient constraint. It is

necessary to impose a certain “ordering” of the variables.

• A typical situation occurs when there is a sequencing of tasks, with

precedences between tasks, possibly with non-adjacency constraints

between some of them.

• In these situations, in addition to the permutation of the variables,

one must ensure that the ordering of the tasks makes a single cycle,

i.e. there must be no sub-cycles.

Page 27: Global Constraints: Generalised Arc Consistency

27

Global Constraints: Circuit

• These problems may be described by means of directed graphs,

whose nodes represent tasks and the directed arcs represent

precedences.

• The arcs may even be labelled by “features” of the precedences,

namely transition times.

• This is a situation typical of several problems of the travelling

salesman type.

A B

C D

A B

C D

2

3

4 5 6

9

8

7

2

2

2

Page 28: Global Constraints: Generalised Arc Consistency

28

Global Constraints: Circuit

• Filtering: For these type of problems, the arcs that do not belong to

any hamiltonian circuit should be eliminated.

• In the graph, it is easy to check that the only possible circuits are

A->B->D->C->A and A->C->D->B->A. Certain arcs (e.g. B->C,

B->B, ...), may not belong to any hamiltonian circuit and can be

safely pruned.

A B

C D

A B

C D

Page 29: Global Constraints: Generalised Arc Consistency

29

Global Constraints: Circuit

• The pruning of the arcs that do not belong to any circuit is the goal of the global constraint circuit/1, available in SICStus.

• This constraint is applicable to a list of domain variables, where the domain of each corresponds to the arcs connecting that variable to other variables, denoted by the order in which they appear in the list.

For example:

A in 2..3, B in 1..4,

C in 1..4, D in 2..3,

circuit([A,B,C,D]).

A/1 B/2

C/3 D/4

A=2

A=3

Page 30: Global Constraints: Generalised Arc Consistency

30

Global Constraints: Circuit

• Global constraint circuit/1 incrementally achieves the pruning of the

arcs not in any hamiltonian circuit. For example, posing

A, D in 2..3, B in 1,2,3,4, C in 1,2,3,4,circuit([A,B,C,D]).

The following prunning is achieved

A in 2..3, B in 1,2,3,4, C in 1,2,3,4, D in 2..3,

since the possible solutions are

[A,B,C,D] = [2,4,3,1] and [A,B,C,D] = [3,1,4,2]

A B

C D

A B

C D

Page 31: Global Constraints: Generalised Arc Consistency

31

Global Constraints: Element

For example, the value X from the arc that

leaves node A, depends on the arc chosen:

if A = 2 then X = 3,

if A = 3 then X = 4;

otherwise X = undefined

A/1 B/2

C/3 D/4

2

3

4 5 6

9

8

7

2

2

2

The disjunction implicit in this definition raises, as well known, problems

of efficiency to constraint propagation.

Often a variable not only has its values constrained by the values of other

variables, but it is actually defined conditionally in function of these values.

Page 32: Global Constraints: Generalised Arc Consistency

32

Global Constraints: Element

In fact, the value of X may only be known upon labelling of variable A.

Until then, a naïf handling of this type of conditoinal constraint would

infer very little from it.

A/1 B/2

C/3 D/4

2

3

4 5 6

9

8

7

2

2

2

However, if other problem constraints impose, for example, X < 4, an

efficient handling of this constraint would impose

not only X = 3 but also A = 2.

if A = 2 then X = 3,

if A = 3 then X = 4;

otherwise X = undefined

Page 33: Global Constraints: Generalised Arc Consistency

33

Global Constraints: Element

• The efficient handling of this type of disjunctions is the goal of global

constraint element/3, available in SICStus and CHIP.

element(X, [V1,V2,...,Vn], V)

• In this constraint, X is a variable with domain 1..n, and both V and

the Vis are either finite domain constraints or constants. The

semantics of the constraint can be expressed as the equivalence

X = i V = Vi

• From a propagation viewpoint, this constraint imposes arc

consistency in X and bounds consistency in V. It is particularly

optimised for when all Vis are ground.

Page 34: Global Constraints: Generalised Arc Consistency

34

Global Constraints: Element & Circuit

• Global constraints may be used together. In particular, constraints

element and circuit may implement the travelling salesman

(satisfaction problem (travelling.pl): For some graph, determine an

hamiltonian circuits whose length does not exceed Max (say 20).

circ([A,B,C,D], Max, Cost):- A in 2..3, B in 1..4, C in {1}\/{3,4}, D in 2..3, circuit([A,B,C,D]), element(A,[_,3,4,_],Ca), element(B,[2,2,5,6],Cb), element(C,[2,_,2,9],Cc), element(D,[_,8,7,_],Cd), Cost #= Ca+Cb+Cc+Cd, Cost #=< Max, labeling([],[A,B,C,D]).

A/1 B/2

C/3 D/4

2

3

4 5 6

9

8

7

2

2

2

Page 35: Global Constraints: Generalised Arc Consistency

35

Global Constraints: Cycle

• Sometimes, the goal is to form more than one (1) circuit connecting

all the nodes.

• This situation corresponds to the classical problem of the Multiple

Traveling Salesman, that is highly suitable to model several

practical applications.

• For example, the case where a number of petrol stations has to be

serviced by a number N of tankers.

• For such type of applications, CHIP extended the global constraint

circuit/1, to another cycle/2, where the first argument is a domain

variable of an integer to account for the number of cycles.

Note: Constraint circuit(L) is implemented in CHIP as cycle(1,L).

Page 36: Global Constraints: Generalised Arc Consistency

36

Global Constraints: Cycle

Example:

X1::[1,3,4], X2::[1,3] , X3::[2,5,6]

X4::[2,5] , X5::[6] , X6::[4,5]

?- N::[1,2,3], X = [X1,X2,X3,X4,X5,X6], cycle(N,X).

N = 1 N = 2 N = 3

X =[3,1,5,2,6,4]X=[3,2,1,5,6,4] X=[3,1,5,2,6,4]

2 4 6

1 3 5

2 4 6

1 3 5

2 4 6

1 3 5

2 4 6

1 3 5

Page 37: Global Constraints: Generalised Arc Consistency

37

Global Constraints: Global Cardinality

• Many scheduling and timetabling problems, have quantitative

requirements of the type

in these N “slots” M must be of type T

• This type of constraints may be formulated with a cardinality

constraint. In some sistems, these cardinality constraints are given

as built-in, or may be implemented through reified constraints.

• In particular, in SICStus the built-in constraint count/4 may be used

to “count” elements in a list, which replaces some uses of the

cardinality constraints (see ahead).

• However, cardinality may be more efficiently propagated if

considered globally.

Page 38: Global Constraints: Generalised Arc Consistency

38

Global Constraints: Global Cardinality

• For example, assume a team of 7 people (nurses) where one or two

must be assigned the morning shift (m), one or two the afternoon

shift (a), one the night shift (n), while the others may be on holliday

(h) or stay in reserve (r).

• To model this problems, let us consider a list L, whose variables Li

corresponding to the 7 people available, may take values in domain

{m, a, n, h, r} (or {1, 2, 3, 4, 5} in languages like SICSTus that require

domains to range over integers).

• Both in SICStus or in CHIP this complex constraint may be

decomposed in several cardinality constraints.

Page 39: Global Constraints: Generalised Arc Consistency

39

Global Constraints: Global Cardinality

SICStus:

count(1,L,#>=,1), count(1,L,#=<,2) % 1 or 2 m/1

count(2,L,#>=,1), count(2,L,#=<,2) % 1 or 2 a/2

count(3,L,#=, 1) , % 1 only

n/3

count(4,L,#>=,0), count(4,L,#=<,2) % 0 to 2 h/4

count(5,L,#>=,0), count(5,L,#=<,2) % 0 to 2 r/5

CHIP:

among([1,2],L,_,[1]), % 1 or 2 m/1

among([1,2],L,_,[2]), % 1 or 2 a/2

among( 1 ,L,_,[3]), % 1 only n/3

among([0,2],L,_,[4]), % 0 to 2 h/4

among([0,2],L,_,[5]), % 0 to 2 r/5

(see [BeCo94] for details):

Page 40: Global Constraints: Generalised Arc Consistency

40

Global Constraints: Global Cardinality

• Nevertheless, the separate, or local, handling of each of these constraints, does not detect all the pruning opportunities for the variables domains. Take the following example:

A,B,C,D::{m,a}, E::{m,a,n}, F::{a,n,h,r}, G ::{n,r}

m (1,2)

F

D

E

A

B

C

G

a (1,2)

n (1,1)

h (0,2)

r (0,2)

Page 41: Global Constraints: Generalised Arc Consistency

41

Global Constraints: Global Cardinality

• A, B, C and D may only take values m and a. Since these may only be attributed to 4 people, no one else, namely E or F, may take these values m and a.

• Since E may now only take value n, that must be taken by a single person, no one else (e.g. F or G) may take value n.

m (1,2)

F

D

E

A

B

C

G

a (1,2)

n (1,1)

h (0,2)

r (0,2)

m (1,2)

F

D

E

A

B

C

G

a(1,2)

n (1,1)

h (0,2)

r (0,2)

Page 42: Global Constraints: Generalised Arc Consistency

42

Global Constraints: Global Cardinality

• This filtering, that could not be found in each constraint alone, can be

obtained with an algorithm that uses analogy with results in

maximum network flows [Regi96].

• A global cardinality constraint gcc/4,

– constrains a list of k variables X = [X1, ..., Xk] ,

– taking values in the domain (with m values) V = [v1, ..., vm],

– such that each of the vi values must be assigned to between Li and Mi variables.

• Then, m SICStus constraints...

count(vi,X,#>=,Li), count(vi,X,#=<,Mi) ...

may be replaced by constraint

gcc([X1....,Xk],[v1,...,vm],[L1,...,Lm],[M1,...,Mm])

Page 43: Global Constraints: Generalised Arc Consistency

43

Global Constraints: Global Cardinality

• The constraint gcc is modelled based on a parallel with a directed graph (or network) with maximum and minimum capacities in the arcs and two additional nodes, a e b. For example:

gcc([A,,...,G],[m,t,n,f,r],[1,1,1,0,0],[2,2,1,2,2])

m (1,2)

F

D

E

A

B

C

G

t (1,2)

n (1,1)

f (0,2)

r (0,2)

1 ; 21 ; 2

1 ; 1

0 ; 20 ; 2

0 ; 1

0 ; 1

0 ;

ab

Page 44: Global Constraints: Generalised Arc Consistency

44

Global Constraints: Global Cardinality

• A solution for the gcc constraint, corresponds to a flow between the

two added nodes, with a unitary flow in the arcs that link variables to

value nodes. In these conditions it is valid the following

Theorem: A gcc constraint with k variables is satisfiable iff there is a

maximal flow of value k, between nodes a and b

i

0

1

i

Flows

m (1,2)

F

D

E

A

B

C

G

t (1,2)

n (1,1)

f (0,2)

r (0,2)

1 ; 21 ; 2

1 ; 1

0 ; 20 ; 2

0 ; 1

0 ; 1

0 ;

2

2

7

b a

Page 45: Global Constraints: Generalised Arc Consistency

45

Global Constraints: Global Cardinality

Of course, being gcc a global constraint it is intended to

1. Obtain a maximum flow with value k, i.e. to show whether the

problem is satisfiable.

2. Achieve generalised arc consistency, by eliminating the arcs

between variables that are not used in any maximum flow solution,

i.e. do not belong to any gcc solution.

3. When some arcs are pruned (by other constraints) redo 1 and 2

incrementally.

In [Regi96] a solution is presented for these problems, together with

a study on its polinomial complexity.

Page 46: Global Constraints: Generalised Arc Consistency

46

Global Constraints: Global Cardinality

1. Obtain a maximal flow of value k

• This optimisation problem may be efficiently solved by linear

programming, that guarantees integer values in the solutions for the

flows.

• However, to take into account the intended incrementality, the

maximal flow may be obtained by using increasing paths in the

residual graph, until no increase is possible.

• The residual graph of some flow f is again a directed graph, with the

same nodes of the initial graph. Its arcs, with lower limit 0, have a

residual capacity that accounts for the non used capacity in a flow f.

Page 47: Global Constraints: Generalised Arc Consistency

47

Global Constraints: Global Cardinality

Residual graph of some flow f

a. Given arc (a,b) with max capacity c(a,b) and flow f(a,b) such that

f(a,b) < c(a,b) there is an arc (a,b) in the residual graph with

residual capacity cr(a,b) = c(a,b) - f(a,b).

The fact that the arc directions are the same means that the flow

may still increase in that direction by up to value cr(a,b).

b. Given arc (a,b) with min capacity l(a,b) and flow f(a,b) such that

f(a,b) > l(a,b) there is an arc (b,a) in the residual graph with residual

capacity cr(b,a) = f(a,b) - l(a,b).

The fact that the arc directions are opposed means that the flow

may decrease the initial flow by up to value cr(b,a).

Page 48: Global Constraints: Generalised Arc Consistency

48

Global Constraints: Global Cardinality

Example:

Given the following flow, with value 6 (lower than the maximal flow, which is of course 7) the following residual graph is obtained

22

2

6

22

1

02

6

2

2

2

Page 49: Global Constraints: Generalised Arc Consistency

49

Global Constraints: Global Cardinality

• Existing an arc (a,b) (in the residual graph) whose flow is not the same

as cr, there might be an overall increase in the flow between arcs a

and b, if the arc belongs to an increasing path of the residual graph.

• In the example below, the path in blue, increases the flow in the

original graph up to its maximum.

22

1

02

7

2

22

2

2

1

2

6

Page 50: Global Constraints: Generalised Arc Consistency

50

Global Constraints: Global Cardinality

• The computation of a maximal flow by this method is guaranteed by the

following

Theorem: A flow f between two nodes is maximal iff there is no

increasing path for f between the nodes.

• A decreasing path between a and b, could be defined similarly as a

path in the residual graph between b and a shown below.

221

02

5

2

122

2

1 6

Page 51: Global Constraints: Generalised Arc Consistency

51

Global Constraints: Global Cardinality

Complexity

• The search for an increasing path may be obtained by a breadth-

first search with complexity O(k+d+), where is the number of

arcs between the nodes and their domains, with size d. As kd

this is the dominant term, and the complexity is O().

• To obtain a maximum flow k, it is required to obtain k increasing

paths, one for each of the k variables. The complexity of the method

is thus O(k).

• As kd, the complexity to obtain a maximal flow k, starting from a

null flow is thus O(k2d). It is of course less, if the starting flow is not

null.

Page 52: Global Constraints: Generalised Arc Consistency

52

Global Constraints: Global Cardinality

• To eliminate arcs that correspond to assignments that do not belong

to any possible solution, we use the following

Theorem:

Let f be a maximum flow between nodes, a and b. For any other

nodes x and y, flow f(x,y) is equal to any flow f´(x,y) induced

between these nodes by another maximal flow f’, iff the arcs (x,y)

and (y,x) are not included in a cycle with more than 3 nodes

that does not include nodes a and b.

• Since the cycles considered do not include both a and b they will not

change the (maximal) flow. Hence, if there are no cycles including

nodes x and y, there are no increasing or decreasing paths through

them that do not change the maximum flow. But then their flow will

remain the same for all maximal flows.

Page 53: Global Constraints: Generalised Arc Consistency

53

Global Constraints: Global Cardinality

• Given the correspondence between maximal flow and the gcc

constraint, if no maximum flow passes in an arc betwen a variable

node X and a value node v, then for no solution of the gcc constraint

is X = v. This can be illustrated in the maximal graph shown below.

22

1

02

7

2

22

2

2

7

2

Page 54: Global Constraints: Generalised Arc Consistency

54

Global Constraints: Global Cardinality

• In the residual graph, the only paths with more than 3 nodes that do

not include nodes a and b, are those shown at the right. Also shown,

in blue, are the arcs with non null flow in the initial situation. These

are all the arcs between variable and value nodes in a maximal flow,

corresponding to possible solutions of constraint gcc/4.

2

2

22

2

7

2

Page 55: Global Constraints: Generalised Arc Consistency

55

Global Constraints: Global Cardinality

• We may compare the initial graph with that obtained after the

elimination of the arcs.

• As expected, the latter fixes value n for variable E, and removes

values m, a and n from variables F and G.

2

m (1,2)

F

D

E

A

B

C

G

a (1,2)

n (1,1)

h (0,2)

r (0,2)

Page 56: Global Constraints: Generalised Arc Consistency

56

Global Constraints: Global Cardinality

Complexity

• Obtaining cycles with more than 3 nodes corresponds to obtain the

subgraphs strongly connected, which can be done in O(m+n) for a

graph with m nodes and n arcs. Here, m = k+d+1 and n kd+d,

hence a global complexity of

O(kd+2d+k+1) O(kd)

• When some constraint removes a value from a variable that was

included in the current maximal flow, a new maximal flow may have

to be recomputed, with complexity at most O(k2d), so the complexity

of using this incremental implementation of the gcc/4 constraint is

O(k2d+kd) O(k2d).

Page 57: Global Constraints: Generalised Arc Consistency

57

Global Constraints: Flow

• Recently [BoPA01] a global constraint was proposed to model and

solve many network flow situations appearing in several problems,

namely transport, communication or production chain applications.

• As the previous ones, the goal of this constraint is to be integrated

with other constraints, as a part of a more general problem, but

allowing the efficient filtering that would not be possible if the

constraint were decomposed into simpler ones.

• Its use is described for problems of maximal flows, production

planning and roastering.

Page 58: Global Constraints: Generalised Arc Consistency

58

Global Constraints: Sequence

• In some applications it is not simply intended to impose cardinality

constraints on a set of variables, i.e. that in this set of variables each

value does not appear more than a certain number of times.

• It is additionally required that these variables are considered in

certain accepted sequences, namely that guarantee that each

value does not appear more than a certain number of times in

every sub-sequence of a given size.

• This situation is well illustrated in the car sequencing problem,

where cardinality constraints must be considered in the context of

some sequencing.

Page 59: Global Constraints: Generalised Arc Consistency

59

Global Constraints: Sequence

Example (Car sequencing):

The goal is to manufacture in an assembly line 10 cars with different

options (1 to 5) shown in the table. Given the assembly conditions of

option i, for each sequence of ni cars, only mi cars can have that

option installed as shown in the table below.

For example, in any sequence of 5 cars, only 2 may have option 4.

instaled.

option capacity 1 2 3 4 5 6 7 8 9 101 1 / 2 X2 2 / 33 1 / 3 X4 2 / 5 X X5 1 / 5

Configuration 1 2 3 4 5 6

cars

X

X

X

X

X

X

XX

Page 60: Global Constraints: Generalised Arc Consistency

60

Global Constraints: Sequence

• Hence, the goal is to implement an efficient global sequence

constraint , gsc/7

gsc(X, V, Seq, Lo, Up, Min, Max)

with the following semantics:

Given a sequence of variables X, Min and Max represent the

minimum/maximum number of times they may take values from list

V. Additionally, in each sequence of Seq variables, Lo and Up

represent the minimum/maximum number of times they may take

values from the list V.

• Notice that in CHIP the gsc/7 constraints has a different syntax

among([Seq, Lo, Up, Min, Max ], X, _, V)

Page 61: Global Constraints: Generalised Arc Consistency

61

Global Constraints: Sequence

• The following program specifies the sequencing constraints of this problem

goal(X):- X = [X1, .. ,X10], L :: 1..6, gcc(X, L, [1,1,2,2,2,2], [1,1,2,2,2,2]),

% gsc(X, V, Seq, Lo, Up, Min, Max) gsc(X, [1,5,6], 2, 1, 1, 5, 5), % option 1

gsc(X, [3,4,6], 3, 2, 2, 6, 6), % option 2 gsc(X, [1,5] , 3, 1, 1, 3, 3), % option 3 gsc(X, [1,2,4], 5, 2, 2, 4, 4), % option 4 gsc(X, [3] , 5, 1, 1, 2, 2). % option 5

option capacity 1 2 3 4 5 6 7 8 9 101 1 / 2 X2 2 / 33 1 / 3 X4 2 / 5 X X5 1 / 5

Configuration 1 2 3 4 5 6

cars

X

X

X

X

X

X

XX

Page 62: Global Constraints: Generalised Arc Consistency

62

Global Constraints: Sequence

• Given the similarity of the constraints, it was proposed in [RePu97]

an efficient implementation of the global sequencing constraint,

gsc/7, based on the global cardinality constraint, gcc/4.

• The way this implementation takes place can be explained by

means of an example.

• Given sequence X = [X1 .. X15], with X :: 1..4, it is intended that

values [1,2] appear between 8 and 12 times, and that each

sequence of 7 variables contains these values between 4 and 5

times , i.e.

gsc(X, [1,2], 7, 4, 5, 8, 12)

Page 63: Global Constraints: Generalised Arc Consistency

63

gsc(X, [1,2], 7, 4, 5, 8, 12)

• We may begin with, by considering a new set of variables Ai,

distributed in subsequences S1, S2 and S3 of 7 elements each

(except the last). The values of each Ak that belong to sequence Sk

are defined as

Xi [1,2] Ai = v , Xi [1,2] Ai = ak

• Clearly, there might not exist less than 2 nor more than 3 values a1

and a2 in the whole sequence A, to guarantee between 4 and 5

values [1,2] in S1 and S2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Xi 1 2 1 3 1 2 3 4 2 3 1 1 2 2 4

Ai v v v a1 v v a1 a2 v a2 v v v v a3

Global Constraints: Sequence

Page 64: Global Constraints: Generalised Arc Consistency

64

gsc(X, [1,2], 7, 4, 5, 8, 12)

In sequence S3 there must exist at least 0 appearences of a3 (it can

be completed to the left with sufficient number of vs) and at most 1

instance of a3, to guarantee an adequate number of values [1,2].

In general, for sequences of size S Seq it is necessary to

guarantee that # ak, the number of occurrences of ak to be

max(0, S - Up) # ak Seq - Lo

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Xi 1 2 1 3 1 2 3 4 2 3 1 1 2 2 4

Ai v v v a1 v v a1 a2 v a2 v v v v a3

Global Constraints: Sequence

Page 65: Global Constraints: Generalised Arc Consistency

65

gsc(X, [1,2], 7 , 4, 5, 8, 12)

gsc(X, V , Seq , Lo, Up, Max, Min)

• One must still guarantee the existence of between 8 (Min) and 12

(Max) vs in the Ai sequences.

• All these conditions are met, with the constraints that map the Xis

into Ais, and also with the global cardinality constraint

gcc(A, [v,a1,a2,a3], [8,2,2,0], [12,3,3,1])

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Xi 1 2 1 3 1 2 3 4 2 3 1 1 2 2 4

Ai v v v a1 v v a1 a2 v a2 v v v v a3

Global Constraints: Sequence

Page 66: Global Constraints: Generalised Arc Consistency

66

• Not all sequences of 7 (Seq) elements were already considered. For

thus purpose, 7 (Seq) gcc constraints must be considerd as shown

in the figure

• All these conditions are met, with the constraints that map the Xis

into Ais, and also with the global cardinality constraint

gcc(A, [v,a1,a2,a3], [8,2,2,0], [12,3,3,1])

Global Constraints: Sequence

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Xi 1 2 1 3 1 2 3 4 2 3 1 1 2 2 4

Ai a1 a1 a2 a2 a3

Bi b2 b2 b2 b3 b3

Ci c2 c2 c2 c3 c3

Di d2 d2 d2 d2 d3

Ei e1 e2 e2 e2 e3

Fi f1 f2 f2 f2 f3

Gi g1 g2 g2 g2 g3

Page 67: Global Constraints: Generalised Arc Consistency

67

The gsc constraint is thus mapped into 7 gcc constraints

gcc(A, [v,a1,a2,a3], [8,2,2,0], [12,3,3,1])gcc(B, [v,b1,b2,b3], [8,0,2,2], [12,1,3,3])gcc(C, [v,c1,c2,c3], [8,0,2,1], [12,2,3,3])gcc(D, [v,d1,d2,d3], [8,0,2,2], [12,3,3,3])gcc(E, [v,e1,e2,e3], [8,0,2,0], [12,3,3,3])gcc(F, [v,f1,f2,f3], [8,0,2,0], [12,3,3,3])gcc(G, [v,g1,g2,g3], [8,1,2,0], [12,3,3,2])

Global Constraints: Sequence1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Xi 1 2 1 3 1 2 3 4 2 3 1 1 2 2 4

Ai a1 a1 a2 a2 a3

Bi b2 b2 b2 b3 b3

Ci c2 c2 c2 c3 c3

Di d2 d2 d2 d2 d3

Ei e1 e2 e2 e2 e3

Fi f1 f2 f2 f2 f3

Gi g1 g2 g2 g2 g3

Page 68: Global Constraints: Generalised Arc Consistency

68

Global Constraints: Stretch

• Recently ([Pesa01]), another global constraint was proposed to

handle globally situations where it is intended to “stretch” the

sequences of equal values.

• Its use has been shown in roastering problems (nurse timetables).

For example, to guarantee that a nurse has a maximum sequence

of day shifts, not a sequence of alternating day and night shifts.

• Again, this constraint is to be integrated with other constraints to be

part of a more general problem, but allowing the efficient filtering

that would not be possible if the constraint were decomposed into

simpler ones.