Scheduling - exactly n staff or none each shift?

57 views
Skip to first unread message

Danny Cheng

unread,
Sep 20, 2022, 5:15:21 AM9/20/22
to or-tools-discuss
I am trying to solve a shift scheduling problem, where each shift should have exactly n staff, or have none at all. Not all the shifts need to be filled. Is this possible? I am looking at the nurse scheduling example and that is a great starting point, but would appreciate any suggestions on what other examples I should study up on.

Mario Leung

unread,
Sep 20, 2022, 5:58:17 AM9/20/22
to or-tools-discuss
Hi, 

if you follow the nurse rostering sample (https://developers.google.com/optimization/scheduling/employee_scheduling), you can simply add variables to record the number of staff handling the specific shifts, and only allow the number to be equal to n or 0. Take a look at the sample pseudo-code below
...
for (int s : all_shifts) {
  for (int d : all_days) {
     IntVar num_nurse_allowed = cp_model.NewIntVar...
    std::vector<BoolVar> num_nurse_worked;  
    for (int n : all_nurses) {
      auto key = std::make_tuple(n, d, s);
       num_nurse_worked.push_back(shifts[key]);
    }
    cp_model.AddEquality( num_nurse_allowed, LinearExpr::Sum(num_nurse_worked));
    cp_model.Add( (num_nurse_allowed == 0) or (num_nurse_allowed == n));
  }
...

Danny Cheng

unread,
Sep 20, 2022, 6:02:14 AM9/20/22
to or-tools-discuss
thanks, that seems rather straight forward, will take a closer look at this tomorrow.

Laurent Perron

unread,
Sep 20, 2022, 6:46:20 AM9/20/22
to or-tools-discuss
It does not work. 

cp_model.Add( (num_nurse_allowed == 0) or (num_nurse_allowed == n));

is not interpreted correctly. what is 'or' ?

I would suggest 

  for (int d : all_days) {
    BoolVar nurses_working = cp_model.NewBoolVar...
    std::vector<BoolVar> num_nurse_worked;  
    for (int n : all_nurses) {
       auto key = std::make_tuple(n, d, s);
       num_nurse_worked.push_back(shifts[key]);
    }
    cp_model.AddEquality( nurses_working * n, LinearExpr::Sum(num_nurse_worked));
  }


--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/cd9265c9-fb1d-477d-9cd3-dd122a861fefn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages