1 Introduction
When a highlevel constraint programming (CP) language lacks a (possibly global) constraint that would allow the formulation of a particular model of a combinatorial problem, then the modeller traditionally has the choice of (1) switching to another CP language that has all the required constraints, (2) formulating a different model that does not require the lacking constraints, or (3) implementing the lacking constraint in the lowlevel implementation language of the chosen CP language. This paper addresses the core question of facilitating the third option, and as a side effect often makes the first two options unnecessary.
The userlevel extensibility of CP languages has been an important goal for over a decade. In the traditional global search approach to CP (namely heuristicbased tree search interleaved with propagation), higherlevel abstractions for describing new constraints include indexicals
[18]; (possibly enriched) deterministic finite automata (DFAs) via the [3] and [12] generic constraints; and multivalued decision diagrams (MDDs) via the [6] generic constraint. Usually, a generic but efficient propagation algorithm achieves a suitable level of local consistency by processing the higherlevel description of the new constraint. In the more recent local search approach to CP (called constraintbased local search, CBLS, in [15]), higherlevel abstractions for describing new constraints include invariants [10]; a subset of firstorder logic with arithmetic via combinators [17] and differentiable invariants [16]; and existential monadic secondorder logic for constraints on set decision variables [2]. Usually, a generic but incremental algorithm maintains the constraint and variable violations by processing the higherlevel description of the new constraint.In this paper, we revisit the description of new constraints via automata, already successfully tried within the global search approach to CP [3, 12], and show that it can also be successfully used within the local search approach to CP. The significance of this endeavour can be assessed by noting that of the currently global constraints in the Global Constraint Catalogue [4] are described by DFAs that are possibly enriched with counters and conditional transitions [3] (note that DFA generators can easily be written for other constraints, such as the [5] and [11] constraints, taking the necessarily ground parameters as inputs), so that all these constraints will instantly become available in CBLS once we show how to implement fully the enriched DFAs that are necessary for some of the described global constraints.
The rest of this paper is organised as follows. In Section 2, we present our algorithm for incrementally maintaining both the violation of a constraint described by an automaton, and the violations of each decision variable of that constraint. In Section 3, we present experimental results establishing the practicality of our results, also in comparison to the prior approach of [13]. Finally, in Section 4, we summarise this work and discuss related as well as future work.
2 Incremental Violation Maintenance with Automata
In CBLS, three things are required of an implemented constraint: a method for calculating the violation of the constraint and each of its decision variables for the initial assignment (initialisation); a method for computing the differences of these violations upon a candidate local move (differentiability) to a neighbouring assignment; and a method for incrementally maintaining these violations when an actual move is made (incrementality). Intuitively, the higher the violation of a decision variable, the more can be gained by changing the value of that decision variable. It is essential to maintain incrementally the violations rather than recomputing them from scratch upon each local move, since by its nature a local search procedure will try many local moves to find one that ideally reduces the violation of the constraint or one of its decision variables.
Our running example is the following, for a simple work scheduling constraint. There are values for two work shifts, day () and evening (), as well as a value for enjoying a day off (). Work shifts are subject to the following three conditions: one must take at least one day off before a change of work shift; one cannot work for more than two days in a row; and one cannot have more than two days off in a row. A DFA for checking ground instances of this constraint is given in Figure 1. The start state is marked by a transition entering from nowhere, while the success states and are marked by double circles. Missing transitions, say from state upon reading value , are assumed to go to an implicit failure state, with a selflooping transition for every value (so that no success state is reachable from it).
2.1 Violations of a Constraint
To define and compute the violations of a constraint described by an automaton, we first introduce the notion of a segmentation of an assignment:
Definition 1 (Segmentation)
Given an assignment , a segmentation is a possibly empty sequence of nonempty substrings (referred to here as segments) of such that for each and we have that .
For example, a possible segmentation of the assignment is ; note that the third character of the assignment is not part of any segment. In general, an assignment has multiple possible segmentations. We are interested in segmentations that are accepted by an automaton, in the following sense:
Definition 2 (Acceptance)
Given an automaton and an assignment , a segmentation is accepted by the automaton if there exist strings , where only and may be empty, such that the concatenated string
is accepted by the automaton.
For example, given the automaton in Figure 1, the assignment has a segmentation with , which is accepted by the automaton via the string with (the empty string) and .
Given an assignment, the algorithm presented below initialises and updates a segmentation. The violations of the constraint and its decision variables are calculated relative to the current segmentation:
Definition 3 (Violations)
Given an automaton describing a constraint and given a segmentation of an assignment for a sequence of decision variables :

The constraint violation of is .

The variable violation of decision variable is if there exists a segment index in such that , and otherwise.
It can easily be seen that the violation of a constraint is also the sum of the violations of its decision variables, and that it is never an underestimate of the minimal Hamming distance between the current assignment and any satisfying assignment.
Our approach, described in the next three subsections, greedily grows a segmentation from left to right across the current assignment relative to a satisfying assignment, and makes stochastic choices whenever greedy growth is impossible.
2.2 Initialisation
A finite automaton is first unrolled for a given length of a sequence of decision variables, as in [12]:
Definition 4 (Layered Graph)
Given a finite automaton with states, the layered graph over a given number of decision variables is a graph with nodes. Each of the vertical layers has a node for each of the states of the automaton. The node for the start state of the automaton in layer 1 is marked as the start node. There is an arc labelled from node in layer to node in layer if and only if there is a transition labelled from to in the automaton. A node in layer is marked as a success node if it corresponds to a success state in the automaton.
The layered graph is further processed by removing all nodes and arcs that do not lead to a success node. The resulting graph, seen as a DFA (or as an ordered MDD), need not be minimised (or reduced) for our approach (although this is a good idea for the global search approaches [3, 12], as argued in [8], and would be a good idea for the local search approach of [13]), as the number of arcs of the graph does not influence the time complexity of our algorithm below. For instance, the minimised unrolled version for decision variables of the automaton in Figure 1 is given in Figure 2. Note that a satisfying assignment corresponds to a path from the start node in layer to a success node in layer , such that each arc from layer to layer of this path is labelled .
Further, we require a number of data structures, where is the number of states in the given automaton and is the number of decision variables it was unrolled for:

records the number of paths from node in layer to a success node in the last layer; for example, see the numbers by each node in Figure 2;

is the number of segments in the current segmentation;

segments record the current segmentation;

records the current violation of decision variable (see Definition 3);
The matrix can be computed in straightforward fashion by dynamic programming. The other three data structures are initialised (when the starting position is ) and maintained (when decision variable is changed, with ) by the procedure of Algorithm 1. Upon some initialisations (lines 2 and 3), it (re)visits only the decision variables (line 4). If the value of the currently visited decision variable triggers the extension of the currently last segment (lines 6 and 9) or the creation of a new segment (lines 6 to 9), then its violation is (line 10). Otherwise, its violation is
and a successor node is picked with a probability weighted according to the number of paths from the current node to a success node (lines 11 to 14). Toward this, we maintain the nodes of the picked path (line 16).
The time complexity of Algorithm 1 is linear in the number of decision variables, because only one path (from layer to layer ) is explored, with a constanttime effort at each node. Once the preprocessing is done, the time complexity of Algorithm 1 is thus independent of the number of arcs of the unrolled automaton! Hence the minimisation (or reduction) of the unrolled automaton would be merely for space savings (and for the convenience of human reading) as well as for accelerating the preprocessing computation of the matrix. In our experiments, these space and time savings are not warranted by the time required for minimisation (or reduction).
Note that this algorithm works without change or loss of performance on nondeterministic finite automata (NFAs). This is potentially interesting since NFAs are often smaller than their equivalent DFAs, but (as just seen) the number of arcs has no influence on the time complexity of Algorithm 1.
For example, in Figure 2, with the initial assignment and a first call to Algorithm 1 with , the first segment will be (the red path). Next, the assignment triggers a violation of for decision variable (we say that it is a violated variable) because there is no arc labelled that connects the current node 4 in layer 3 with any nodes in layer 4. However, node 4 in layer 3 has two outgoing arcs, namely to nodes 3 and 5 in layer 4 (in blue). In layer 4, there are paths from node 3 to the last layer, compared to such paths from node 5, so node 3 is picked with probability and node 5 is picked with probability (where the , , and are the purple numbers by those nodes), and we assume that node 3 in layer 4 is picked. From there, we get the second segment (the green path), which stops at success node 5 in the last layer. The violation of the constraint is thus , because the value of one decision variable does not participate in any segment.
Continuing the example, we assume now that decision variable is changed to value , and hence we call Algorithm 1 with . Only segment can be kept from the previous segmentation picked for , namely (the red path). Since there is an arc labelled from the current node 4 in layer 3, namely to node 5 in layer 4, segment is extended (line 9) to . However, with decision variable still having value , this segment cannot be extended further, since there is no arc labelled from node 5 in layer 4, and hence is violated. Similarly, decision variables and are violated no matter which successors are picked, so no new segment is ever created. The violation of the constraint is thus because the value of three decision variables do not participate in any segment. Hence changing decision variable from value to value would not be considered a good local move, as the constraint violation increases from to . Changing decision variable to value instead would be a much better local move, as the first segment is then extended to the entire current assignment , without detecting any violated variables, so that the violation of the constraint is then , meaning that a satisfying assignment was found.
2.3 Differentiability
At present, the differences of the (constraint and variable) violations upon a candidate local move are calculated naïvely by first making the candidate move and then undoing it.
2.4 Incrementality
Local search proceeds from the current assignment by checking a number of neighbours of that assignment and picking a neighbour that ideally reduces the violation: the exact heuristics are often problem dependent. But in order to make local search computationally efficient, the violations of the constraint and its decision variables have to be computed in an incremental fashion whenever a decision variable changes value. As shown in Subsection 2.2, our initialisation Algorithm 1 can also be invoked with an arbitrary starting position when decision variable is assigned a new value.
We have implemented this algorithm in Comet [15], an objectoriented CP language with among others a CBLS backend (available at www.dynadec.com).
3 Experiments
We now establish the practicality of the proposed violation maintenance algorithm by experimenting with it. All local search experiments were conducted under Comet (version 2.0 beta) on an Intel 2.4 GHz Linux machine with 512 MB memory while the constraint programming examples where implemented using SICStus Prolog.
Mon  Tue  Wed  Thu  Fri  Sat  Sun  

1  
2  
3  
4  
5 
Many industries and services need to function around the clock. Rotating schedules such as the one in Table 1 (a reallife example taken from [9]) are a popular way of guaranteeing a maximum of equity to the involved work teams (see [9]). In our first benchmark, there are day (), evening (), and night () shifts of work, as well as days off (). Each team works maximum one shift per day. The scheduling horizon has as many weeks as there are teams. In the first week, team is assigned to the schedule in row . For any next week, each team moves down to the next row, while the team on the last row moves up to the first row. Note how this gives almost full equity to the teams, except, for instance, that team does not enjoy the six consecutive days off that the other teams have, but rather three consecutive days off at the beginning of week and another three at the end of week . The daily workload may be uniform: for instance, in Table 1, each day has exactly one team onduty for each work shift, and two teams entirely offduty; we denote this as ; assuming the work shifts average h, each employee will work h over the fiveweekcycle, or h per week. Daily workload, whether uniform or not, can be enforced by global cardinality () constraints [14] on the columns. Further, any number of consecutive workdays must be between two and seven, and any change in work shift can only occur after two to seven days off. This can be enforced by a constraint [5] and a circular constraint [11] on the table flattened rowwise into a sequence .
Our model posts the and constraints described by automata. The constraints on the columns of the matrix are kept invariant: the first assignment is chosen so as to satisfy them, and then only swap moves inside a column are considered. As a metaheuristic, we use tabu search with restarting. At each iteration, the search procedure in Algorithm 2 selects a violated variable (line 4; recall that the violation of a decision variable is here at most ) and another variable of distinct value in the same column so that their swap (line 8) gives the greatest violation change (lines 5 to 7). The length of the tabu list is the maximum between and the sum of the violations of all constraints (lines 9 and 10). The best solution so far is maintained (lines 11 to 14). Restarting is done every iterations (lines 15 and 16). The expressions for the length of the tabu list and the restart criterion were experimentally determined.
Recall that Algorithm 1 computes a greedy random segmentation; hence it might give different segmentations when used for probing a swap and when used for actually performing that swap. Therefore, we record the segmentation of each swap probe, and at the actual swap we just apply its recorded segmentation.
We ran experiments over the eight instances from to (we write the latter as ) with uniform daily workload, where the weekly workload is h. For example, instance has the uniform daily workload of teams on the day shift, teams on the evening shift, teams on the night shift, and teams offduty. Table 2 gives statistics on the run times and numbers of iterations to find the first solutions over runs from random initial assignments.
optimisation time (ms)  number of iterations  

instance  min  max  avg  min  max  avg  
6  100  22  20  9  528  115  108  
12  692  168  154  32  2484  585  561  
32  2588  688  611  44  6612  1726  1571  
80  6553  1199  1275  86  11212  2125  2303  
60  9373  1417  1545  72  15604  2292  2556  
160  5901  1527  1227  161  8051  2051  1681  
176  9896  1720  1686  157  11680  1966  1981  
216  12472  2620  2309  150  12588  2603  2354 
Minimum, maximum, average, standard deviation of optimisation times (in milliseconds) and numbers of iterations to the first solutions of rotating nurse schedules (100 runs) from
random initial assignments.Posting the product of the and automata (accepting the intersection of their two regular languages) has been experimentally determined to be more efficient than posting the two automata individually, hence all experiments in this paper use the product automaton.
Further improvements can be achieved by using a nonrandom initial assignment. Table 3 gives statistics on the run times and numbers of iterations to find the first solutions over runs, where the initial assignment of instance consists of copies of Table 4. The results show that this nonrandom initialisation provides a better starting point. Although much more experimentation is required, these initial results show that even on the instance with decision variables it is possible to find solutions quickly.
optimisation time (ms)  number of iterations  
instance  min  max  avg  min  max  avg  
1  16  2  3  6  61  11  9  
1  64  12  12  13  235  34  46  
12  76  25  13  21  173  46  29  
28  172  51  27  32  297  72  49  
28  200  79  42  34  286  106  61  
56  368  135  76  61  487  156  101  
84  768  188  123  69  848  189  140  
112  764  233  112  72  736  202  113 
Mon  Tue  Wed  Thu  Fri  Sat  Sun  

1  
2  
3  
4  
5  
6 
The only related work we are aware of is a Comet implementation [13] of the constraint [12], based on the ideas for the propagator of the soft constraint [7]
. The difference is that they estimate the violation change compared to the
nearest solution (in terms of Hamming distance from the current assignment), whereas we estimate it compared to one randomly picked solution. In our terminology (although it is not implemented that way in [13]), they find a segmentation, such that an accepting string for the automaton has the minimal Hamming distance to the current assignment.Tables 5 and 6 give comparisons between (our reimplementation of) [13], our method, and a SICStus Prolog constraint program (CP) where the product automaton of the and constraints was posted using the builtin propagationbased implementation of the constraint [3]. These experiments show:

Compared with [13], our method has a higher number of iterations, as it is more stochastic. However, our run times are lower, as our cost of one iteration is much smaller (linear in the number of decision variables, instead of linear in the number of arcs of the unrolled automaton).

Compared with the CP method, both local search methods need more time to find the first solution when the number of weeks is small. However, when the number of weeks increases, the runtime of CP increases sharply. From the instance , the runtime of CP exceeds the average runtime of our method. From the instance , the runtime of CP exceeds also the average runtime of [13].
optimisation time (ms)  number of iterations  
instance  our method  [13]  CP  our method  [13]  
avg  avg  avg  avg  
22  20  395  378  10  115  108  98  100  
168  154  1584  1187  10  585  561  223  170  
688  611  3441  2871  10  1726  1571  333  287  
1199  1275  5584  4423  40  2125  2303  399  319  
1417  1545  8828  7606  100  2292  2556  514  444  
1527  1227  13888  10863  510  2051  1681  672  529  
1720  1686  13170  9814  3520  1966  1981  536  485  
2620  2309  20202  11530  25820  2603  2354  745  602  
St Louis Police  12740  11199  50261  48026  –  20287  17952  3248  2498 
optimisation time (ms)  number of iterations  

instance  our method  [13]  our method  [13]  
avg  avg  avg  avg  
2  3  44  26  11  9  10  7  
12  12  182  65  34  46  22  9  
25  13  478  201  46  29  41  17  
51  27  834  254  72  49  56  18  
79  42  1546  876  106  61  87  51  
135  76  2414  1233  156  101  113  59  
188  123  4517  3276  189  140  181  134  
233  112  4473  1958  202  113  160  71  
St Louis Police  3990  4012  67159  55632  5389  5598  3949  3159 
Besides the rotating nurse instances, we ran experiments on another, harder reallife scheduling instance. The St Louis Police problem (described in [13]) has a seventeenweekcycle; however it has more constraints than the rotating nurse problem. It has nonuniform daily workloads. For example, on Mondays, five teams work during the day, five at night, four in the evening, and three teams enjoy a day off; while on Sundays, three teams work during the day, four at night, four in the evening, and six teams enjoy a day off. Any number of consecutive workdays must be between three and eight, and any change in work shift can only occur after two to seven days off. The problem has other vertical constraints; for example, no team can work in the same shift on four consecutive Mondays. Further, the problem has complex constraints that limit possible changes of work shifts; for example, only the patterns , , , , , and are allowed. For this hard reallife problem, our method still works well: experimental results can also be found in Tables 5 and 6.
It is possible to post (see Figure 3) the constraints of the rotating nurse problem using the differentiable invariants [16] of Comet. This is possible in general for any automaton by encoding all the paths to a success state by using Comet’s conjunction and disjunction combinators. As the automata get larger, these expressions can become too large to post, and even when it is possible to post these expressions our current experiments show that our approach is more efficient.
4 Conclusion
In summary, we have shown that the idea of describing novel constraints by automata can be successfully imported from classical (global search) constraint programming to constraintbased local search (CBLS). Our violation algorithms take time linear in the number of decision variables, whereas the propagation algorithms take amortised time linear in the number of arcs of the unrolled automaton [3, 12]. We have also experimentally shown that our approach is competitive with the CBLS approach of [13].
There is of course a tradeoff between using an automaton to describe a constraint and using a handcrafted implementation of that constraint. On the one hand, a handcrafted implementation of a constraint is normally more efficient during search, because properties of the constraint can be exploited, but it may take a lot of time to implement and verify it. On the other hand, the (violation or propagation) algorithm processing the automaton is implemented and verified once and for all, and our assumption is that it takes a lot less time to describe and verify a new constraint by an automaton than to implement and verify its algorithm. We see thus opportunities for rapid prototyping with constraints described by automata: once a sufficiently efficient model, heuristic, and metaheuristic have been experimentally determined with its help, some extra efficiency may be achieved, if necessary, by handcrafting implementations of any constraints described by automata.
As witnessed in our experiments, constraint composition (by conjunction) is easy to experiment with under the DFA approach, as there exist standard and efficient algorithms for composing and minimising DFAs, but there is no known systematic way of composing violation (or propagation) algorithms when decomposition is believed to obstruct efficiency.
In the global search approach to CP, the common modelling device of reification can be used to shrink the size of DFAs describing constraints [3]. For instance, consider the constraint, which holds if and only if . Upon reifying the decision variables into new Boolean decision variables such that , it suffices to pose the constraint, where corresponds to the regular expression , meaning that at least one must be found in the sequence of the decision variables. However, such explicit reification constraints are not necessary in constraintbased local search, as a total assignment of values to all decision variables is maintained at all times: instead of processing the values of the decision variables when computing the segments, one can process their reified values.
It has been shown that the use of counters (initialised at the start state and evolving during possibly conditional transitions) to enrich the language of DFAs and thereby shrink the size of DFAs can be handled in the global search approach to CP [3], possibly upon some concessions at the level of local consistency that can be achieved. In the Global Constraint Catalogue [4], some of the currently constraints described by DFAs use lists of counters, and another constraints use arrays of counters. We need to investigate the effects on our violation maintenance algorithm of introducing counters and conditional transitions.
Acknowledgements
The authors are supported by grant 20076445 of the Swedish Research Council (VR), and Jun He is also supported by grant 2008611010 of China Scholarship Council and the National University of Defence Technology of China. Many thanks to Magnus Ågren (SICS) for some useful discussions on this work, and to the anonymous referees of both LSCS’09 and the Doctoral Programme of CP’09, especially for pointing out the existence of [13, 7].
References
 [1]
 [2] Magnus Ågren, Pierre Flener & Justin Pearson (2007): Generic incremental algorithms for local search. Constraints 12(3), pp. 293–324. (Collects the results of papers at CPAIOR’05, CP’05, and CP’06, published in LNCS 3524, 3709, and 4204).
 [3] Nicolas Beldiceanu, Mats Carlsson & Thierry Petit (2004): Deriving filtering algorithms from constraint checkers. In: Mark Wallace, editor: Proceedings of CP’04, LNCS 3258. SpringerVerlag, pp. 107–122.
 [4] Nicolas Beldiceanu, Mats Carlsson & JeanXavier Rampon (2007): Global constraint catalogue: Past, present, and future. Constraints 12(1), pp. 21–62. Dynamic online version at www.emn.fr/xinfo/sdemasse/gccat. Long version as Technical Report T2005:08, Swedish Institute of Computer Science, November 2005.
 [5] Stéphane Bourdais, Philippe Galinier & Gilles Pesant (2003): HIBISCUS: A constraint programming application to staff scheduling in health care. In: Francesca Rossi, editor: Proceedings of CP’03, LNCS 2833. SpringerVerlag, pp. 153–167.
 [6] Kenil C. K. Cheng & Roland H. C. Yap (2008): Maintaining generalized arc consistency on ad hoc ary constraints. In: Peter J. Stuckey, editor: Proceedings of CP’08, LNCS 5202. SpringerVerlag, pp. 509–523.
 [7] WillemJan van Hoeve, Gilles Pesant & LouisMartin Rousseau (2006): On global warming: Flowbased soft global constraints. Journal of Heuristics 12(45), pp. 347–373.
 [8] Mikael Z. Lagerkvist (2008): Techniques for Efficient Constraint Propagation. Technical Report, KTH – The Royal Institute of Technology, Stockholm, Sweden. Licentiate Thesis.
 [9] Gilbert Laporte (1999): The art and science of designing rotating schedules. Journal of the Operational Research Society 50(10), pp. 1011–1017.
 [10] Laurent Michel & Pascal Van Hentenryck (1997): Localizer: A modeling language for local search. In: Gert Smolka, editor: Proceedings of CP’97, LNCS 1330. SpringerVerlag, pp. 237–251.
 [11] Gilles Pesant (2001): A filtering algorithm for the stretch constraint. In: Toby Walsh, editor: Proceedings of CP’01, LNCS 2239. SpringerVerlag, pp. 183–195.
 [12] Gilles Pesant (2004): A regular language membership constraint for finite sequences of variables. In: Mark Wallace, editor: Proceedings of CP’04, LNCS 3258. SpringerVerlag, pp. 482–495.
 [13] Benoit Pralong (2007): Implémentation de la contrainte Regular en Comet. Master’s thesis, École Polytechnique de Montréal, Canada.
 [14] JeanCharles Régin (1996): Generalized arcconsistency for global cardinality constraint. In: Proceedings of AAAI’96. AAAI Press, pp. 209–215.
 [15] Pascal Van Hentenryck & Laurent Michel (2005): ConstraintBased Local Search. The MIT Press.
 [16] Pascal Van Hentenryck & Laurent Michel (2006): Differentiable invariants. In: Frédéric Benhamou, editor: Proceedings of CP’06, LNCS 4204. SpringerVerlag, pp. 604–619.
 [17] Pascal Van Hentenryck, Laurent Michel & Liyuan Liu (2004): Constraintbased combinators for local search. In: Mark Wallace, editor: Proceedings of CP’04, LNCS 3258. SpringerVerlag, pp. 47–61.

[18]
Pascal Van Hentenryck, Vijay Saraswat &
Yves Deville (1993):
Design, implementation, and evaluation of the
constraint language cc(FD).
Technical Report CS9302,
Brown University, Providence, USA.
Revised version in
Journal of Logic Programming
37(1–3):293–316, 1998. Based on the unpublished manuscript Constraint Processing in cc(FD), 1991.
Comments
There are no comments yet.