每周轮班,两天休息

2024-10-03 15:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为工人随机生成一个8小时的班次。你知道吗

从0开始到24*7+8(下周一上午8点)。每一天都有开始时间和结束时间。你知道吗

(0,0)如果关闭。你知道吗

输出应如下所示:

[[  0   0]
 [ 29  37]
 [ 60  68]
 [ 80  88]
 [  0   0]
 [121 129]
 [165 173]]

夜班制:例如,第1天23-31班。你知道吗

轮班之间必须有12小时的间隔。根据上面的例子:如果第二天没有下班,那么第二天的班次从32开始。你知道吗

如果第三天休息,第二天的结束时间为48-1。所以第二组的摆动空间是[32,47]

这是我写的代码:

import numpy as np

LastShiftEndTime = 24*7 +8 # 176 next monday's morning 8am
hours_168 =168
day_rest=12
days = np.arange(7)
time_8h=8
off_days_8h=2
off_days_8h_parttime=5


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,3] # twodaysoff=[1,6] # wodaysoff=[0,1]
    # twodaysoff=[3,6]
    # print("twodaysoff:", twodaysoff)
    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()
    # mod_shift8h = test
    #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

在twodaysoff=[0,4]和twodaysoff=[0,3]上不起作用

----修订了本杰明的法典

import numpy as np
def sxx():
# Position days off
    work = np.array([1,1,1,1,1,0,0], np.bool)
    np.random.shuffle(work)

    # Pick start time for the first day
    start = np.zeros(7, np.int16)
    first_day = np.argmax(work)
    start[first_day] = np.random.randint(first_day*24, (first_day+1)*24)

    # Pick start times for the following days
    for i in range(1,7):
        if work[i] == True:
            while (start[i] - start[i-1]) < 12 + 8:
                start[i] = np.random.randint(i*24, (i+1)*24-1)
            if i ==6 :
                start[i] = np.random.randint(i*24,(i+1)*24)
                if work[i] and (start[i] - start[i - 1]) < 12 + 8:
                    # print(start[i-1])
                    start[i-1] = np.random.randint ((i-1)*24,(i-1)*24 +(24-8))
                    start[i] = np.random.randint(start[i-1]+20, (i+1)*24)

    # End times
    end = start + 8
    end[~work] = 0
    # return list(zip(start, end))
    return np.array(list(zip(start, end)))

这几乎是正确的,除了返回

[[ 21  29]
 [ 44  52]
 [  0   0]
 [ 87  95]
 [  0   0]
 [128 136]
 [157 165]]

这是不对的。你知道吗


Tags: themodifshifttimenprandomdays
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:12

这是非常复杂的代码。。。你知道吗

首先选择休息日,然后选择第一天的开始时间。接下来,生成第二天的开始时间,直到它们符合您的12小时标准。结束时间是8小时从开始时间。你知道吗

import numpy

# Position days off
work = numpy.array([1,1,1,1,1,0,0], numpy.bool)
numpy.random.shuffle(work)

# Pick start time for the first day
start = numpy.zeros(7, numpy.int16)
first_day = numpy.argmax(work)
start[first_day] = numpy.random.randint(first_day*24, (first_day+1)*24)

# Pick start times for the following days
for i in range(1,7):
    if work[i]:
        while (start[i] - start[i-1]) < 12 + 8:
            start[i] = numpy.random.randint(i*24, (i+1)*24+9)

# End times
end = start + 8
end[~work] = 0

print zip(start, end)
>>> [(0, 0), (45, 53), (0, 0), (74, 82), (118, 126), (141, 149), (168, 176)]

像这样的。。。尽管可能有一种更简单的方法,使用纯Python。把时间范围、限制条件等写在纸上,可以帮助你弄清楚这个答案对你自己是否正确。你知道吗

相关问题 更多 >