2 March 2006 Constraints - Finite Domains 2 Cardinality (Meta-) Constraint Many problems, impose various types of precedence betwen tasks, have the requirement that among a list of constraints Lr = [C1, C2,..., Cn] The number of constraints to satisfy must lie between a lower bound L and an upper bound U, both inclusive. Syntactically, this meta-constraints may be specified as #(L,U, [C1, C2,..., Cn]) In particular, such constraints are quite common in resource management and in scheduling problems, where resources (machines, materials, tasks or even people) must be allocated in a flexible way.
2 March 2006 Constraints - Finite Domains 3 Cardinality Constraint: Example Example (Nurse Scheduling): From the 7 nurses available at some hospital ward, at least 4 must be on duty in the first shift. This constraint may be modelled by considering boolean variables X ij to represent that nurse i is on duty in shift j. The intended allocation may be modelled by specification, to each shift j, of the cardinality meta-constraint #(4,7, [X 1j =1, X 2j =1,..., X 7j =1]) In alternative, 7 variables N ij are considered for each shift j, whose domain are the nurses 0 to 7 (0 meaning no nurse) leading to the formulation #(4, 7, [N 1j 0, N 2j 0,..., N 7j 0])
2 March 2006 Constraints - Finite Domains 4 Cardinality Constraint: Operational Semantics The operational semantics of this meta-constraint may be specified by rewriting rules, using the Ask & Tell primitives just presented. The first two rules rewrite the constraint into a simplified form Rule 1: If one constraint is satisfied, it may be removed, and the number of constraints to satisfy is decreased by one. #(L,U, [C1, C2,..., Ci,..., Cn]), ask(Ci) #(L-1,U-1, [C1, C2,...,Ci-1,Ci+1,..., Cn]) Rule 2: If one constraint is not satisfied, it is simply removed from the list. #(L,U, [C1, C2,..., Ci,..., Cn]), ask(~Ci) #(L,U, [C1, C2,...,Ci-1,Ci+1,..., Cn])
2 March 2006 Constraints - Finite Domains 5 Cardinality Constraint: Operational Semantics The two following rules refer to situations where the satisfaction or dissatisfaction of the constraints must be imposed. Rule 3: If the number of constraints in the list is the same as the lower limit, then all constraints must be satisfied. #(L,U, [C1, C2,..., Ci,..., Cn]), L = n tell(C1), tell(C2),..., tell(Cn) Rule 4: If the upper limit becomes 0, all the constraints in the list must be dissatisfied. #(L,U, [C1, C2,..., Ci,..., Cn]), U = 0 tell(~C1), tell(~C2),..., tell(~Cn)
2 March 2006 Constraints - Finite Domains 6 Cardinality Constraint: Operational Semantics Finally, the meta-constraint is trivially verified after certain transitions of rules 1 and 2 that decrease the constraints in the list and the lower and upper limits. Rule 5: If no more constraints must be satisfied, and the constraints in the list are not more than the upper limit, the meta-constraint succeeds. #(L,U, [C1, C2,..., Cn]), L = 0, U >= n true Rule 6: If there are less constraints in the list than the lower bound, the meta- constraint fails #(L,U, [C1, C2,..., Cn]), L > n false
2 March 2006 Constraints - Finite Domains 7 Cardinality Constraint through Reified Constraints Although this constraint may be specified directly from the rules presented, the Ask&Tell mechanisms of SICStus based in the reified constraints allow a more efficient form of specification, by counting the satisfied constraints and setting the intended limits, by means of a typical recursive Prolog-like definition. cardinal(L,U,Cs):- sats(Cs,N), N #=< U, L #=< N. sats(,0). sats([C1|Cs],N1):- C1 # B, N1 #= N2 + B, sats(Cs,N2).
2 March 2006 Constraints - Finite Domains 8 Cardinality Constraint through Reified Constraints The cardinality (meta-)constraint is genericaly applicable to combinations of constraints requiring some form of counting. Other combinations of constraints are possible to specify through the use of reified constraints. Among others, it is worth mentioning the case of conditional constraints, i.e. those that should only be imposed if others are satisfied. In scheduling, for example, it may be the case that only one of tasks T 1 and T 2 must finish before some time limit Z. If this is task T 2, then task T 3 must start after some delay interval, I, starting from the end of task T 2.
2 March 2006 Constraints - Finite Domains 9 Conditional Constraints through Reification The guarantee that only one of the tasks T 1 or T 2 terminates before Z, may be specified with cardinality card( 1, 1, [S 1 +D 1 #< Z, S 2 +D 2 #< Z] ) However, the conditional constraint between T 2 e T 3 is not easily expressed by means of countings, since the number of constraints to be satisfied on tasks T 2 / T 3 may be 0, 1 or 2. Conditional constraints may be modelled directly on the variables associated with the reified constraints. In this case, we would have the following reifications S 1 +D 1 # B 1 S 2 +D 2 # B 2 S 3 #> S 2 +D 2 +I # B 3
2 March 2006 Constraints - Finite Domains 10 Conditional Constraints through Reification Now, using directly boolean variables from the reified constraints, we may specify both the cardinality constraint on tasks T 1 and T 2 B 1 + B 2 #= 1 as well as the conditional constraint on tasks T 2 and T 3, through the inequality B 3 #>= B 2 Such inequality expresses the intended condition. If constraint C 2 is not satisfied (B2=0) the conditional constraint is not effectively posted or told (C 3 may be satisfied or not). When constraint C 2 is satisfied (B2=1), then B 3 is set to 1, and constraint C 3 is told.
2 March 2006 Constraints - Finite Domains 11 Propositional Constraints For convenience, rather than working with the boolean variables associated to reified constraints, SICStus allows its direct use in propositional constraints. For instance, the constraints on tasks T 1, T 2 and T 3 could be directly specified by means of the following propositional constraints Cardinality (one and only one of constraints C 1 and C 2 ) (S 1 +D 1 #< Z) #\ ( S 2 +D 2 #< Z) Conditional (if C 2 then C 3 ) (S 3 #> S 2 +D 2 +I ) #
2 March 2006 Constraints - Finite Domains 12 Cardinality Constraint through Reified Constraints The following propositional constraints are defined #\ C1B1 #= 0C1 must be dissatisfied C1 #/\ C2B1+B2 #= 2C1 and C2 must both be satisfied C1 #\ C2B1+B2 #= 1 One and only one of C1 and C2 must be satisfied C1 #\/ C2B1+B2 #>=1At least one of C1 and C2 must be satisfied C1 #=> C2B2 #>= B1It C1 (C2) is satisfied, then C2 (C1) must also be C2 #
2 March 2006 Constraints - Finite Domains 14 Constructive Disjunction Example: Let us consider two constraints C 1 and C 2 on variable X, whose domain is 1 to 100, of which we want that at least one of them stands (disjunction) C1 : X #>= 50C2 : X #=< 20 The previous propositional constraints would suspend until the domain of X be pruned to either above 20 or below 50, in which case, one of the disjuncts would be committed to. For example, if X is reduced to 3.. 40, C 2 could be posted safely, further pruning the domain to 3.. 20. However, the interval 21.. 49 could have been immediately pruned from the the domain of X, since it does not satisfy either of the disjuncts.
2 March 2006 Constraints - Finite Domains 15 Constructive Disjunction Such antecipation of domain pruning is the goal of constructive disjunction. Its modelling may be specified through adequate indexical constraints. disjunction1(X, S1, S2) :- X in S2.. Sup \/ inf.. S1. ?- X in 1..100, disjunction1(X,20,50). X in(1..20)\/(50..100) and compared with the least commitment implementation disjunction2(X, S1, S2) :- X #>= S2 #\/ X #=< S1. ?- X in 1..100, disjunction2(X,20,50). X in 1..100
2 March 2006 Constraints - Finite Domains 16 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
2 March 2006 Constraints - Finite Domains 17 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).