Abstract
The goal of the Nurse Scheduling Problem is to find an assignment of nurses to shifts according to specific requirements. Frequently, a computed schedule may become not usable because of sudden absences of some nurses. In this cases, Nurse Rescheduling amounts to the computation of a new schedule, which has to satisfy the original requirements and the new absences. Additionally, a good solution to the Nurse Rescheduling Problem must be as similar as possible to the original schedule, which practically means that the number of changes has to be minimized. This paper focuses on the requirements specified by an Italian hospital, and recently addressed by an approach based on Answer Set Programming (ASP). Even if promising results have been obtained with ASP, the original encoding presents some intrinsic weaknesses, which are identified and eventually circumvented in this paper. The new encoding is designed by taking into account both intrinsic properties of Nurse Scheduling and internal details of ASP solvers, such as cardinality and weight constraint propagators. The performance gain of
Introduction
The Nurse Scheduling Problem (NSP) consists of generating a schedule of working and rest days for nurses employed in hospital units. The schedule should determine the shift assignments of nurses for a predetermined window of time, and must satisfy requirements imposed by the Rules of Procedure of hospitals. A proper solution to the NSP is crucial to guarantee the high level of quality of health care, to improve the degree of satisfaction of nurses, and the recruitment of qualified personnel. Given its practical relevance for the quality of hospital structures, NSP has been widely studied in the literature, and several variants have been considered [17, 22]. Such variants are usually grouped according to several factors, as the planning period, the different types of shifts considered, and requirements on the preferences of hospitals and nurses.
The NSP variant considered in this paper concerns a planning period fixed to one year with three different types of shifts (morning, afternoon and night) and requirements on nurses and hospitals provided by an Italian hospital. Specifically, such requirements concern restrictions to the number of working hours per year and to the number of times nurses are assigned to a specific shift.
However, in hospital units that operate 24 hours a day, 7 days for week, it is frequent that one planned schedule cannot be fulfilled due to sudden absences of nurses. The Nurse Rescheduling Problem (NRP), instead, occurs when one or more nurses notify their unavailability to attend one or more scheduled shifts, and aims at finding a new schedule taking into such unavailability, at the same time minimizing the differences with a previous computed schedule. It is important to emphasize here that rescheduling is usually independent from the quality of the schedule, and it is usually due to unpredictable events, e.g. illness of one or more nurses. When a sudden absence occurs, the planned schedule must be modified to ensure a good quality of the health care service. On the other hand, employees usually tend to organize their free time, thus modifications of a previously announced schedule may create personal inconveniences and may not be very well accepted by the workforce. Therefore, typically the schedule cannot be completely rebuilt from scratch. Instead, the goal should be to determine a new feasible scheduling minimizing the number of shift changes with respect to the previous schedule. Moreover, as argued in [23], rescheduling is a quite frequent activity and often requires reactive and immediate decisions. Thus, any approach aiming at solving the NRP should consider this additional requirement of being efficiently computed.
Complex combinatorial problems, such as NSP and NRP, are usually the target for the application of logic formalisms such as Answer Set Programming (ASP) [16]. Its simple syntax [18] and the intuitive semantics [34], combined with the availability of robust implementations (see, e.g. [4, 30]), make ASP an ideal candidate for addressing such problems. Indeed, ASP has been already successfully used for solving hard combinatorial and application problems in several research areas, including Artificial Intelligence [11, 25], Bioinformatics [27, 39], Hydroinformatics [28], Databases [42], and also employed in industrial applications [2, 26]. Recently, the aforementioned variant of NSP has been modeled by means of an ASP encoding presented in [24]. The encoding resulted to be natural and intuitive, in the sense that it was designed by applying the standard modeling methodology, yet obtaining reasonable performance on solving the analyzed instances.
On the other hand, it turned out that the encoding presented in [24] shows some limitations and intrinsic weaknesses, mainly due to aggregates [5], i.e. operations on multi-sets of weighted literals that evaluate to some value. Indeed, the encoding exploits some aggregates with a quite large number of literals and few different weights, resulting to be counterproductive for the performance of modern ASP solvers [32], since they decrease their propagation power.
In this paper, we circumvented such limitations by taking into account only combinations of values that can lead to admissible schedules. Interestingly, the new encoding did not require to significantly sacrifice readability, as it remains intuitive and clear. Moreover, the new encoding is used as basis for an ASP-based solution for NRP.
The performance of the ASP solvers executed on the new encoding for NSP has been empirically evaluated on the same data and settings used in [24], showing a clear improvement in performance of state-of-the-art ASP systems
The encoding for NRP has been tested by considering one previous schedule, and simulating sudden absences for nurses. Results on 150 random scenarios show that state-of-the-art ASP systems
The contributions of the paper can be summarized as follows: We formalize the variant of NSP considered in [24] (Section 4.1). We formalize the variant of NRP based on the variant of NSP considered in this paper (Section 4.2). We propose a new ASP-based solution to the NSP overcoming some limitations of the encoding presented in [24] (Section 5.2). We propose a new ASP-based solution to NRP (Section 6). We present an experimental analysis comparing the basic and advanced ASP solutions as well as with SAT and ILP based solutions (Section 7.1). Results show a significant improvement of the performance of ASP solvers and, specifically, We present an evaluation of rescheduling, which simulates sudden absences of nurses (Section 7.2). Results show that ASP systems can compute a solution for NRP in few seconds in our setting.
Preliminaries
We assume that the reader is familiar with basic knowledges on Answer Set Programming and
The evaluation of an ASP program is usually made in two steps, called grounding and solving. First, the ASP program with variables is evaluated by the grounder, which is responsible to produce its variable-free (propositional) counterpart.
The grounding of p consists of 60 (i.e. 6 × 10) propositional rules of the following form:
Intuitively, choice rulep1 enforces that exactly one atom between
The grounding of c consists of 30 (i.e. 10 × 3) (integrity) constraints of the following form:
As example, constraint c1 enforces that the count of true atoms among
Finally, the grounding of w consists of 180 (i.e. 6 × 10 × 3) weak constraints of the following form:
Weak constraints are used to express conditions that should be preferably satisfied. Thus, they may be violated. The semantics of programs with weak constraints imposes a minimization on violations. For instance, the informal meaning of w1 is “out(1,1,1) should preferably be false”. In addition, each weak constraint is associated to a weight and to a priority level (defined by the syntax [weight@level]). Optimal answer sets are those minimizing the sum of weights of the violated weak constraints at the highest priority level and, among them, those which minimize the sum of weights of the violated weak constraints in the next lower level. The process is applied recursively until the lowest level is reached.⊲
The propositional program produced by the grounder is evaluated by the solver, whose role is to produce an answer set. Modern ASP solvers implement the algorithm CDCL [32], which is based on the pattern choose-propagate-learn. Intuitively, the idea is to build an answer set step-by-step by starting from an empty interpretation, i.e. all atoms are initially undefined. Then, the algorithm heuristically chooses an undefined atom to be true in the answer set, and the deterministic consequences of this choice are propagated, i.e. new atoms are derived true or false in the answer set candidate. The propagation may lead to a conflict, i.e. an atom is true and false at the same time. In this case, the conflict is analyzed and a new constraint is added to the propositional program (learning). The conflict is then repaired, i.e. choices leading to the conflict are retracted and a new undefined atom is heuristically selected. The algorithm then iterates until no undefined atoms are left, i.e. an answer set is produced, or the incoherence of the propositional program is proved, i.e. no answer sets are admitted.
In presence of weak constraints, modern solvers apply a strategy based on the so-called unsatisfiable cores analysis. In particular, such an algorithm starts by searching an answer set satisfying all weak constraints, which would be therefore optimal. On the other hand, if such an answer set does not exist, a subset of the weak constraints that cannot be jointly satisfied is identified. Such a set is called unsatisfiable core, and essentially evidences that any optimal answer set must sacrifice at least one of the desiderata expressed by the weak constraints. Therefore, the program is modified by replacing the weak constraints in the unsatisfiable core with new weak constraints that essentially express a preference for answer sets satisfying all but one of the original weak constraints, and anyhow the largest number of them, so that the process can be reiterated.
Problems description
We start this section by providing a description of the Nurse Scheduling Problem as posed by an Italian hospital (Section 3.1). In the description we identify elements that allow to reuse the proposed solution even if part of the specification given by the hospital will change. After that, we describe the Nurse Rescheduling Problem as posed by the same Italian hospital (Section 3.2).
Nurse scheduling problem
NSP amounts to the totalization of partial schedules assigning nurses to working and rest days over a predetermined period of time, which is fixed to one year in this paper. Usually, partial schedules to be totalized involve few data concerning already authorized vacations. Admissible schedules must satisfy a set of requirements dictated by the rules of the hospital units. In the following, we report the requirements specified by an Italian hospital.
Hospital requirements. For every working day, nurses can be assigned to exactly one of the following shifts: morning (7 A.M. – 2 P.M.), afternoon (2 P.M. – 9 P.M.), night (9 P.M. – 7 A.M.). Thus, the morning and the afternoon shifts last 7 hours, whereas the night shift lasts 10 hours. In order to ensure the best assistance program for patients, the number of nurses in every shift x ∈ {morning, afternoon, night} must range from
Nurses requirements. In order to guarantee a fair workload, each nurse must work a number of hours ranging from work min to work max . Additional requirements are also imposed to ensure an adequate rest period to each nurse: (a) nurses are legally guaranteed 30 days of paid vacation; (b) the starting time of a shift must be at least 24 hours later than the starting time of the previous shift; and (c) each nurse has at least two ordinary rest days for every window of fourteen days. In addition, nurses working on two consecutive nights deserve one special rest day in addition to the ordinary rest days.
Balance requirements. The number of morning, afternoon and night shifts assigned to every nurse should range over a set of acceptable values, that is, from
Optimal balance requirements/ In addition to the above requirements, the hospital reported some further requirements to guarantee a balance in the assignment of shifts. Indeed, the number of morning, afternoon and night shifts assigned to every nurse should be preferably fixed to some desired values, that is, x day for each x ∈ {morning, afternoon, night}.
Nurse rescheduling problem
NRP addresses situations where an already computed schedule is not usable because of sudden absences of some nurses. Stated differently, nurses are following a previously computed schedule, and some of them report impossibility to work on some future days, for example because of health problems or personal issues. In such cases, the previously computed schedule has to be changed starting from a future day that must not follow any reported absence. The new schedule must clearly satisfy all requirements described in Section 3.1, with the exception that any absence due to health problems must not be rescheduled. Additionally, the new schedule must minimize the differences with the previous schedule, and such a minimization has priority over any other optimality requirements.
Problems formalization
The computational problems described in the previous section, namely the Nurse Scheduling Problem and the Nurse Rescheduling Problem, are formalized in Section 4.1 and 4.2, respectively. The formalization takes into account all parameters identified in the previous section, which are properly represented as part of the input.
Nurse scheduling problem
According to the requirements described in Section 3.1, here we define the decisional problem NSP d , and the optimization problem NSP o (which also takes into account the optimal balance requirements). To ease the reading, formulas are reported in Fig. 1.

Formulas involved in the Nurse (Re)Scheduling Problems.
Let us fix a set S = S
w
∪ {special - rest, rest, vacation} of shifts, where S
w
= {morning, afternoon, night} is the set of working shifts. Let us also fix natural numbers work
min
, work
max
, and
Hence, given a set N of nurses, and a partial schedule s′ of the form (1), NSP d amounts at establishing the existence of a schedule s of the form (2) extending s′ and satisfying (3)–(9); NSP o amounts at computing a schedule s of the form (2) satisfying (3)–(9), and minimizing (10).
According to the requirements described in Section 3.2, here we define two optimization problems, namely NRP d and NRP o , where the second also takes into account the optimal balance requirements.
In addition to sets and natural numbers fixed in the previous section, let us fix a set S p = {health - problem, personal - problem} of shifts to report impossibility for a nurse to work in a day, and a set S hp = {x hp ∣ x ∈ S w } of working shifts used to keep track of absences due to health problems.
Given a set N of nurses, a (previously computed) schedule s pre of the form (11), a partial schedule s req of the form (12) reporting problems, and a day start - date ∈ [1 . .365], NRP d amounts at computing a new schedule s new of the form (13) satisfying (3), (5)–(9), (14)–(18), and minimizing (19); NRP o amounts at computing a new schedule s new of the form (13) satisfying (3), (5)–(9), (14)–(18), and minimizing (19) and (10), in this order, that is, (19) is minimized first, and ties are possibly broken by minimizing (10).
Nurse scheduling via ASP
In this section, after recalling an existing encoding from the literature [24] (Section 5.1), we present the new advanced encoding (Section 5.2).
Existing encoding
Instances of NSP
d
and NSP
o
are represented by means of ASP facts and constants. Specifically, the interval [1 . .365] of days is encoded by facts of the form
The computed schedule is encoded by atoms of the form
The ASP encoding introduced in [24] is reported in Fig. 2. It implements the Guess&Check programming methodology: Choice rule r1 is used to guess the schedule s : N × [1 . .365] → [1 . .6] extending s′ and assigning each day of each nurse to exactly one shift, and rules r2–r13 are used to discard schedules not satisfying some of the desired requirements. Specifically, hospital requirements, formalized as property (3), are enforced by the integrity constraints r2 and r3, which filter out assignments exceeding the limits. Regarding nurse requirements, property (4) is enforced by integrity constraints r4 and r5, property (5) by integrity constraint r6, property (6) by integrity constraint r7, property (7) by integrity constraint r8, and property (8) by integrity constraint r9–r11. Note that r7 takes advantage of the numerical identifiers associated with shifts, and in particular by the fact that morning has id 1, afternoon has id 2, and night has id 3. Concerning balance requirements, formalized as property (9), they are enforced by integrity constraints r12 and r13. Rules r1–r13 encode NSP d , while for NSP o we also need weak constraint r14: It assigns a cost to each admissible schedule measured according to function (10). Optimum schedules are those minimizing such a cost.

ASP encoding introduced in [24] for NSP o (and for NSP d if r14 is removed).
The aim of this section is to introduce a new encoding, shown in Fig. 3, which improves the encoding reported in the previous section. First of all, it has to be noted that many constraints of the encoding in Fig. 2 only involve assignments to working shifts, that is, morning, afternoon and night. The Guess part of the encoding (i.e. rule r1) can thus be replaced by two different choice rules,

Advanced ASP encoding for NSP
o
(and for NSP
d
if
A second improvement is obtained by combining the knowledge represented by (4) and (9) with some observations on how rules r4 and r5 are evaluated. In fact, during the solving phase, rules obtained by instantiating r4 and r5 comprise aggregates with relatively large aggregation sets and few different weights. Specifically to our setting, where morning and afternoon shifts are fixed to 7 hours, and night shifts to 10 hours, each aggregation set contains 365 elements with weight 7, and 365 elements with weight 10. It turns out that several schedules result into exactly the same sum value. The question is now how many of these schedules actually satisfy both (4) and (9). Restricting to the specification given by the hospital, that is,
Number of working hours assigned to nurse n, that is, 7 · (M + A) +10 · N, where M = | {d ∈ [1 . .365] : s (n, d) = morning} |, A = | {d ∈ [1 . .365] : s (n, d) = afternoon} |, and N = | {d ∈ [1 . .365] : s (n, d) = night} |. Admissible values, that is, those in the interval [1687 . .1692], are emphasized in bold
Actually, rules
In the previous optimization, note that conjunction
Finally, a further improvement is obtained by checking the number of nonworking days assigned to each nurse. For the specification given by the hospital it must range between 149 and 150, and in general the admitted range can be determined by rule
The aim of this section is to introduce an encoding, shown on Fig. 4, for updating a given schedule due to a sudden absence of one or more nurses. In particular, the absence of nurses is encoded by atoms of the form

ASP encoding for rescheduling NRP
o
(and for NRP
d
if
The idea is to compute a new schedule, taking into account the unavailability of some nurses, such that differences with the previous schedule are minimized. To this end, rules s2 and s3 are used to compute the days for which the schedule of an unavailable nurse must be changed, corresponding to all working days between the range of unavailability provided by the nurse. Schedules for the days before starting date are not changed (rule s4). Then, nurses that are absent due to health problems cannot be assigned to other shifts (rules s5–s7), whereas nurses that are absent for personal problems are assigned to the shift rest (rules s8 and s9). The differences between the new schedule and the previous one are minimized by means of the weak constraint s10. The correctness of the new schedule is then checked using a slight different variant of the encoding reported in Fig. 3, where the only differences are in rule
In this section the results of the empirical evaluation are reported. The experiments take into account both the problem of generating a schedule, and the problem of repair a schedule to sudden absences. Both experiments were executed on the same computer equipped with four core Intel Xeon CPU X3430 2.4 GHz and 16 GB of RAM, running Debian Linux. Time and memory were limited to 1 hour and 8 GB, respectively. All ASP material can be found at http://www.star.dist.unige.it/∼marco/Data/material.zip.
Nurse scheduling
The experiments consider real data provided by the Italian hospital unit, which comprises a set of 41 nurses and holidays selected using the preferences of nurses of the year 2015. Moreover, the scalability of the approach has been evaluated by considering different number of nurses. In particular, an additional experiment was run by considering 10, 20, 41, 82 and 164 nurses without fixed holidays. Both the decisional (NSP d ) and the optimization (NSP o ) variants of NSP were considered. Concerning the decisional variant, the ASP-based approaches are compared to solutions based on SAT and on ILP.
The ASP encodings have been tested using the system
In order to test SAT and ILP solutions, a pseudo-Boolean formula based on the ideas of the advanced ASP encoding is created. The pseudo-Boolean formula was represented using the OPB format, which is parsed by the tool
Results. The results of the instance with parameters provided by the Italian hospital are reported in Table 2. The best result overall is obtained by
Results of the experiment with 41 nurses and fixed holidays
Results of the experiment with 41 nurses and fixed holidays
Scalability. A further analysis about the scalability of the encoding, considering different numbers of nurses, is reported in the following. In particular, for both NSP d and NSP o , five instances are considered, containing 10, 20, 41, 82 and 164 nurses, respectively. For each instance, the number of working nurses during each shift is proportionally scaled and holidays are randomly generated, whereas other requirements are not modified. Results are reported in Table 3.
Scalability of the approach. Solving time (s) for each solver
The best results overall is obtained again by

Comparison of the number of conflicts (in thousands) of
The experiments for rescheduling consider the schedule computed by
Absences ranging from 1 to 7 days are considered short; from 8 to 30 days are considered mid and from 30 days to one year are considered long. For each test, a random number of absent nurses is generated and for each nurse a range of unavailability is also randomly generated. In short term instances, the number of absent nurses is limited to 3, while for mid term and long term instances it is limited to 2 and 1, respectively. Moreover, short term instances include both health and personal problems, while mid term and long term include only health problems. An example of a short term instance is the following:
representing that nurses with ids 12 and 9 are not available for 3 days due to personal problems and health problems, respectively.
Since ASP has been shown to be the best solution for NSP, here we consider only this approach. The encodings have been tested using the system
Results. The results of the experiments are reported in Table 4. First of all, both
Average solving time (s) for clasp and wasp
Average solving time (s) for
In recent years, several approaches to solve NSP have been proposed. The main differences concern (i) the planning periods; (ii) the different type of shifts; (iii) the requirements related to the coverage of shifts, i.e. the number of personnel needed for every shift; and (iv) other restrictions on the rules of nurses (see [17] for more detailed information). In this paper a one-year window of time has been considered as in [21], where however the same requirements on nurses and hospitals were not reported. Concerning the shifts, we considered three different shifts (morning, afternoon and night) with no overlapping among shifts, whereas in the literature other approaches are based on one single shift only (see e.g. [43]). Other requirements depend on the different policies of the considered hospitals. Thus, this makes the different strategies not directly comparable with each other.
Concerning other solving technologies reported in the literature, they range from mathematical to meta-heuristics approaches, including solutions based on integer programming [10, 12], genetic algorithms [3], fuzzy approaches [51], and ant colony optimization algorithms [36], to mention a few. Detailed and comprehensive surveys on NSP can be found in [17, 22].
As far as the relation between the basic and advanced encodings, the two encodings mainly differ with respect to how the constraints related to hospital and balance requirements are modeled. Indeed, the new encoding takes into account only combinations of parameters values that can lead to a valid schedule.
As argued in two recent surveys [23, 47], NRP is considered strategic in many hospitals, since rescheduling is an almost daily activity and any change to previous schedule usually faces unexpected resistance. Thus, most of the approaches are based on minimizing the changes with the previous schedule as also considered in this paper.
Many solutions have been proposed in the literature [23, 47], including heuristic approaches [38, 48], genetic algorithms [46], parallel algorithms [14], artificial immune systems [41], and integer multicommodity flow formulations [45].
In [13], authors considered the possibility of hiring temporary staff, called travelers, for dealing with sudden absences of nurses. It turns out that this approach is needed in hospitals facing a chronic nursing shortage, i.e. when permanent nurses cannot fulfill the requirements imposed by the hospital, which is not the case of the hospital considered here.
Finally, ASP encodings have been proposed for scheduling problems other than NSP and NRP: Incremental Scheduling Problem [19], where the goal is to assign jobs to devices such that their executions do not overlap one another; and Team Building Problem [50], where the goal is to allocate the available personnel of a seaport for serving the incoming ships. However, to the best of our knowledge, the only ASP encodings for NSP and NRP are those shown in Sections 5.1, 5.2 and 6.
Conclusion
In this paper an advanced ASP encoding for addressing a variant of NSP has been proposed. The new encoding overcomes the limitations of the one proposed in [24] by taking into account intrinsic properties of NSP and internal details of ASP solvers. The new NSP encoding has been used as basis for an ASP-based solution for solving NRP. The ASP-based approach to NSP has been compared with the basic one [24] and with other declarative approaches on real setting provided by an Italian hospital. Results clearly show that
Moreover, we also observe that the techniques presented in this paper might be also used for similar encodings. In particular, if the problem at hand involves several constraints on aggregated values, a predicate like countGE proposed in Section 5.2 may help ASP solvers to better prune the search space; on the other hand, the programmer should take into account the effective size of the instantiation of such a predicate, and evaluate whether it is affordable or not.
As future work, we plan to extend our ASP encoding in order to take into account also other requirements [47]. In particular, it would be interesting to evaluate the performance of the ASP encodings on the instances of the international nurse scheduling competitions [20, 37].
Footnotes
Acknowledgments
We would like to thank Nextage srl for providing support for this work. Mario Alviano has been partially supported by the Italian Ministry for Economic Development (MISE) under project “PIUCultura – Paradigmi Innovativi per l’Utilizzo della Cultura” (n. F/020016/01-02/X27), and under project “Smarter Solutions in the Big Data World (S2BDW)” (n. F/050389/01-03/X32) funded within the call “HORIZON2020” PON I&C 2014-2020, and by Gruppo Nazionale per il Calcolo Scientifico (GNCS-INdAM).
