1

I'm trying to add the constraint that the nurse will work at least two days in a row. Code: https://developers.google.com/optimization/scheduling/employee_scheduling#program2

My idea is: "If there is working shift and before was not then add working shift next day"

for n in all_nurses:
    for d in range(1,6): #Because use d-1 and d+1
        for s in all_shifts:
#1-st try            model.AddImplication(shifts[(n, d-1, s)]==0 and shifts[(n, d, s)]==1, shifts[(n, d+1, s)]==1)
#2-nd try:            model.Add(shifts[(n, d+1, s)]==1).OnlyEnforceIf(shifts[(n, d-1, s)]==0 and shifts[(n, d, s)]==1)

1-st: throw TypeError: NotSupported: model.GetOrMakeBooleanIndex(shift_n0d2s0 == 1) 2-nd: throw: AttributeError: 'BoundedLinearExpression' object has no attribute 'Index'

Przemek
  • 35
  • 5

1 Answers1

3

The correct version of your second try is:

model.Add(shifts[(n, d+1, s)]==1).OnlyEnforceIf([shifts[(n, d-1, s)].Not(), shifts[(n, d, s)]])

You can also do:

model.AddBoolOr([shifts[(n, d-1, s)], shifts[(n, d, s)].Not(), shifts[(n, d+1, s)]])

Think of it as forbidding Trues of len 1, see: https://stackoverflow.com/a/56877058/7810777

Also, you are not taking into account the borders, right now [1, 0, 0, ...] is allowed.

Stradivari
  • 2,626
  • 1
  • 9
  • 21
  • "Also, you are not taking into account the borders, right now [1, 0, 0, ...] is allowed." I used () so it should be forbidden. Could you clarify – Przemek Jul 20 '20 at 16:06
  • Sorry, I don't understand "I used ()" – Stradivari Jul 20 '20 at 16:40
  • I used parentheses, so `[(n, d+1, s)]` is a tuple and therefore [1, 0, 0, ...] is not allowed. But I think I could misunderstood sentence: "Also, you are not taking into account the borders, right now [1, 0, 0, ...] is allowed." – Przemek Jul 20 '20 at 21:38