Google Or-Tools Employee Scheduling .条件不正常

Google Or-Tools Employee Scheduling .The condition does not work properly

我正在使用 that nurse scheduling example。我有 3 个员工 2 个班次和 7 天,我有一个条件,如果一个员工在第 1 个班次工作 he/she 第二天不能在第 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 班工作

根据您的约束:

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()])

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