When a high-level 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 low-level 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 user-level extensibility of CP languages has been an important goal for over a decade. In the traditional global search approach to CP (namely heuristic-based tree search interleaved with propagation), higher-level abstractions for describing new constraints include indexicals; (possibly enriched) deterministic finite automata (DFAs) via the  and  generic constraints; and multi-valued decision diagrams (MDDs) via the  generic constraint. Usually, a generic but efficient propagation algorithm achieves a suitable level of local consistency by processing the higher-level description of the new constraint. In the more recent local search approach to CP (called constraint-based local search, CBLS, in ), higher-level abstractions for describing new constraints include invariants ; a subset of first-order logic with arithmetic via combinators  and differentiable invariants ; and existential monadic second-order logic for constraints on set decision variables . Usually, a generic but incremental algorithm maintains the constraint and variable violations by processing the higher-level 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  are described by DFAs that are possibly enriched with counters and conditional transitions  (note that DFA generators can easily be written for other constraints, such as the  and  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 . 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 self-looping 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 non-empty sub-strings (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 sub-sections, 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.
A finite automaton is first unrolled for a given length of a sequence of decision variables, as in :
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 , and would be a good idea for the local search approach of ), 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 constant-time effort at each node. Once the pre-processing 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 pre-processing 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 non-deterministic 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 out-going 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.
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.
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 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.
Many industries and services need to function around the clock. Rotating schedules such as the one in Table 1 (a real-life example taken from ) are a popular way of guaranteeing a maximum of equity to the involved work teams (see ). 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 on-duty for each work shift, and two teams entirely off-duty; we denote this as ; assuming the work shifts average h, each employee will work h over the five-week-cycle, or h per week. Daily workload, whether uniform or not, can be enforced by global cardinality () constraints  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  and a circular constraint  on the table flattened row-wise 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 meta-heuristic, 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 off-duty. 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|
Minimum, maximum, average, standard deviation of optimisation times (in milliseconds) and numbers of iterations to the first solutions of rotating nurse schedules (100 runs) fromrandom 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 non-random 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 non-random 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|
. The difference is that they estimate the violation change compared to thenearest 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 ), 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 re-implementation of) , our method, and a SICStus Prolog constraint program (CP) where the product automaton of the and constraints was posted using the built-in propagation-based implementation of the constraint . These experiments show:
Compared with , 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 .
|optimisation time (ms)||number of iterations|
|instance||our method||||CP||our method|||
|St Louis Police||12740||11199||50261||48026||–||20287||17952||3248||2498|
|optimisation time (ms)||number of iterations|
|instance||our method||||our method|||
|St Louis Police||3990||4012||67159||55632||5389||5598||3949||3159|
Besides the rotating nurse instances, we ran experiments on another, harder real-life scheduling instance. The St Louis Police problem (described in ) has a seventeen-week-cycle; however it has more constraints than the rotating nurse problem. It has non-uniform 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 real-life 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  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.
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 constraint-based 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 .
There is of course a trade-off between using an automaton to describe a constraint and using a hand-crafted implementation of that constraint. On the one hand, a hand-crafted 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 meta-heuristic have been experimentally determined with its help, some extra efficiency may be achieved, if necessary, by hand-crafting 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 . 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 constraint-based 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 , possibly upon some concessions at the level of local consistency that can be achieved. In the Global Constraint Catalogue , 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.
The authors are supported by grant 2007-6445 of the Swedish Research Council (VR), and Jun He is also supported by grant 2008-611010 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].
-  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 CP-AI-OR’05, CP’05, and CP’06, published in LNCS 3524, 3709, and 4204).
-  Nicolas Beldiceanu, Mats Carlsson & Thierry Petit (2004): Deriving filtering algorithms from constraint checkers. In: Mark Wallace, editor: Proceedings of CP’04, LNCS 3258. Springer-Verlag, pp. 107–122.
-  Nicolas Beldiceanu, Mats Carlsson & Jean-Xavier Rampon (2007): Global constraint catalogue: Past, present, and future. Constraints 12(1), pp. 21–62. Dynamic on-line version at www.emn.fr/x-info/sdemasse/gccat. Long version as Technical Report T2005:08, Swedish Institute of Computer Science, November 2005.
-  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. Springer-Verlag, pp. 153–167.
-  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. Springer-Verlag, pp. 509–523.
-  Willem-Jan van Hoeve, Gilles Pesant & Louis-Martin Rousseau (2006): On global warming: Flow-based soft global constraints. Journal of Heuristics 12(4-5), pp. 347–373.
-  Mikael Z. Lagerkvist (2008): Techniques for Efficient Constraint Propagation. Technical Report, KTH – The Royal Institute of Technology, Stockholm, Sweden. Licentiate Thesis.
-  Gilbert Laporte (1999): The art and science of designing rotating schedules. Journal of the Operational Research Society 50(10), pp. 1011–1017.
-  Laurent Michel & Pascal Van Hentenryck (1997): Localizer: A modeling language for local search. In: Gert Smolka, editor: Proceedings of CP’97, LNCS 1330. Springer-Verlag, pp. 237–251.
-  Gilles Pesant (2001): A filtering algorithm for the stretch constraint. In: Toby Walsh, editor: Proceedings of CP’01, LNCS 2239. Springer-Verlag, pp. 183–195.
-  Gilles Pesant (2004): A regular language membership constraint for finite sequences of variables. In: Mark Wallace, editor: Proceedings of CP’04, LNCS 3258. Springer-Verlag, pp. 482–495.
-  Benoit Pralong (2007): Implémentation de la contrainte Regular en Comet. Master’s thesis, École Polytechnique de Montréal, Canada.
-  Jean-Charles Régin (1996): Generalized arc-consistency for global cardinality constraint. In: Proceedings of AAAI’96. AAAI Press, pp. 209–215.
-  Pascal Van Hentenryck & Laurent Michel (2005): Constraint-Based Local Search. The MIT Press.
-  Pascal Van Hentenryck & Laurent Michel (2006): Differentiable invariants. In: Frédéric Benhamou, editor: Proceedings of CP’06, LNCS 4204. Springer-Verlag, pp. 604–619.
-  Pascal Van Hentenryck, Laurent Michel & Liyuan Liu (2004): Constraint-based combinators for local search. In: Mark Wallace, editor: Proceedings of CP’04, LNCS 3258. Springer-Verlag, pp. 47–61.
Pascal Van Hentenryck, Vijay Saraswat &
Yves Deville (1993):
Design, implementation, and evaluation of the
constraint language cc(FD).
Technical Report CS-93-02,
Brown University, Providence, USA.
Revised version in
Journal of Logic Programming37(1–3):293–316, 1998. Based on the unpublished manuscript Constraint Processing in cc(FD), 1991.