Google Or-Tools员工安排。条件无法正常运行

问题描述 投票:1回答:1

我正在使用that nurse scheduling example。我有3名员工2班和7天我有一个条件,如果一名员工在第1班工作他/她第二天不能在班次0工作。这是我的代码,它不起作用。

    for n in all_nurses:
      for d in all_days:
        model.Add(sum(shifts[(n, d, s)] for s in range(0,1))+sum(shifts[(n, (d+1)%6, s)] for s in range(1,2)) <= 1)

这是output。护士2在第0天工作,第1班和第二天也在班次1工作

python scheduling constraint-programming or-tools
1个回答
1
投票

根据你的约束:

for n in all_nurses:
    for d in all_days:
        model.Add(sum([shifts[(n, d, 1)], shifts[(n, (d+1)%7, 0)]]) <= 1)

一个更好的配方是

for n in all_nurses:
    for d in all_days:
        model.AddBoolOr([shifts[(n, d, 1)].Not(), shifts[(n, (d + 1) % 7, 0)].Not()])

ref:https://github.com/google/or-tools/blob/aa0c6c42a523ee4c23633585b86fb6d3e090f8c8/ortools/sat/samples/bool_or_sample_sat.py#L23-L28

© www.soinside.com 2019 - 2024. All rights reserved.