Introduction

Balancing constraints arise in many real-world applications, most often to express the need of a fair distribution of items or work. Simonis (2007) suggested a global constraint to balance the shift distribution among nurses, and Pesant (2008) proposed the use of balancing constraints for a fair allocation of individual schedules.

Two global constraints and their propagators are available in constraint programming for optimizing load balancing: spread (Pesant and Régin 2005; Schaus et al. 2006), which constrains the variance and the mean of a set of variables, and deviation (Schaus et al. 2007a, b), which constrains the mean absolute deviation and the mean of a set of variables. We also say that spread and deviation, respectively, constrain the \(L_2\) and \(L_1\) norms of a set of variables \(X_1..X_n\) with respect to their mean (\(s=\sum _{i \in [1..n]} X_i\)), i.e.,

  • \(L_1\): \(\sum _{i \in [1..n]} |X_i-s/n|\);

  • \(L_2\): \(\sum _{i \in [1..n]} (X_i-s/n)^2\).

These criteria are not equivalent: Minimizing \(L_1\) or \(L_2\) does not lead to the same solutions and it is not always obvious which one to choose. In fact, this is an old and recurrent debate (see for instance Gorard 2005). The \(L_2\) criteria is more sensitive to outliers.

Contributions This article improves the filtering algorithm for the spread constraint. The initial filtering introduced in Pesant and Régin (2005) is for a very general version of spread including a variable mean and a variable representing the median. Unfortunately, this algorithm has never been implemented and is very difficult to understand.Footnote 1 A simplified version with a fixed constant mean was introduced in Schaus et al. (2006). As in Pesant and Régin (2005), this algorithm achieves a relaxed version of the bound-consistency assuming continuous interval domains. We introduce a stronger filtering achieving the classical bound-consistency on integer domains. We give all the details (proofs and pseudocode) of the intermediate results introduced in Pesant and Régin (2005), Schaus et al. (2006) and reused in our new algorithm. Our algorithm is explained visually and illustrated on numeric examples for each step; making is possible for someone to re-implement it. Our implementation is freely available in OscaR solver (OscaR Team 2012).

As an application for spread, we revisit a problem of nurse-to-patient assignment first introduced in Mullinax and Lawley (2002) and solved with CP in Schaus et al. (2009). This problem considers a fair distribution of the workload assigned to nurses. We use the two-step decomposition approach from Mullinax and Lawley (2002) computing first the number of nurses allocated to each zone before solving the problem in each zone independently. We prove that the first step can be solved optimally by solving a resource allocation problem. This proof was missing in Schaus et al. (2009). Finally, we show that for some instances, we can prove the two-step approach to be optimal using a new optimality check procedure for this problem.

Organization “Definition” formally defines the consistency levels achieved by the filtering algorithms from Pesant and Régin (2005), Schaus et al. (2006) and our new algorithm. “Filtering for spread” reviews the existing filtering algorithm and explains in detail the new filtering algorithms achieving bound-consistency for spread. “Nurse-to-patient assignment problems” introduces the two-step decomposition approach to solve the nurse-to-patient assignment problem and experimental results using the new filtering for spread.

Definition

Preliminaries We use the following definitions and notations to describe the semantics of the spreadconstraint and propagators.

Definition 1

Let \(X\) be a finite-domain (discrete) variable. The domain of \(X\) is a set of ordered values that can be assigned to \(X\) and is denoted by \(\hbox {Dom}(X)\). The minimum (resp. maximum) value of the domain is denoted by \(X^{\min }=\min (\hbox {Dom}(X))\) (resp. \(X^{\max }=\max (\hbox {Dom}(X))\). An integer interval with integer bounds \(a\) and \(b\) is denoted \([a..b] \subseteq \mathbb {Z} \), while a rational interval is denoted \([a,b] \subseteq \mathbb {Q} \). An assignment on the variables \(\mathbf X =[X_1,X_2,\ldots ,X_n]\) is denoted by the tuple \(\mathbf {x}\) and the \(i\)-th entry of this tuple by \(\mathbf {x}[i]\). The extended rational interval domain of \(X_i\) is \(I_D^{\mathbb {Q} }(X_i)=[X_i^{\min },X_i^{\max }]\) and its integer interval domain is \(I_D^{\mathbb {Z} }(X_i)=[X_i^{\min }\ ..\ X_i^{\max }]\). Since we always consider \(n\) variables, when not specified otherwise our summations always range over \([1..n]\) i.e., \(\sum _i\) is equivalent to \(\sum _{i \in [1..n]}\). Similarly when it is possible we simplify \(\forall i \in [1..n]\) into \(\forall i\).

Before defining formally the spread constraint, observe that if \(\sum _{i}X_i = s\) then:

$$\begin{aligned} n \cdot \sum _{i}|X_i-s/n|^2= n \cdot \sum _{i}X_i^2 -s^2. \end{aligned}$$
(1)

It means that adding a constraint on \(L_2\) criteria with a fixed sum value is equivalent to adding a constraint on \(\sum _{i}X_i^2\), since \(s\) and \(n\) are constants. But working with the latter is more convenient when dealing with finite domain integer variables. We now define the spread constraint with a fixed mean.

Definition 2

Given finite domain variables \(\mathbf X =(X_1,X_2,\ldots ,X_n)\), an integer value \(s\) and a finite domain variable \(\varDelta \), \(\mathtt{spread }(\mathbf X ,s,\varDelta )\) holds if and only if

$$\begin{aligned} \sum _{i}X_i = s \quad \mathrm {and} \quad \sum _{i}X_i^2 \le \varDelta . \end{aligned}$$

Example 1

Tuple \(\mathbf {x}=(2,3,3,2) \in \mathtt{spread }([X_1,X_2,X_3,X_4],s=10,\varDelta =26)\) but \(\mathbf {x}=(1,4,3,2) \notin \mathtt{spread }([X_1,X_2,X_3,X_4],s=10,\varDelta =26)\) because \(1^2+4^2+3^2+2^2=30>26\).

The definition of bound-consistency specialized for continuous and integer interval domains is given next:

Definition 3

(\(\mathbb {Q} \)-bound-consistency and \(\mathbb {Z} \)-bound-consistency) A constraint \(C(X_1,\ldots ,X_n)\) (\(n>1\)) is \(\mathbb {Q} \)-bound-consistent (resp. \(\mathbb {Z} \)-bound-consistent) with respect to domains \(\mathrm{Dom}(X_i)\) if for all \(i \in \{1,\ldots ,n\}\) and each value \(v_i \in \{X_i^{\min }, X_i^{\max } \}\), there exist values \(v_j \in I_D^{\mathbb {Q} }(X_j)\) (resp. \(v_j \in I_D^{\mathbb {Z} }(X_j)\)) for all \(j \in \{1,\ldots ,n\}-\{i\}\) such that \((v_1,\ldots ,v_n) \in C\).

Note that for the \(L_1\) criterion, the \(\mathbb {Q} \)-bound-consistency filtering was introduced in Schaus et al. (2007b) and the \(\mathbb {Z} \)-bound-consistency filtering in Schaus et al. (2007a). The \(\mathbb {Q} \)-bound-consistency filtering for spread is described in Pesant and Régin (2005), Schaus et al. (2006). Our contribution is a stronger \(\mathbb {Z} \)-bound-consistency filtering for integer domains obtained by adapting the algorithm from Schaus et al. (2006).

We illustrate in the next example that the \(\mathbb {Z} \)-bound-consistency for spread can be stronger than the \(\mathbb {Q} \)-bound-consistency:

Example 2

Consider ten variables with domains all equal to \([1..2]\) and a sum that must be equal to \(15\). An assignment (from the extended continuous domain) minimizing the sum of squares would assign all of them to \(1.5\). The lower bound on the sum of square variables will thus be set to \(10 \cdot 1.5^2 = 22.5\). An integer assignment minimizing the sum of squares would assign five variables to \(2\) and the other five to \(1\). The lower bound on the sum of square variables would thus be set to \(5 \cdot 2^2 + 5 \cdot 1^2= 25\).

To achieve \(\mathbb {Z} \)-bound-consistency, the propagators for spread compute \(\underline{\varDelta }^\mathbb {Z} \) to filter \(\varDelta ^{\min }\), and \(\overline{X}_{i}^\mathbb {Z} \) and \(\underline{X}_{i}^\mathbb {Z} \) to filter \(X_i^{\max }\) and \(X_i^{\min }\):

$$\begin{aligned} \underline{\varDelta }^\mathbb {Z} \!&= \!\min _\mathbf {x}\left\{ \sum _{i}\mathbf {x}[i]^2 \ \mathrm {s.t.} \ \sum _{i}\mathbf {x}[i]\!=\!s \ \quad \mathrm {and}\ \quad \forall i:\mathbf {x}[i] \in I_D^{\mathbb {Z} }(X_i)\right\} \end{aligned}$$
(2)
$$\begin{aligned} \overline{X}_{i}^\mathbb {Z} \!&= \!\max _{\mathbf {x}}\left\{ \mathbf {x}[i] \ \ \mathrm {s.t.} \ \ \sum _{j} \mathbf {x}[j]^2 \le \varDelta ^{\max }\ \quad \mathrm {and}\quad \sum _{j} \mathbf {x}[j]\!=\!s \ \quad \mathrm {and}\quad \ \forall j: \mathbf {x}[j] \!\in \! I_D^{\mathbb {Z} }(X_j)\right\} .\nonumber \\ \end{aligned}$$
(3)

Note that \(\underline{X}_{i}^\mathbb {Z} \) is defined similarly. The filtering of \(\varDelta \) is implemented in time complexity \({\mathcal O}(n\cdot \log (n))\) and that of \(\mathbf X \) in \({\mathcal O}(n^2)\). The next section explains how to compute these quantities efficiently to filter \(\varDelta \) and \(X_i\)’s.

Filtering for spread

First, we explain in detail the \({\mathcal O}(n \cdot \log (n))\) algorithm from Pesant and Régin (2005) to compute \(\underline{\varDelta }^\mathbb {Q} \). Then we adapt this algorithm to compute \(\underline{\varDelta }^\mathbb {Z} \). The key point of the algorithm given in the next theorem (in the \(\mathbb {Q} \) context) is a clear characterization of an optimal solution to the problem of minimization of the sum of squares with a given sum. This theorem says that in an assignment of sum \(s\) minimizing the sum of squares, there cannot exist two values that can be made closer by moving both of them inside their corresponding domains.

Theorem 1

\( \mathbf {x}\in \mathrm{argmin}_\mathbf {y}\{ \sum _{i}\mathbf {y}[i]^2 \ \mathrm {s.t.} \ \sum _{i}\mathbf {y}[i]=s \ \mathrm {and}\ \forall i: \mathbf {y}[i] \in I_D^{\mathbb {Q} }(X_i) \} \Leftrightarrow \sum _{i}\mathbf {x}[i]=s\) and \( \not \exists \ (i,j)\) such that: \(i\ne j\), \(\mathbf {x}[i]<X_i^{\max }\), \(\mathbf {x}[j]>X_j^{\min }\) and \(\mathbf {x}[i]<\mathbf {x}[j]\).

Proof

We denote by \(\hbox {opt}(\mathbf {x})\) the left member of the bi-conditional symbol and by \(\lnot p(\mathbf {x})\) the right member.

  • (\(\Rightarrow \)) \(p(\mathbf {x}) \Rightarrow \lnot \hbox {opt}(\mathbf {x})\): Assume it is possible to find a pair \(i,j\) such that \(\mathbf {x}[i]<X_i^{\max }\), \(\mathbf {x}[j]>X_j^{\min } \) and \(\mathbf {x}[i]<\mathbf {x}[j]\), then it is possible to transform the assignment \(\mathbf {x}\) into a assignment \(\mathbf {x}'\) also of sum \(s\) with \(\sum _{i}\mathbf {x}'[i]^2 < \sum _{i}\mathbf {x}[i]^2\). We define the positive value \(\delta =\min \{((\mathbf {x}[j]-\mathbf {x}[i])/2),( X_i^{\max }-\mathbf {x}[i]),( \mathbf {x}[j]-X_j^{\min }) \}\) and \(\mathbf {x}'[k]=\mathbf {x}[k] \ \forall k \ne i,j\), \(\mathbf {x}'[j]=\mathbf {x}[j]-\delta \), \(\mathbf {x}'[i]=\mathbf {x}[i]+\delta \). If we define \(\varDelta (\mathbf {x})= \sum _{i}\mathbf {x}[i]^2\), then \(\varDelta (\mathbf {x})-\varDelta (\mathbf {x}')= -2 \delta ^2+2 \delta \cdot ( \mathbf {x}[j]- \mathbf {x}[i])\ge \delta ^2>0\) because \((\mathbf {x}[j]-\mathbf {x}[i]) \ge 2\delta \).

  • (\(\Leftarrow \)) \(\lnot \hbox {opt}(\mathbf {x}) \Rightarrow p(\mathbf {x})\): We assume that \(\mathbf {x}\) is not optimal and we consider \(\mathbf {x}'\) a modified version of \(\mathbf {x}\) also of sum \(s\). Without loss of generality, we consider that the first \(k\) entries were increased, the next \(l\) were decreased and the other were left unchanged: \((\mathbf {x}'[i]-\mathbf {x}[i])=\delta _i>0\) for \(i \in [1..k]\), \((\mathbf {x}[i]-\mathbf {x}'[i])=\delta _i>0\) for \(i \in [k+1..k+l]\) and \(\mathbf {x}[i]=\mathbf {x}'[i]\) for \(i \in [(k+l+1)..n]\). The sum \(s\) is preserved from \(\mathbf {x}\) to \(\mathbf {x}'\) if \(\sum _{i=1}^k \delta _i-\sum _{i=k+1}^{k+l} \delta _i=0\). The difference between the sum of squares can be written as:

    $$\begin{aligned} \varDelta (\mathbf {x})-\varDelta (\mathbf {x}')= - \sum _{i=1}^{k+l} \delta _i^2-2\cdot \sum _{i=1}^{k} \mathbf {x}[i]\delta _i+2 \cdot \sum _{i=k+1}^{k+l} \mathbf {x}[i]\delta _i. \end{aligned}$$

    We show that this difference is strictly negative (i.e., \(\mathbf {x}'\) does not improve \(\mathbf {x}\)) if every increasing entry is larger than every decreasing entry, \(\forall i \in [1..k], \forall j \in [k+1..k+l]: \ \mathbf {x}[i]>\mathbf {x}[j]\). A lower bound on \(( \sum _{i=1}^{k} \mathbf {x}[i]\delta _i)\) is \(( (\min _{i \in [1..k]} \mathbf {x}[i]) \cdot \sum _{i=1}^{k} \delta _i)\) and a strict upper bound on \(( \sum _{i=k+1}^{k+l} \mathbf {x}[i]\delta _i)\) is \( ((\min _{i \in [1..k]} \mathbf {x}[i]) \cdot \sum _{i=k+1}^{k+l} \delta _i)\). Hence we have:

    $$\begin{aligned} \varDelta (\mathbf {x})-\varDelta (\mathbf {x}')&< \left( -\sum _{i=1}^{k+l} \delta _i^2\right) +2 \cdot \left( \min _{i \in [1..k]} \mathbf {x}[i]\right) \cdot \left( -\sum _{i=1}^{k} \delta _i+\sum _{i=k+1}^{k+l} \delta _i\right) \nonumber \\&= - \sum _{i=1}^{k+l} \delta _i^2 <0 \end{aligned}$$

    because \(\sum _{i=1}^k \delta _i-\sum _{i=k+1}^{k+l} \delta _i=0\).

\(\square \)

An assignment \(\mathbf {x}\) such that \( \not \exists \ i,j\) with \(\mathbf {x}[i]<X_i^{\max }\), \(\mathbf {x}[j]>X_j^{\min } \) and \(\mathbf {x}[i]<\mathbf {x}[j]\) is called a \(\nu \)-\(\mathbb {Q} \)centered assignment in Pesant and Régin (2005):

Definition 4

(\(\nu \)-\(\mathbb {Q} \) centered assignmentPesant and Régin 2005) A \(\nu \)-\(\mathbb {Q} \)centered assignment \(\mathbf {x}\in \mathbb {Q} ^n\) on \(\mathbf X \) with \(\nu \in \mathbb {Q} \) is such that \(\forall i\) :

$$\begin{aligned} \mathbf {x}[i]= \left\{ \begin{array}{ll} X_i^{\max } &{}\quad \mathrm {if}\ X_i^{\max } < \nu \\ X_i^{\min } &{}\quad \mathrm {if}\ X_i^{\min } > \nu \\ \nu &{}\quad \mathrm{otherwise}. \end{array} \right. \end{aligned}$$

Corollary 1

(Pesant and Régin 2005) \(\mathbf {x}\in \hbox {argmin}_\mathbf {y}\{ \sum _{i}\mathbf {y}[i]^2 \ \mathrm {s.t.} \ \sum _{i}\mathbf {y}[i]=s \ \mathrm {and}\ \forall i :\mathbf {y}[i] \in I_D^{\mathbb {Q} }(X_i) \}\) if and only if \(\mathbf {x}\) is a \(\nu \)-\(\mathbb {Q} \)centered assignment of sum \(s\).

The optimal value \(\underline{\varDelta }^\mathbb {Q} \) can be obtained from a tuple which is \(\nu \)-\(\mathbb {Q} \)centered and exhibiting a sum of \(s\). Note that by definition of a \(\nu \)-\(\mathbb {Q} \)centered assignment, this tuple is completely defined by the value \(\nu \). The algorithm from Pesant and Régin (2005) searches for the value \(\nu \) such that the \(\nu \)-\(\mathbb {Q} \)centered assignment exhibits a sum of \(s\).

For a given value \(\nu \), the sum of the corresponding \(\nu \)-\(\mathbb {Q} \)centered assignment is

$$\begin{aligned} \sum _{X_i^{\min }>\nu } X_i^{\min }+\sum _{X_i^{\max }<\nu } X_i^{\max }+\sum _{\nu \in I_D^{\mathbb {Q} }(X_i)} \nu . \end{aligned}$$
(4)

In order to simplify the notations, we denote by:

  • \(R(\nu )\), the (Right) variables \(\{X_i \ \mathrm {s.t.} \ X_i^{\min }> \nu \}\),

  • \(L(\nu )\), the (Left) variables \(\{X_i \ \mathrm {s.t.} \ X_i^{\max }<\nu \}\) and by

  • \(M(\nu )\), the (Medium) variables \(\{X_i \ \mathrm {s.t.} \ X_i \notin (R(\nu ) \cup L(\nu ))\}\).

The cardinality of the sets \(R(\nu )\), \(L(\nu )\) and \(M(\nu )\) is denoted, respectively, \(r(\nu )\), \(l(\nu )\) and \(m(\nu )\). Let us furthermore define

  • \(\mathrm{es}(\nu )=\sum _{X_i \in R(\nu )} X_i^{\min }+\sum _{X_i \in L(\nu )} X_i^{\max }\) (Extrema-Sum), and

  • \(\mathrm{es}_{(2)}(\nu )=\sum _{X_i \in R(\nu )} (X_i^{\min })^2+\sum _{X_i \in L(\nu )} (X_i^{\max })^2\).

When no ambiguity is possible, the \(\nu \) argument will be dropped.

With the introduced notations, the sum of the \(\nu \)-\(\mathbb {Q} \)centered assignment (4) can be simply written as \(\mathrm{es}+m \cdot \nu \). Our objective to find the lower bound \(\underline{\varDelta }^\mathbb {Q} \) is achieved if we can find a value \(\nu \) such that \(\mathrm{es}+m \cdot \nu =s\).

Non-empty intervals such that every domain either completely subsumes it or does not overlap it, have the property that for every value \(\nu \) inside it, the sets \(R(\nu )\), \(L(\nu )\), \(M(\nu )\) and the value \(\mathrm{es}(\nu )\) are constant. This is more formally stated in Property 1.

Property 1

All intervals \(I=[a,b] \subset \mathbb {Q} \) with \(a<b\) such that \(\forall i: ( I \subseteq I_D^{\mathbb {Q} }(X_i)) \vee ( I \ge I_D^{\mathbb {Q} }(X_i)) \vee (I \le I_D^{\mathbb {Q} }(X_i)) \) have the property that for every \(a<(\nu ^1,\nu ^2)<b\) the following equalities hold: \(R(\nu ^1)=R(\nu ^2)\), \(L(\nu ^1)=L(\nu ^2)\), \(M(\nu ^1)=M(\nu ^2)\) and \(\mathrm{es}(\nu ^1)=\mathrm{es}(\nu ^2)\).

Proof

Direct consequences of the definitions of \(R\), \(L\), \(M\) and \(\mathrm{es}\).

Property 1 leads naturally to an extension of the definitions of \(R\), \(L\), \(M\) and \(\mathrm{es}\) to intervals \(I\) such as the ones considered in Property 1:

  • \(R(I)\), the variables \(\{X_i \ \mathrm {s.t.} \ X_i^{\min } \ge \max (I) \}\),

  • \(L(I)\), the variables \(\{X_i \ \mathrm {s.t.} \ X_i^{\max } \le \min (I) \}\),

  • \(M(I)\), the variables \(\{X_i \ \mathrm {s.t.} \ X_i^{\min } \le \min (I) \ \mathrm {and}\ X_i^{\max } \ge \max (I) \}\) and

  • \(\mathrm{es}(I)=\sum _{X_i \in L(I)} X_i^{\max }+\sum _{X_i \in R(I)} X_i^{\min } \).

Note that \(M(I)=\mathbf X -(R(I) \cup L(I))\).

When \(\nu \) varies inside such an interval \(I\), the sum ranges in the interval \(\mathrm{si}(I)=[\mathrm{es}(I)+m(I) \cdot \min (I),\mathrm{es}(I)+m(I) \cdot \max (I)]\) (\(\mathrm{si}\) stands for Sum Interval). If \(s\) does not fall in \(\mathrm{si}(I)\) then the value \(\nu \) we are looking for does not lie inside \(I\) neither. On the contrary if \(s\) belongs to this interval, \(\nu \) is the solution of the equation \(s=\mathrm{es}+m \cdot \nu \) that is \(\nu =(s-\mathrm{es})/m\).

There are at most \(2\cdot n-1\) intervals \(I\) to consider (when all the bounds are different). These are obtained by sorting the set of upper and lower bounds of every variable \(X_i\) into increasing order. Any two consecutive values of this sorted sequence form an interval \(I\) satisfying Property 1.

Definition 5

Let \(B(\mathbf X )\) be the sorted sequence in non-decreasing order of the set of boundsFootnote 2 \(\bigcup _i \{X_i^{\min },X_i^{\max }\}\). Let \({\mathcal I}(\mathbf X )\) be the set of intervals defined by a pair of two consecutive elements of \(B(\mathbf X )\). The \(k^{th}\) interval of \({\mathcal I}(\mathbf X )\) is denoted by \(I_k\). For an interval \(I=I_k\), we define the operator \(\mathrm {prev}(I)=I_{k-1}, (k>1)\) and \(\mathrm {succ}(I)=I_{k+1}, (k<|{\mathcal I}(\mathbf {X})|)\).

Example 3

(Building \(I(\mathbf X )\)) Let \(\mathbf X =\{X_1,X_2,X_3\}\) with \(\mathrm{Dom}(X_1)=[1..3]\), \(\mathrm{Dom}(X_2)=[2..6]\) and \(\mathrm{Dom}(X_3)=[3..9]\). Then \({\mathcal I}(X)=\{I_1,I_2,I_3,I_4\}\) with \(I_1=[1,2]\), \(I_2=[2,3]\), \(I_3=[3,6]\), \(I_4=[6,9]\). We have \(\mathrm {prev}(I_3)=I_2\) and \(\mathrm {succ}(I_3)=I_4\).

Observe that for two consecutive intervals \(I_k\) and \(I_{k+1}\) taken from \({\mathcal I}(\mathbf X )\), the sum intervals are also contiguous: \(\max (\mathrm{si}(I_k))=\min (\mathrm{si}(I_{k+1}))\). It is then possible to make an algorithm to filter the lower bound of \(\varDelta \). Algorithm 1 computes \(\underline{\varDelta }^\mathbb {Q} \) by iterating over the contiguous intervals \(\mathrm{si}(I_k)\) until the sum lies inside it.

figure a

Algorithm 1 executes in \({\mathcal O}(n)\) once \({\mathcal I}(\mathbf X )\) is computed and that \(\mathrm{es}(I)\) and \(m(I)\) are available for every \(I \in {\mathcal I}(\mathbf X )\). Unfortunately, computing \({\mathcal I}(\mathbf X )\) requires to sort the bounds, hence \({\mathcal O}(n \log (n))\) time. Algorithm 1 also needs the values \(\mathrm{es}(I)\) and \(m(I)\). For a given \(I\), these can be obtained in \(\varTheta (n)\) by scanning every variable once. This would raise the overall complexity of Algorithm 1 to \({\mathcal O}(n^2)\) since in the worst case, \(\mathrm{es}(I)\) and \(m(I)\) must be computed for every \(I \in {\mathcal I}(\mathbf X )\). A smarter procedure is possible to compute \(\mathrm{es}(I)\) and \(m(I)\) in linear time for every \(I \in {\mathcal I}(\mathbf X )\). Lemma 1 explains how the values \(\mathrm{es}(I)\) can be computed in linear time for all \(I \in {\mathcal I}(\mathbf X )\) once \(l(I), r(I)\) are given for all \(I \in {\mathcal I}(\mathbf X )\). Finally, Algorithm 2 computes \(l(I)\), \(r(I)\) and \(m(I)\) for all \(I \in {\mathcal I}(\mathbf X )\) in linear time too. This last algorithm can be easily understood with the invariant given in the pseudocode.

Lemma 1

(Pesant and Régin 2005) \(\mathrm{es}(I_{k+1})=\mathrm{es}(I_k)+(p_{k+1}-q_{k+1})\cdot \max (I_k)\) where \(p_{k+1}=l(I_{k+1})-l(I_k)\) and \(q_{k+1}=r(I_k)-r(I_{k+1})\).

Note that \(lc \) is initialized at line 1 with \(b_1^+\) and not \(0\) because some variables might already be bound to value \(b_1\).

figure b

The complete filtering algorithm dominated by a complexity of \({\mathcal O}(n\log (n))\) is:

  • sort the bounds (\({\mathcal O}(n\log (n))\)),

  • compute \({\mathcal I}(\mathbf X )\) and for all \(I \in {\mathcal I}(\mathbf X ): r(I)\), \(l(I)\) and \(m(I)\) with Algorithm 2 (\(\varTheta (n)\)),

  • compute \(\mathrm{es}(I)\) for all \(I \in {\mathcal I}(\mathbf X )\) with Lemma 1 (\({\mathcal O}(n)\)),

  • filter \(\varDelta \) with Algorithm 1 (\({\mathcal O}(n)\)).

Example 4

(Computing \(\underline{\varDelta }^\mathbb {Q} \)) Variables and domains are from Example 3 and the sum considered is \(s=10\). Relevant values necessary to compute \(\underline{\varDelta }^\mathbb {Q} \) are given in Table 1. Since \(s \in \mathrm{si}(I_3)\): \(\nu =(s-\mathrm{es})/m=(10-3)/2=3.5\) and \(\underline{\varDelta }^\mathbb {Q} = \mathrm{es}_{(2)}(I_3)+m(I_3)\cdot 3.5^2=33.5\). For \(s=9\), \(s \in \mathrm{si}(I_2)\) and \(s \in \mathrm{si}(I_3)\). Whichever interval is chosen between \(I_2\) and \(I_3\), the value \(\nu \) is the same. Consequently the value for \(\underline{\varDelta }^\mathbb {Q} \) is also the same.

Table 1 Values \(I_i\), \(R(I_i)\), \(L(I_i)\), \(M(I_i)\), \(\mathrm{es}(I_i)\), \(\mathrm{es}_{(2)}(I_i)\), \(\mathrm{si}(I_i)\) relative to Example 4

The remainder of the section explains how to compute \(\underline{\varDelta }^\mathbb {Z} \ge \lceil \underline{\varDelta }^\mathbb {Q} \rceil \) that will allow us to achieve \(\mathbb {Z} \)-bound-consistency rather than \(\mathbb {Q} \)-bound-consistency on spread.

As shown in the next theorem, the optimal solution obtained with integer assignments is very similar to the one obtained on rational domains in Theorem 1.

Theorem 2

\(\mathbf {x}\in \hbox {argmin}_\mathbf {y}\{ \sum _{i}\mathbf {y}[i]^2 \ \mathrm {s.t.} \ \sum _{i}\mathbf {y}[i]=s \, \mathrm {and}\, \forall i: \mathbf {y}[i] \in I_D^{\mathbb {Z} }(X_i) \}\) \(\Leftrightarrow \) \(\sum _{i}\mathbf {x}[i]=s\) and \( \not \exists \ (i,j)\) such that: \(i\ne j\), \(\mathbf {x}[i]<X_i^{\max }\), \(\mathbf {x}[j]>X_j^{\min } \) and \(\mathbf {x}[i]+1<\mathbf {x}[j]\).

Proof

Similar to the proof of Theorem 1:

  • (\(\Rightarrow \)) Take \(\delta =1\).

  • (\(\Leftarrow \)) All the \(\delta _i\)’s are integer and greater or equal to 1. We show that the difference \((\varDelta (\mathbf {x})-\varDelta (\mathbf {x}'))\) is non-positive if every increasing entry is at most one smaller than every decreasing entry, \(\forall i \in [1..k], \forall j \in [k+1..k+l]: \ \mathbf {x}[i]+1 \ge \mathbf {x}[j]\). A lower bound on \(( \sum _{i=1}^{k} \mathbf {x}[i]\delta _i)\) is \(( (\min _{i \in [1..k]} \mathbf {x}[i]) \cdot \sum _{i=1}^{k} \delta _i)\) and an upper bound on \(( \sum _{i=k+1}^{k+l} \mathbf {x}[i]\delta _i)\) is \( ((\min _{i \in [1..k]} \mathbf {x}[i]+1) \cdot \sum _{i=k+1}^{k+l} \delta _i)\). Hence we have:

    $$\begin{aligned} \varDelta (\mathbf {x})\!-\!\varDelta (\mathbf {x}') \!&\le \! \left( \!-\!\sum _{i=1}^{k+l}\delta _i^2\right) \!+\! \left( \min _{i \in [1..k]} \mathbf {x}[i]\right) \cdot \left( -2 \sum _{i=1}^{k} \delta _i\!+\! 2 \sum _{i=k+1}^{k+l} \delta _i\right) \!+\!\!\,2 \sum _{i=k+1}^{k+l} \delta _i \\&\le \left( - \sum _{i=1}^{k+l} \delta _i^2\right) +2 \sum _{i=k+1}^{k+l} \delta _i \\&\le \left( - \sum _{i=1}^{k} \delta _i^2\right) + \sum _{i=k+1}^{k+l} \delta _i (2- \delta _i). \end{aligned}$$

    Since \(\forall i \in [1..k+l]: \delta _i \ge 1\) we have \(\sum _{i=1}^{k} \delta _i^2 \ge \sum _{i=1}^{k} \delta _i\) and \(\sum _{i=k+1}^{k+l} \delta _i (2 - \delta _i) \le \sum _{i=k+1}^{k+l} \delta _i\). Thus

    $$\begin{aligned} - \sum _{i=1}^{k} \delta _i^2+ \sum _{i=k+1}^{k+l} \delta _i (2 - \delta _i) \le - \sum _{i=1}^k \delta _i + \sum _{i=k+1}^{k+l} \delta _i= 0. \ \ \end{aligned}$$

\(\square \)

Definition 6

A \(\mathbb {Z} \)centered assignment \(\mathbf {x}\) is such that \(\forall i: \mathbf {x}[i] \in I_D^{\mathbb {Z} }(X_i)\) and \( \not \exists \ i,j\) such that \(\mathbf {x}[i]<X_i^{\max }\), \(\mathbf {x}[j]>X_j^{\min } \) and \(\mathbf {x}[i]+1<\mathbf {x}[j]\).

Algorithm 3 computes a \(\mathbb {Z} \)centered assignment of sum \(s\).

figure c

This algorithm can be easily implemented but is very inefficient. A smarter method is possible. The idea is that it is always possible to distribute the \(m\) entries assigned to \(\nu \) on the integer values \(\lfloor \nu \rfloor \) and \(\lceil \nu \rceil \) while conserving a sum of \(s\). Clearly, such an assignment is \(\mathbb {Z} \)centered since the values assigned to \(\nu \) differ by at most one after the transformation.

The distribution of the \(m\) entries on \(\lfloor \nu \rfloor \) and \(\lceil \nu \rceil \) while conserving the sum of \(s\) is given as follows. The value \(\nu \) always takes the form of \((s-\mathrm{es})/m\) and there are exactly \(m\) entries assigned to \(\nu \). Hence, it is always possible to distribute these \(m\) entries on \(\lfloor \nu \rfloor \) and \(\lceil \nu \rceil \) without modifying the sum. The only question to answer is how many of them must be assigned to \(\lfloor \nu \rfloor \) and to \(\lceil \nu \rceil \)? Let us denote by \(\nu ^+\) the number of entries that must be assigned to \(\lceil \nu \rceil \) and by \(\nu ^-\) the number of entries that must be assigned to \(\lfloor \nu \rfloor \). Of course \(\nu ^+ + \nu ^-=m\). Looking at the Fig. 1, the sum is preserved if

$$\begin{aligned} \nu ^- \cdot ((s-\mathrm{es})\!\!\mod m)= (m-\nu ^-) \cdot (m-(s-\mathrm{es})\!\!\mod m). \end{aligned}$$

Hence, the distribution on \(\lfloor \nu \rfloor \) and \(\lceil \nu \rceil \) is:

$$\begin{aligned} \nu ^-&= m-(s-\mathrm{es}) \mod m\\ \nu ^+&= (s-\mathrm{es}) \mod m. \end{aligned}$$
Fig. 1
figure 1

Distances between \(\nu =(s-\mathrm{es})/m\), \(\lfloor \nu \rfloor \) and \(\lceil \nu \rceil \)

Algorithm 1 can be modified into Algorithm 4 to achieve a bound-consistent filtering of \(\varDelta \) with the computation of \(\underline{\varDelta }^\mathbb {Z} \).

figure d

Example 5

The variables are the same as in Example 4 and the sum is \(s=10\). From Example 4, \(s \in \mathrm{si}(I_3)\) and \(\nu =(s-\mathrm{es})/m=(10-3)/2=3.5\). The distribution of overlapping variables between \(\lfloor \nu \rfloor \) and \( \lceil \nu \rceil \) is given by \(\nu ^+=(s-\mathrm{es}(I)) \mod m(I)=(10-3) \mod 3=1\) and \(\nu ^-=1\). Consequently \(\underline{\varDelta }^\mathbb {Z} =\mathrm{es}_{(2)}(I_3)+4^2+3^2 = 34\).

Filtering of \(\mathbf X \)

Only the propagation of the upper bound of \(X_i\) is considered here since the propagation of its lower bound is a symmetrical problem. The propagation of \(X_i\) is achieved in Schaus et al. (2006) by computing the largest consistent value assuming the domains of other variables are rational intervals:

$$\begin{aligned} \overline{X}_{i}^\mathbb {Q}&= \max _{\mathbf {x}}\left\{ \mathbf {x}[i] \ \mathrm {s.t.} \ \sum _{i}\mathbf {x}[i]^2 \le \varDelta ^{\max } \ \mathrm {and}\sum _{i}\mathbf {x}[i]=s \ \mathrm {and}\ \forall j: \mathbf {x}[j] \in I_D^{\mathbb {Q} }(X_j)\right\} . \nonumber \\ \end{aligned}$$
(5)

Algorithm to compute \(\overline{X}_{i}^\mathbb {Q} \)

The idea of the algorithm from Pesant and Régin (2005) is to start from the a \(\nu \)-\(\mathbb {Q} \)centered assignment \(\mathbf {x}\) of sum \(s\) found in Algorithm 1. This assignment is the one of sum \(s\) with minimal sum of squares \(\underline{\varDelta }^\mathbb {Q} \). If \(\mathbf {x}[i]=X_i^{\max }\), the upper bound of the variable \(X_i\) is consistent. Otherwise the optimal value \(\overline{X}_{i}^\mathbb {Q} \) is obtained by successively assigning \(X_i\) to increasing values until either the minimal sum of squares reaches \(\varDelta ^{\max }\) or it is proved that \(X_i^{\max }\) is consistent. At that point, \(\overline{X}_{i}^\mathbb {Q} \) is equal to the current value considered for \(X_i\). This procedure is valid since the increasing values assigned to \(X_i\) range from \(\mathbf {x}[i]\) to \(X_i^{\max }\) and the minimal sum of squares \(\underline{\varDelta }^\mathbb {Q} \) increases quadratically when the value assigned to \(X_i\) increases.

A detailed description of Algorithm 6 follows. Let us denote by \(x_i\) the current value assigned to \(X_i\) and by \(d=x_i-\mathbf {x}[i]\) the distance of the current value of \(X_i\) to the \(i\)th entry in the starting \(\nu \)-\(\mathbb {Q} \)centered assignment \(\mathbf {x}\) of sum \(s\). Let us furthermore denote by \({\underline{\varDelta }^\mathbb {Q} }' ,\mathrm{si}'\), \(\mathrm{es}',m',\nu '\) the modified values if \(X_i\) were assigned to \(x_i=\mathbf {x}[i]+d\).

The interval \(I\) from \({\mathcal I}(\mathbf X )\) is such that \(s \in \mathrm{si}(I)\). Let us first assume \(X_i\in R(I)\), which implies \(\mathbf {x}[i]=X_i^{\min }\) (by definition of a \(\nu \)-\(\mathbb {Q} \)centered assignment). Recall that if \(X_i \in M(I)\) then \(\mathbf {x}[i]=\nu =(s-\mathrm{es}(I))/m\) and if \(X_i \in L(I)\) then \(\mathbf {x}[i]=X_i^{\max }\) and no filtering of the upper bound is possible.

The following lemma describes the quadratic evolution of \(\underline{\varDelta }^\mathbb {Q} \) with \(d\), i.e., when \(x_i\) increases. An illustration of Lemma 2 is given in Fig. 2.

Lemma 2

if \(d\le s-\min (\mathrm{si}(I)) \) then

  1. 1.

    \(\mathrm{es}'(I)=\mathrm{es}(I)+d\),

  2. 2.

    \(\mathrm{es}_{(2)}'(I)=\mathrm{es}_{(2)}+d^2+2d\cdot X_i^{\min }\),

  3. 3.

    \(m'=m\),

  4. 4.

    \(\nu '=\nu -d/m\) and

  5. 5.

    \({\underline{\varDelta }^\mathbb {Q} }' = \mathrm{es}_{(2)}'(I) +m \cdot (\nu ')^2\) \({\underline{\varDelta }^\mathbb {Q} }' = \mathrm{es}_{(2)}(I)+d^2+2d X_i^{\min }+m \cdot (\nu -\frac{d}{m})^2\) \({\underline{\varDelta }^\mathbb {Q} }' = \underline{\varDelta }^\mathbb {Q} + (d^2+2d X_i^{\min }+d^2/m - 2 d \nu ).\)

Fig. 2
figure 2

Consequences of the increasing of \(x_i\) by \(d\) : \({\underline{\varDelta }^\mathbb {Q} }'\) increases quadratically and \(\nu '\) decreases by \(d/m\)

From Lemma 2, the minimal sum of squares increases quadratically with \(d\). The maximum consistent value for \(X_i\) is the non-negative solution of the following second degree equation:

$$\begin{aligned} \underline{\varDelta }^\mathbb {Q} + d^2+2d X_i^{\min }+d^2/m - 2 d \nu =\varDelta ^{\max }. \end{aligned}$$

The non-negative solution of this equation is \(d^*=\frac{-b+\sqrt{b^2-ac}}{a}\) where \(a= 1+1/m\), \(b=X_i^{\min }-\nu \) and \(c=\underline{\varDelta }^\mathbb {Q} -\varDelta ^{\max }\). This reasoning is valid as long as \(d\le s-\min (\mathrm{si}(I)) \). Otherwise, \(\nu '\) moves outside the interval \(I\) and the modified values in Lemma 2 are no longer valid. If \(d^* \le s-\min (\mathrm{si}(I)) \) then \(\overline{X}_{i}^\mathbb {Q} =\mathbf {x}[i]+d^*\). Otherwise, \(\mathbf {x}[i]+s-\min (\mathrm{si}(I))\) is a consistent value for \(X_i\) and larger values for \(x_i\) must be considered. The procedure is repeated with the interval \(\mathrm {prev}(I)\) instead of \(I\). Indeed when \(X_i \leftarrow x_i=\mathbf {x}[i]+s-\min (\mathrm{si}(I))\), the value \(\nu '\) is equal to \(\min (I)=\max (\mathrm {prev}(I))\) and \(s=\min (\mathrm{si}'(I))=\max (\mathrm{si}'(\mathrm {prev}(I)))\).

The process is repeated until one valid solution of the second degree equation \(d^* \le s-\min (\mathrm{si}(I)) \) is found or the current interval considered is equal to \(I_1\).

Until now, we assumed that \(X_i \in R(I)\). The case \(X_i \in M(I)\) can also lead to the filtering of \(X_i^{\max }\). This case can be reduced to the same procedure as for the case \(X_i \in R(I)\):

  • The \(x_i\) starts to increase from \(\nu \) rather than from \(X_i^{\min }\) as previously. Hence, the interval \(I\) is conceptually split in two at the value \(\nu \): \(I'=[\min (I),\nu ]\) and \([\nu ,\max (I)]\).

  • The value \(\overline{X}_{i}^\mathbb {Q} \) is computed on this modified configuration by considering that the domain of \(X_i\) is now \([\nu ,X_i^{\max }]\).

  • The same procedure as described previously can be applied since with this modified domain we have that \(X_i \in R(I')\).

We say that the interval \(I\) is conceptually split because actually we only need to adapt the computation of \(m'(I)\), \(\mathrm{es}'(I)\) and \(\mathrm{es}_{(2)}'\) from Lemma 2. For \(x_i=X_i^{\min }+d\) and an interval \(I \in {\mathcal I}(\mathbf X )\) such that \(x_i>\min (I)\), if \(X_i \in M(I)\) then \(m'(I')=m(I)-1\) where \(I'=[\min (I),\min \{\max (I),x_i\}]\). The reason is that \(I_D^{\mathbb {Q} }(X_i)\) subsumes \(I'\) hence if \(X_i \leftarrow x_i\), then \(X_i\) belongs to \(R(I')\) and not to \(M(I')\) anymore. The unified procedure to adapt the values \(m'\), \(\mathrm{es}'\) and \(\mathrm{es}_{(2)}'\) for an interval \(I\) when \(X_i \leftarrow x_i\) with \(x_i \ge \min (I)\) is given in the Procedure get updated values.

figure e

The algorithm to compute \(X_i^{\max }\) is given in Algorithm 6. In lines 1–5, the current value for \(x_i\) is initialized to \(X_i^{\min }\) if \(X_i \in R(I)\) and to \(\nu \) if \(X_i \in M(I)\). In the main loop in lines 6–23, the algorithm tries to discover whether there is a value for \(x_i\) such that the value of the sum of squares reaches the maximum value \(\varDelta ^{\max }\) while keeping the value \(\nu \) in the current interval \(I\). If this is not possible (line 14) and the upper bound \(X_i^{\max }\) is not yet proved to be consistent (line 21), the procedure is repeated on the previous interval \(\mathrm {prev}(I)\) with \(x_i\) increased (line 20) such that the current value \(\nu \) is equal to \(\max (I)\) in the next iteration of the loop.

figure f

The complexity of Algorithm 6 is linear in the number of intervals in \({\mathcal I}\), which is smaller than \(2n-1\). It is applied for each variable \(X_i\) to make the filtering \(X_i^{\max } \leftarrow \min (X_i^{\max },\lfloor \overline{X}_{i}^\mathbb {Q} \rfloor )\) and a similar procedure is used to filter the lower bounds \(X_i^{\min }\). Hence the overall complexity of filtering \(\mathbf X \) is \({\mathcal O}(n^2)\). This complexity can actually be improved to \({\mathcal O}(n \cdot \log (n))\) by doing a binary search on the intervals \(I\) rather than a linear search. The binary search is valid since the sum of squares increases piecewise quadratically and continuously with the current value of \(x_i\).

Once again, the filtering with the value \(\overline{X}_{i}^\mathbb {Q} \) is not \(\mathbb {Z} \)-bound-consistent because it corresponds to an assignment with some variables assigned to \(\nu \), which might be not integer. Line 16 of Algorithm 6 computes the \(\mathbb {Z} \)-bound-consistent upper bound for \(X_i\) given \(\overline{X}_{i}^\mathbb {Q} \) and the current interval \(I\). We explain in the following get \(\overline{X}_{i}^\mathbb {Z} \)(\(x_i\), \(I\)) detailed in Algorithm 7 running in \({\mathcal O}(m)\). The main steps of the procedure are illustrated in Fig. 3. Line 1, rounds down the value \(\overline{X}_{i}^\mathbb {Q} \). This corresponds to the arrow A in Fig. 3. Lines 2–6 compute the sum of squares corresponding to a \(\mathbb {Z} \)centered assignment of sum \(s\) with \(X_i\) assigned to \(\overline{X}_{i}^\mathbb {Z} \). We can use the getUpdatedValues procedure to do this in constant time. Since the \(\mathbb {Q} \)centered assignment of sum \(s\) has been transformed into a \(\mathbb {Z} \)centered assignment of sum \(s\), the sum of squares might have increased (\(\underline{\varDelta }^\mathbb {Z} >\underline{\varDelta }^\mathbb {Q} = \varDelta ^{\max })\). In this case, the current value \(\overline{X}_{i}^\mathbb {Z} \) at line 1 is not consistent for \(X_i\) and there is some opportunity to decrease it even more by steps of 1 until a consistent value for \(X_i^{\max }\) is found (lines 7–9).

Fig. 3
figure 3

Illustration of the main steps of the Algorithm 7

figure g

Note that no more than \(m\) iterations are needed because as shown in Fig. 3 with arrows B, each time \(\overline{X}_{i}^\mathbb {Z} \) is decreased by 1, one variable assigned to \(\lfloor \nu \rfloor \) must be increased in the \(\mathbb {Z} \)centered assignment (to maintain the correct sum \(s\)). We are guaranteed that within at most \(m\) steps the \(\mathbb {Z} \)centered assignment will correspond to a \(\mathbb {Q} \)centered assignment which was proved to exhibit a sum of squares \(\le \!\varDelta ^{\max }\) in Algorithm 6. It remains to explain how to update in constant time the value \(\underline{\varDelta }^\mathbb {Z} \) in line 8 of Algorithm 7, each time \(\overline{X}_{i}^\mathbb {Z} \) is decreased by one. We refer to arrows B of Fig. 3 for the visual explanation and to the next transformations to obtain the formula.

$$\begin{aligned} {\underline{\varDelta }^\mathbb {Z} }^{'}&\leftarrow \mathrm{es}_{(2)}'+1-2\overline{X}_{i}^\mathbb {Z} +(\nu ^+ + 1) \cdot \lceil \nu \rceil ^2 +(\nu ^- -1) \cdot \lfloor \nu \rfloor ^2 \\&= \underline{\varDelta }^\mathbb {Z} + 1-2\overline{X}_{i}^\mathbb {Z} + \lceil \nu \rceil ^2 - \lfloor \nu \rfloor ^2 \\&= \underline{\varDelta }^\mathbb {Z} + 1-2\overline{X}_{i}^\mathbb {Z} + (\lceil \nu \rceil - \lfloor \nu \rfloor )\cdot (\lceil \nu \rceil + \lfloor \nu \rfloor ) \\&= \underline{\varDelta }^\mathbb {Z} + 1-2\overline{X}_{i}^\mathbb {Z} + 1 \cdot (2 \lceil \nu \rceil -1 ) \\&= \underline{\varDelta }^\mathbb {Z} + 2 \cdot ( \lceil \nu \rceil - \overline{X}_{i}^\mathbb {Z} ) \end{aligned}$$

The term \(1-2\overline{X}_{i}^\mathbb {Z} \) is the resulting modification to \(\mathrm{es}_{(2)}'\) caused by the decreasing by one of \(\overline{X}_{i}^\mathbb {Z} \), and the \(+ 1\) and \(-1\) applied, respectively, on \(\nu ^+\) and \(\nu ^-\) are because one variable assigned to \(\lfloor \nu \rfloor \) is increased by one. The other lines are simple algebraic manipulations to simplify the formula.

Nurse-to-patient assignment problems

As an application for spread  we consider the daily assignment of newborn infant patients to nurses in a hospital described in Mullinax and Lawley (2002) and solved with CP in Schaus et al. (2009). In this problem, some infants require little attention, while others need significant care. The amount of work required by the infant during one shift is called the acuity. A nurse is in charge of a group of infants and the total amount of acuity is the workload of the nurse during that shift. For ensuring an optimal care quality and perceived fairness for the nurses, it is essential to balance the workload. In addition, the problem features various side constraints:

  • A nurse can work in only one zone, but the patients are located in \(p\) different zones.

  • A nurse cannot be responsible for more than \(\hbox {children}^{\max }\) infants.

  • The total amount of acuity of a nurse cannot exceed \(\hbox {acuity}^{\max }\).

The balance objective and the various constraints make it very difficult to find a good solution in a reasonable time. Since nurses only work in one zone, the number of nurses assigned to each zone has already a huge impact on the quality of the balancing. In Mullinax and Lawley (2002), the problem was tackled using a MIP model recalled in “The MIP model”, but the results were not satisfactory. In this paper, we reuse our best two-step CP approach introduced in Schaus et al. (2009) to reach the required solution quality and scalability. This approach is recalled in “A two-step CP model”. We improve this approach in two ways:

  1. 1.

    We recognize that the first step is a discrete resource allocation problem that can be solved optimally with well-known algorithms.

  2. 2.

    We show that the final result can, for some instances, be proved optimal using a check procedure based on the second best solution of the resource allocation problem.

The MIP model

We now review the main variables of the MIP model from Mullinax and Lawley (2002). We also describe the limitations of the MIP model and suggest why a CP approach may address them. Due to space reasons, we do not reproduce the entire MIP model, but readers can consult Mullinax and Lawley (2002) for more details. The technical details presented here are sufficient for our purposes. The MIP model contains four families of variables:

  1. 1.

    \(X_{ij}=1\) if infant \(i\) is assigned to nurse \(j\) and \(0\) otherwise;

  2. 2.

    \(Z_{jk}=1\) if nurse \(j\) is assigned zone \(k\) and \(0\) otherwise;

  3. 3.

    \(Y_{k,\max }\) is the maximum acuity of a nurse in zone \(k\);

  4. 4.

    \(Y_{k,\min }\) is the minimum acuity of a nurse in zone \(k\).

All these variables are linked with linear constraints to enforce the constraints of the problem. The objective function implements what we call the range–sum criterion and consists of minimizing the sum of the acuity ranges of the \(p\) zones, i.e.,

$$\begin{aligned} \sum _{k=1}^p ( Y_{k,\max } - Y_{k,\min }). \end{aligned}$$

The MIP model has a fundamental limitation: The objective function may produce poorly balanced workloads. It tends to equalize the workload inside the zones, but may produce huge differences among the workload of different zones. This is illustrated in Fig. 4. The left solution is obtained by minimizing the range–sum criterion and the right solution by minimizing the variance (\(L_2\) norm in the next section). The range–sum objective is minimal on the left, because the workloads inside each of the two zones are identical. Unfortunately, nurses in the first zone work twice as much as those in the second zone. The right solution is obtained by minimizing the variance and is significantly more appealing.This illustrates clearly that “the high level objective that all nurses should be assigned an equal amount of patient acuity”Mullinax and Lawley (2002) is not properly captured with the range–sum criterion.

Fig. 4
figure 4

Comparison of two solutions on a 6 nurses, 14 infants, and 2 zones problem. Solution on the left is obtained by minimizing the range–sum criterion. Solution on the right is obtained by minimizing the variance

It is not immediately obvious how to remedy these problems. The variance is non-linear and is not easily modeled in a MIP approach. In addition, a CP approach may exploit the combinatorial structure in the bin-packing and the side-constraints, while the MIP relaxation is generally bad for bin-packing like problems. Finally, there are important symmetries that are not removed in their model: For a given solution, the nurses are completely interchangeable.

A two-step CP model

This approach introduced in Schaus et al. (2009) first pre-computes the number of nurses assigned to each zone and then assigns the patients to nurses. This simplifies the resolution by

  1. 1.

    Removing one degree of flexibility, which is the number of nurses in each zone.

  2. 2.

    Removing the necessity of expressing disjointedness constraint between the patients assigned to different nurses, since the set of nurses that can be assigned to each patient can be pre-computed.

Furthermore, pre-assigning the number of nurses in each zone decomposes the problem. Given the pre-computed number of nurses in each zone, it is equivalent to minimize \(L_{2}\) among all the nurses at once or to minimize \(L_{2}\) separately inside each zone (see Eq. 1). Therefore, if a given number of nurses is assigned to regions, the total acuity for that region is fixed and the best we can do is minimizing the \(L_2\) criterion inside this zone independently of other zones.

Step I: Finding the number of nurses in each zone

The problem of discovering the right number of nurses assigned to each nurses is crucial, because the decomposition may be significantly suboptimal if these numbers are not properly chosen. Indeed, the number of nurses assigned to each zone has a crucial impact on the quality of the balancing. However, after visualizing some optimal solutions, we observed that the workloads of the nurses are extremely well balanced (almost the same) inside the zones. This suggested solving a relaxation of the problem to discover a good distribution of the nurses to the zones. The relaxation allows the acuity of a child in a zone to be distributed among the nurses of that zone (continuous relaxation of the acuity). Since the acuity of a child can be split, the relaxed problem will have an optimal solution where the nurses of a zone have exactly the same workload \(\frac{A_k}{x_k}\), i.e., the total acuity \(A_k =\sum _{i \in \mathcal {P}_k} a_i\) of zone \(k\) divided by the number of nurses \(x_k\) in zone \(k\). This is schematically illustrated on Fig. 5 for a two-zone relaxation problem and stated in Proposition 1.

Fig. 5
figure 5

Illustration of a solution of the relaxation solved to find the number of nurses in each zone

Proposition 1

An optimal solution of the relaxed problem must have the same workload for all the nurses in a given zone.

Proof

If this is not the case, the sum of squares inside that zone can be reduced by making closer the workload given to two nurses (same argument as the one used in proof of Theorem 1)

The mathematical formulation of the relaxed problem is

$$\begin{aligned} \min&\sum _{k=1}^p x_k \cdot \left( \frac{A_k}{x_k}\right) ^2 \end{aligned}$$
(6)
$$\begin{aligned} \hbox {s.t.}&\sum _{k=1}^p x_k = m \end{aligned}$$
(7)
$$\begin{aligned}&x_k \in \mathbb {Z} ^+_0 \end{aligned}$$
(8)

with \(m\) integer and \(m\ge p\). The workload of all the nurses of zone \(k\) is \(\frac{A_k}{x_k}\).

Solving the Relaxation This problem is a discrete resource allocation problem with separable objective convex function (Katoh and Mine 1979). It can be solved optimally with the greedy INCREMENT algorithm described in Ibaraki and Katoh (1988). The algorithm starts with one nurse for each zone, and consecutively increments the number of nurses from the zone that will increase the least its \(L_2\). When the number of nurses \(m\) is reached, the allocation is optimal.Footnote 3 Algorithm 8 is an instance of INCREMENT method applied to our problem running in \({\mathcal O}(p+m \cdot \log p)\):

figure h

The complexity is obtained using a heap data structure to select in \({\mathcal O}(\log p)\) the \(k = \hbox {argmax}_i \{ \frac{A_i^2}{x_i}-\frac{A_i^2}{x_i+1} \}\) at each iteration and it takes a linear time \({\mathcal O}(p)\) to initialize it.

Step II: CP model

Listing 1 gives the OscaR CP OscaR Team (2012) model for the second step. In particular, it considers the allocation of patients of zone \(i\) given the number of nurses (nbNursesInZone(i)) assigned to this zone in Step I. This model minimizes the sum of squared acuities (sum2 variable). The constraint binpacking is the one introduced in Shaw (2004) making the link between the acuity of each patient and the total acuity of each nurse. The gcc constraint is the global cardinality constraint introduced in Régin (1996) forcing each nurse to have between 1 and 3 patients. The search is a non-deterministic search (Hentenryck and Michel 2006) that breaks the symmetries dynamically, trying at each node to assign the chosen patient to the nurses who already have some patients plus at most one nurse without any patient yet.

figure i

Proving optimality of our solutions

We define the candidate optimal solution to be the solution obtained after the optimization in each zone independently, based on the nurse allocation obtained from the continuous relaxation. This candidate optimal solution obtained with our two-step approach is not optimal if another allocation of the number of nurses to the different zones would have led to a better sum of squares objective at the end of the second step. There are a finite number of ways of allocating the nurses to the different zones. Optimality could be proven by solving the second step for every possible allocation. We can avoid that effort if the objective of the second best continuous relaxation of the first step is actually larger than the sum of squares of our candidate optimal solution. The question to answer now is how to compute the second best continuous relaxation. This second best solution is obtained by taking the second best decision when selecting \(k\) during the last iteration of Algorithm 8. More precisely when \(\sum _{i} x_i = m-1\), the second best should be selected instead of the best one with \(\hbox {argmax}_i\). The correctness of this procedure is guaranteed by the separable convex nature of the function to minimize. Alternatively to find the second best solution, one can start from the optimal solution with \(\sum _{i} x_i = m\) then update this solution by applying the change \(x_i \leftarrow x_i-1\), \(x_j \leftarrow x_j+1\) that minimizes the increase: \(\hbox {argmin}_{i\ne j | x_j > 1} \{ ( \frac{A_i^2}{x_i+1} - \frac{A_i^2}{x_i} ) + (\frac{A_i^2}{x_j-1}-\frac{A_j^2}{x_j}) \}\).

Experimental results

Problem Instances Reference Mullinax and Lawley (2002) specifies a statistical model to generate instances very similar to their real instances. This statistical model was also used to measure the robustness of their solution technique with respect to the number of nurses, the number of infants, and the number of zones. The model contains a single parameter: the number of zones. The maximum acuity per nurse is fixed to \(\hbox {acuity}^{\max }=105\), and the maximum number of infants per nurse is fixed to \(\hbox {children}^{\max }=3\). The instance generator fixes the number of nurses, the number of infants, the acuity, and the zone of each infant. The different steps to generate an instance are as follows:

  • The number of patients in a zone is specified by a Poisson random variable with mean 3.8 and offset by 10.

  • The acuity \(Y\) of a patient is obtained by first generating a number \(X \sim \hbox {Binomial}(n=8,p=0.23)\) and then choosing the number \(Y \sim \hbox {Unif}(10 \cdot (X+1),10 \cdot (X+1)+9)\).

  • The total number of nurses is obtained by solving a First Fit Decreasing (FFD) procedure in each zone. More precisely, the total number is the number of nurses found in each zone by the FFD procedure. The FFD procedure starts by ranking the patients in decreasing acuity. Then, the patient with the highest acuity is assigned to the first nurse. The next patients are assigned successively to the first nurse that can accommodate them without violating the maximum acuity and the number of patient constraints.

We generated 10 instances of 3 zones problem, and three larger instances with 6, 15 and 20 zones. The results are given in Table 2. The columns, respectively, represent the number of nurses, the number of patients, the total number backtracks, the overall time, the average workload per nurse, the lower bound on the standard deviation coming from the continuous relaxation (Step I), the standard deviation of the best solution, and the lower bound computed from the second best continuous relaxation. It can be seen that the computation times are very short (at most 1 s) and except for one 3 zones instance and the 20 zones instance (in bold), all the results are proven optimal since the lower bound of the second best continuous relaxation is always larger than the standard deviation of the optimal solution. Note that the MIP formulation from Mullinax and Lawley (2002) could hardly solve problems with 2 zones in less than 30 min.

Table 2 Results on 3, 6, 15 and 20 zones problem instances

The last three lines of Table 2 are obtained with instances from a 6,15 and 20 zones. The solution for the 15 zones instance is depicted in Fig. 6. This solution is also proven optimal since the second best lower bound is larger than the standard deviation computed for our best solution.

Fig. 6
figure 6

Solution of a 15 zones instance obtained with the two-step approach

Conclusion

We have introduced the bound-consistent filtering algorithms for the spread constraint on finite integer domains. We used spread to solve a real-life nurse-to-patient assignment problem using a two-step decomposition approach. We show that in practice this decomposition is optimal on typical instances. Our implementation of the spread constraint is open source and available on the repository of OscaR Team (2012). All the instances used in our experiments are available on http://becool.info.ucl.ac.be/resources/nurse-patient-assignment.