1

I'm using AddExactlyOne from Python or-tools CP-SAT for the nurses scheduling problem.

(Using the from ortools.sat.python import cp_model code at the bottom of the link)

However, this code used AddExactlyOne to assign exact only one nurse in a shift. What I want to do is to assign exactly two nurses in a shift.

I tried:

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

instead of: model.AddExactlyOne(shifts[(n, d, s)] for n in all_nurses), which is already stated in the code.

However, no luck. Any idea how to assign exactly two nurses in a shift? Something like: AddExactlyTwo instead of AddExactlyOne?

Thanks in advance.

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
MG G
  • 3
  • 1

1 Answers1

0

The code

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

works correctly.

Unfortunately, the nurses scheduling problem is very constrained, and depends on the fact that #nurses == #shifts.

because of that assumption, any modification of the model will most likely create an infeasible model.

For a better model, please look at the shift scheduling sample.

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
  • Hello Laurent. Thanks for reply. Actually I tried num_nurses = 10 and num_shifts = 2 and model.AddExactlyOne(shifts[(n, d, s)] for n in all_nurses), and it gave feasible solution. However, with model.Add(sum(shifts[(n, d, s)] for n in all_nurses) == 2) it doesn't give any feasible solution. Actually, I tried to alter number of nurses and shifts but again it works only with model.Add(sum(shifts[(n, d, s)], Any advice, please? – MG G May 17 '23 at 09:20
  • As I said, the model is very brittle, and just meant to be used to showcase some API. For a real example, start from the shift scheduling code. – Laurent Perron May 17 '23 at 09:28