Constrains to a shift start
回答済みHello!
I have this code to assign shift to a set of nurses:
from gurobipy import *
numday = 15
num_nurses = ["a","b","c","Telly","e"]
multis = { "a":[1,3],"b":[1,3],"c":[3,6],"Telly":[3,4],"e":[2,5] }
m = Model('nsp')
x = m.addVars(num_nurses,range(numday), vtype=GRB.BINARY,name='x')
# Don't allow nurses to start a 3-day shift that goes past day 15.
# We do this by setting the upper bound to zero on these variables.
for i in num_nurses:
for j in range(numday-multis[i][0]+1, numday):
x[i,j].ub=0
#every day must be assigned exclusively to one nurse
# for every day we count the 3-day shifts that started on that day, but also the
# day before, and the day before (when multis=3)
m.addConstrs(
quicksum(x.get((i,k),0) for i in num_nurses for k in range(j-multis[i][0]+1, j+1)) == 1
for j in range(numday))
# minimum days off
m.addConstrs(
quicksum(x[i,k] for k in range(j-multis[i][0]-multis[i][1]+1, j+1)) <= 1
for i in num_nurses
for j in range(multis[i][0]+multis[i][1]-1, numday))
m.optimize()
starts = [k for k,v in x.items() if v.X > 0.5]
roster = {(nurse,day):"." for nurse in num_nurses for day in range(numday)}
for nurse, day in starts:
for j in range(day, day+multis[nurse][0]):
if (nurse,j) in roster:
roster[nurse,j] = "x"
for j in range(day+multis[nurse][0], day+multis[nurse][0]+multis[nurse][1]):
if (nurse,j) in roster:
roster[nurse,j] = "-"
for i in num_nurses:
print(f"{i:<6}", " ".join([roster[i,j] for j in range(numday)]))
in this example there are shift for one day and for more consecutive days. And also this model has waiting times between one shift and the next one
I need to add a constraint that if my shift is for more than 3 days, the shift must start only in selected days.
Let’s see an example:
|
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
|
Telly |
S |
X |
x |
- |
- |
- |
- |
. |
. |
S |
x |
x |
- |
- |
- |
- |
In this case Telly has a 3-day shift with a minimum 4 – day off.
I want to add a constraint that Telly shifts must start in the days 0,3,6,9,…
Another example:
|
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
|
|
b |
. |
. |
. |
. |
S |
x |
x |
x |
- |
- |
. |
. |
S |
x |
x |
x |
In this case b has a 4-day shift with a minimum 2 – day off.
I want to add a constraint that b shifts must start in the days 4,12,20,28,…
But I want the constraint to be flexible enough to be variable to different combinations of shift and days off, and between nurses.
Mainly, shifts starts are a multiple of 3 or 5. However I want to also be able to set the days for a nurse to start their shift. If this is possible in one or more constraints it is ok for me.
Thanks!
Bernardo
-
To add a constraint that ensures a nurse's shift starts only on specific days, which can be multiples of 3 or 5 or any other specified set of days, you can modify your Gurobi model. This constraint should be flexible enough to cater to different shift lengths and minimum days off, as well as varying for each nurse. Here's how you can implement this:
-
Define Start Days: First, define the allowed start days for each nurse. This can be a dictionary where each nurse is associated with a list of days they are allowed to start their shift.
-
Add Constraint: Add a new constraint to your model that ensures that if a nurse is scheduled to start a shift, it must be on one of their allowed start days.
Let's implement these steps in your existing code
from gurobipy import *
numday = 15
num_nurses = ["a", "b", "c", "Telly", "e"]
multis = { "a": [1, 3], "b": [1, 3], "c": [3, 6], "Telly": [3, 4], "e": [2, 5] }# Define the allowed start days for each nurse
allowed_start_days = {
"a": list(range(0, numday, 3)), # Starts on days 0, 3, 6, 9, 12...
"b": list(range(4, numday, 8)), # Starts on days 4, 12...
"c": list(range(0, numday, 5)), # Example for nurse c
"Telly": list(range(0, numday, 3)), # Starts on days 0, 3, 6, 9, 12...
"e": list(range(2, numday, 7)), # Example for nurse e
}m = Model('nsp')
x = m.addVars(num_nurses, range(numday), vtype=GRB.BINARY, name='x')# Existing constraints...
# Add constraint for shift starts based on allowed days
for nurse in num_nurses:
for day in range(numday):
if day not in allowed_start_days[nurse]:
x[nurse, day].ub = 0# Rest of your model...
m.optimize()
# Rest of your code...
In this code snippet,allowed_start_daysis a dictionary where each nurse has a list of days they are allowed to start their shifts. The constraint ensures that if a day is not in a nurse's allowed start days, the upper bound for the variablex[nurse, day]is set to 0, effectively disallowing a shift to start on that day.This approach provides the flexibility to set different allowed start days for each nurse and can accommodate various shift lengths and off days by adjusting the ranges in the
allowed_start_daysdictionary.0 -
サインインしてコメントを残してください。
コメント
1件のコメント