I want to randomly generate a 8 hours shift for worker.
Start from 0 to 24*7+8(next monday's 8am). Each day has a start time and end time.
(0,0) if off.
the output should look like this:
[[ 0 0]
[ 29 37]
[ 60 68]
[ 80 88]
[ 0 0]
[121 129]
[165 173]]
Shift can be overnight: eg, 23--31 for 1st day.
There must be a 12 hours gap between shifts. from example above: if the 2nd day is not off, then 2nd day shift starts from 32.
if 3rd day off, end time of 2nd will be 48-1. so wiggle room for 2nd is [32,47]
This is the code I wrote:
import numpy as np
def shift8h():
shift = np.zeros((7, shift_start_end), dtype=int)
twodaysoff = np.sort(np.random.choice(range(7), off_days_8h, replace=False)) # random 2 dif number
#twodaysoff=[0,4] # twodaysoff=[1,6] # wodaysoff=[0,1] # twodaysoff=[1,2]
work_days = np.setdiff1d(days, twodaysoff) # 5 work days
for i in work_days:
for j in twodaysoff:
if j - i != 1: # if the day is NOT the previous day before the off-days
starttime1 = np.random.randint(0, 23) + i * 24
endtime = starttime1 + time_8h
shift[i] = (starttime1, endtime)
# else:
if j - i == 1 and j != 6: # if the days is the day before the off-days
starttime1 = np.random.randint(0, 16) + i * 24 #not start work withing 8 hours before midnight of off
endtime = starttime1 + time_8h
shift[i] = (starttime1, endtime)
# add
if starttime1 - shift[i - 1][1] < 12: # 12 hours break btw work days
# print('yes')
# shift[i-1][0]=np.random.randint(0,starttime1-12) +i*24
shift[i - 1][0] = np.random.randint(0, 10) + i * 24
# print(shift[i-1][0])
shift[i - 1][1] = shift[i - 1][0] + time_8h
# print(shift[i])
if j == 6:
shift[j] = [0, 0]
if j == 1:
shift[j - 1][0] = (np.random.randint(0, 17)) # 0..16 24-8
shift[j - 1][1] = shift[j - 1][0] + time_8h
return shift
def modify_shift8h():
mod_shift8h = shift8h()
#print("old:", a)
for i in range(1, len(mod_shift8h) - 1):
if abs(mod_shift8h[i][0] - mod_shift8h[i - 1][1]) < 12 and np.array_equal(mod_shift8h[i],
np.array([0, 0])) == False: # and mod_shift8h[i][0]-a[i-1][1]!=0:
mod_shift8h[i - 1][1] = mod_shift8h[i][0] - 12 - np.random.randint(0, 4)
mod_shift8h[i - 1][0] = mod_shift8h[i - 1][1] - time_8h
return mod_shift8h
modify_shift8h()
It does not work on twodaysoff=[0,4] and twodaysoff=[0,3]
Aucun commentaire:
Enregistrer un commentaire