REGULAR
ConstraintLet's see a new (strange) global constraint
REGULAR(X,T,s0,F)
X
variables...T
specifies the valid state transitions: (scur,v,snext)
s0
is the initial stateF
is the set of accepting statesIn detail, the constrain is satisfied iff:
REGULAR
ConstraintConsider this example:
s0
(initial state)F
(accepting states)This is an example of a valid sequence (6 variables)
REGULAR
ConstraintConsider this example:
s0
(initial state)F
(accepting states)This an invalid sequence (forbidden transition)
REGULAR
ConstraintConsider this example:
s0
(initial state)F
(accepting states)This an invalid sequence (the last state is not in F
)
REGULAR
ConstraintConsider this example:
s0
(initial state)F
(accepting states)As usual, the REGULAR
constraint is capable of filtering
X
variablesREGULAR
ConstraintThe constraint can sometimes be very useful:
Hover, the constraint is not always provided by solvers
The reason is that REGULAR
can be decomposed
Yi
for each Xi
...Yn
variable for the finale stateTABLE
constraintsTABLE
constraint regulates a single transitionREGULAR
ConstraintREGULAR
ConstraintOr-tools provides REGULAR
via the API:
slv.TransitionConstraint(X, T, s0, F)
Where:
X
is a list with the Xi
variablesT
is a "matrix" (list of lists) with the allowed transitionss0
is the index of the initial stateF
is a list with the accepting statesThe solver builds the decomposition automatically
Consider the following problem (by Tim Curtois):
We need to plan the working shifts for the employees of a company
eoh
is known (in days)Each type of working shift k
:
lk
(in minutes)Ik
Each worker i
:
j
(or takes a day off)Mi,k
shifts of type k
Di
and at least di
minutes overallAgain, each worker:
wi
week endsj \mod 7 = 5 \text{ or } 6
c_i
and at most C_i
consecutive shiftsg_i
consecutive days offV_i
(vacation)There are positive preferences h
over shifts:
i_h
k_h
on day j_h
, we pay a penalty w^p_h
There are negative preferences h
over shifts:
i_h
k_h
on day j_h
, we pay a penalty w^n_h
There are cover requirements h
for shifts and days:
y_{h}
be the number of shifts k_h
on day j_h
y_h
is r_h
, we pay w^u_h (r_h - y_h)
y_h
is r_h
, we pay w^o_h (y_h - r_h)
u_h
is much greater than the penalty o_h
About the number of entities:
n_e
be the number of workersn_s
be the number of shift typesn_p
be the number of positive preferencesn_n
be the number of negative preferencesn_c
be the number of cover requirementsThis is a rather complex problem
The main variables are:
\begin{align}
& x_{i,j} \in \{0..n_s\} & \forall i = 0..n_e-1, j = 0..eoh-1 \\
& w_{i,j} \in \{0, 1\} & \forall i = 0..n_e-1, j = 0..eoh-1
\end{align}
x_{i,j}
is the shift type for worker i
on day j
x_{i,j} = n_s
for a day offw_{i,j} = 1
if worker i
does not take a day off on j
Chaining constraints:
\begin{align}
& w_{i,j} = (x_{i,j} \neq n_s) & \forall i = 0..n_e-1, j = 0..eoh-1
\end{align}
Compatibility constraints between subsequent shifts:
\begin{align}
& {\rm\scriptsize TABLE([x_{i,j}, x_{i,j+1}], T)} & \forall i = 0..n_e-1, j = 0..eoh-2
\end{align}
T
contains all the valid transitions, i.e.
(k', k") \in T \text{ iff } k' = n_s \vee k" = n_s \vee k" \notin I_{k'}
Maximum number of shift types per worker:
\begin{align}
& {\rm\scriptsize GCC}(X_{i,:}, [0..n_s], [0..0], [M_{i,0}, M_{i,1}, ... \infty]) & \forall i = 0..n_e-1
\end{align}
Limits on the number of minutes per worker (l_{n_s} = 0
):
\begin{align}
& \sum_{j=0..eoh-1} l_{x_{i,j}} \leq D_i & \forall i = 0..n_e-1 \\
& \sum_{j=0..eoh-1} l_{x_{i,j}} \geq d_i & \forall i = 0..n_e-1 \\
\end{align}
Maximum number of weekends:
\begin{align}
& W^{we}_{i,h} \in \{0,1\} & \forall i = 0..n_e-1, h = 0..^{eoh}/_7
\end{align}
W^{we}_{i,h} = 1
if worker i
works on the h
-th weekend\begin{align}
& W^{we}_{i,h} = \max(W_{i,7(h+1)-2}, W_{i,7(h+1)-1}) & \forall i = 0..n_e-1, h = 0..^{eoh}/_7
\end{align}
\begin{align}
\sum_{h = 0..^{eoh}/_7} W^{we}_{i,h} \leq w_i
\end{align}
Mandatory days off:
\begin{align}
& x_{i,j} = n_s & \forall i = 0..n_e-1, j \in V_i
\end{align}
Constraint on consecutive days:
{\rm\scriptsize REGULAR}
on the W_{i,j}
variables for each worker i
Constraint on consecutive days:
{\rm\scriptsize REGULAR}
on the W_{i,j}
variables for each worker I
Constraint on consecutive days:
{\rm\scriptsize REGULAR}
on the W_{i,j}
variables for each worker I
The penalty for not satisfying positive preferences:
\begin{align}
& z_p = \sum_{h = 0..n_p-1} w^p_h (x_{i_h, j_h} \neq k_h)
\end{align}
The penalty for not satisfying negative preferences:
\begin{align}
& z_n = \sum_{h = 0..n_n-1} w^n_h (x_{i_h, j_h} = k_h)
\end{align}
The penalty for not satisfying cover preferences:
\begin{align}
& {\rm\scriptsize COUNT}(X_{:,j}, k_h, y_h) & \forall h = 0..n_c-1 \\
& z_o = \sum_{h = 0..n_c-1} w^o_h \max(y_h - r_h, 0) \\
& z_u = \sum_{h = 0..n_c-1} w^u_h \max(r_h - y_h, 0) \\
\end{align}
The overall cost function is:
\begin{align}
\min z = z_p + z_n + z_o + z_u
\end{align}
The model and two instances are available on the start-kit
Instance1
to optimalityInstance2
NOTE: both tasks are difficult!