3
$\begingroup$

Let's say nurses normally do 1 shift

for d in all_days:
    for s in all_shifts:
        model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 1)

But I want to make nurses either do 1 shift or 3 shifts (but no 2 shifts). How do I combine between

        model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 1)

and

        model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 3)

?

$\endgroup$
2

2 Answers 2

3
$\begingroup$

You can also do:

model.AddLinearExpressionInDomain(sum(variables), cp_model.Domain.FromValues([1, 3]))

Or even:

for d in all_days:
    for s in all_shifts:
        b = model.NewBoolVar("")
        model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 1).OnlyEnforceIf(b)
        model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 3).OnlyEnforceIf(b.Not())

PS:

Let's say nurses normally do 1 shift

Wouldn't that be:

for n in all_nurses:
    model.Add(sum(shifts[(n, d, s)] for d in all_days for s in all_shifts) == 1)
$\endgroup$
2
8
$\begingroup$

Introduce a binary variable $x_{d,s}$ and change the right hand side to $1+2x_{d,s}$.

$\endgroup$
1
  • $\begingroup$ Good library-agnostic solution! $\endgroup$ Jun 9, 2020 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.