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
$\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)
| improve this answer | |
$\endgroup$
8
$\begingroup$

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ Good library-agnostic solution! $\endgroup$ – Rizky Luthfianto Jun 9 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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