69
1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of specialised algorithms that take the semantics of the constraint for efficient propagation. Example: Constrain a set of n variables to be all different between themselves. all_diff ([A1, A2, ..., An] Alternatives To define the constraint over binary constraints. To define a global constraint on the set of variables.

1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

1

Global Constraints: all_diffIt is often important to define n-ary “global” constraints, for at least two reasons

• Ease the modelling, • Exploitation of specialised algorithms that take the

semantics of the constraint for efficient propagation.

Example: Constrain a set of n variables to be all different between themselves.

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

Alternatives• To define the constraint over binary constraints.• To define a global constraint on the set of variables.

Page 2: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

2

Global Constraints: all_diffThe constaint 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.

all_diff([]).

all_diff([H|T]):-

one_diff(H,T),

all_diff(T).

one_diff(_,[]).

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

X #\= H,

one_diff(X,T).

Page 3: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

3

Global Constraints: all_diffHowever, constraint propagation based on binary constraints alone does not provide much propagation.

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.

And then, it is very easy to infer such elimination with a global view of the constraint!

Page 4: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

4

Global Constraints: all_diff

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

Variables X1, X5 e X8 may only take values 1, 2 e 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 e 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

Page 5: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

5

Global Constraints: all_diff

In this case, these prunings could be obtained, by maintaining 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 maintemnance 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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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.

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

10

Global Constraints: all_diffExample: 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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

11

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.

Global Constraints: all_diff

Page 12: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

14

Global Constraints: all_diff

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

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

15

Global Constraints: all_diffUpon 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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

16

Global Constraints: all_diff2. Elimination of a non-vital arc that does not belong to the maximal matching

- Eliminate the arcs that do not belong any more to an alternating cycle or path.

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

17

Global Constraints: all_diff3. Elimination of a non-vital arc that belongs 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

The new maximal matching includes arcs D-5 e E-6. With such matching, arc E-5 does not belong any more to any even alternating path or alternating cycle.

Page 18: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

18

Time Complexity:

Assuming n variables, each of which with d values, and being D 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 only O(dnn), not the poor result obtained with naïf analysis.

Global Constraints: all_diff

Page 19: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

19

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. Other versions of the algorithm (less pruning) are also available through another built-in constraint, named all _different/2, where the 2nd argument controls the available pruning options.

Global Constraints: all_diff

Page 20: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

20

Global Constraints: all_diff

Example ( program all_different.txt):

adn([A,B,C,D,E,F,G,H,I,J]):-

A in 0..9, B in 0..8, C in 0..7, D in 0..6, E in 0..5,

F in 0..4, G in 0..3, H in 0..2, I in 0..1, J in 0..1,

statistics(runtime,[_,_]),

all_different([A,B,C,D,E,F,G,H,I,J]),

labeling([],[A,B,C,D,E,F,G,H,I,J]),

statistics(runtime,[_,T]),

nl,write('T'-T), nl,

fd_statistics.

Page 21: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

21

Global Constraints: all_diff

Example: ?- ad3(L).

T-0

Resumptions: 2

Entailments: 1

Prunings: 20

Backtracks: 0

Constraints created: 1

L=[9,8,7,6,5,4,3,2,0,1]?

yes

?- ad1(L).

T-150

Resumptions: 47933

Entailments: 42373

Prunings: 39556

Backtracks: 5560

Constraints created: 45

L=[9,8,7,6,5,4,3,2,0,1]?

yes

Page 22: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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.

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

Hence, other constraints could be specified on tasks or resources, but not easily involving both types of objects.

Page 23: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

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.

A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

Page 26: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

26

Global Constraints: assignment

Hence, the initial domainsA1: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,A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

A1

A2

A3

A4

A5

B1

B2

B3

B4

B5

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,

Page 27: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

27

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 28: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

28

Global Constraints: circuitThese problems may be described by means of directed graphs, whose nodes represent tasks and the directed arcs represent precedences.

A B

C D

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

2

3

4 5 6

9

8

7

2

2

2

Page 29: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

29

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 e 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 30: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

30

Global Constraints: circuitThe 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 31: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

31

Global Constraints: circuit

The global constraint circuit/1 incrementally achieves the pruning of the arcs that do not belong to any hamiltonian circuit. For example, posting the constraint

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

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 32: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

32

Global Constraints: element

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.

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.

Page 33: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

33

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 constraint little would infer 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 not only impose X = 3 but also A = 2.

if A = 2 then X = 3, if A = 3 then X = 4; otherwise X = undefined

Page 34: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

34

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 35: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

35

Global Constraints: element

Example (travelling.txt): For the graph below, determine an hamiltonian circuits whose length does not exceed some maximum value Max (say 20).

circ([A,B,C,D], Max, Circ):- 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), Circ #= Ca+Cb+Cc+Cd, Circ #=< 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 36: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

36

Global Constraints: cycle

Sometimes, the goal is to form more than a 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: The constraint circuit(L) is in fact implemented in CHIP as cycle(1,L).

Page 37: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

37

Example:X1::[1,3,4] X4::[2,5]

X2::[1,3] X5::[6]

X3::[2,5,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]

Global Constraints: cycle

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 38: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

38

Many scheduling and timetabling problems, have 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.

Global Constraints: global cardinality

Page 39: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

39

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 and in CHIP this complex constraint may be decomposed in several cardinality constraints.

Global Constraints: global cardinality

Page 40: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

40

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 (see [BeCo94] for details):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

Global Constraints: global cardinality

Page 41: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

41

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}

Global Constraints: global cardinality

m (1,2)

F

D

E

A

B

C

G

a (1,2)

n (1,1)

h (0,2)

r (0,2)

Page 42: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

42

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.

Global Constraints: global cardinality

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 43: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

43

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, will constrain 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 e 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])

Global Constraints: global cardinality

Page 44: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

44

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

Global Constraints: global cardinality

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 45: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

45

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

Global Constraints: global cardinality

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 46: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

46

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

Global Constraints: global cardinality

Page 47: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

47

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.

Global Constraints: global cardinality

Page 48: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

48

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) > c(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).

Global Constraints: global cardinality

Page 49: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

49

Example: Given the following flow, with value 6 (lower than the maximal flow, 7) the following residual graph is obtained

Global Constraints: global cardinality

22

2

6

22

1

02

6

2

2

Page 50: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

50

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.

Global Constraints: global cardinality

22

1

02

7

2

22

2

2

1

Page 51: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

51

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.

Global Constraints: global cardinality

221

02

5

2

122

2

1

Page 52: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

52

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.

Global Constraints: global cardinality

Page 53: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

53

To eliminate arcs that correspond to assignments that do not belong to any possible solution, we use

Theorem: Let f be a maximum flow between nodes, a e 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.

Global Constraints: global cardinality

Page 54: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

54

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

Example: Assume the following maximal graph, with value 7, and the corresponding residual graph.

Global Constraints: global cardinality

22

1

02

7

2

22

2

2

7

Page 55: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

55

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.

Global Constraints: global cardinality

22

2

7

2

Page 56: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

56

We may compare the initial graph with that obtained after the elimination of the arcs, which, as expected fixes value n for variable E, and removes values m, a and n from variables F and G.

Global Constraints: global cardinality

2

m (1,2)

F

D

E

A

B

C

G

a (1,2)

n (1,1)

h (0,2)

r (0,2)

Page 57: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

57

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

Global Constraints: global cardinality

Page 58: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

58

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 59: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

59

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 a sequence, and guarantee the above goal (that each value does not appear more than a certain number of times) for 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 60: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

60

Global Constraints: sequence

Example:

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 (see table: for each sequence of 5 cars, only 2 may have option 4 installed).

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 61: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

61

Global Constraints: sequence

Hence, the goal is to implement an efficient global sequence constraint , gsc/7

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

Its semantics is as follows:

Given the 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 62: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

62

Global Constraints: sequence

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 10

1 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 63: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

63

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 between 4 and 5 times these values, i.e.

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

Page 64: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

64

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 .

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 v v v a1 v v a1 a2 v a2 v v v v a3

Page 65: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

65

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

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

In sequence S3 there must exist at least 0 a3 (it can be completed to the left with sufficient number of vs) and at most 1 a3, to guarantee an adequate number of values [1,2].

In general, for sequences of size S Seq it is necessary to guarantee a number of ak , # ak, between

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

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 v v v a1 v v a1 a2 v a2 v v v v a3

Page 66: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

66

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

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 v v v a1 v v a1 a2 v a2 v v v v a3

Page 67: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

67

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

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 68: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

68

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: 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 69: 1 Global Constraints: all_diff It is often important to define n-ary “global” constraints, for at least two reasons Ease the modelling, Exploitation of

69

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.