How to set up conditionals in Google OR-Tools?

Viewed 56

I’m trying to use OR-Tools to solve custom nurse scheduling problem - essentially each nurse works exactly two paired shifts, and gives a ranked list of the paired preferences.

For example, nurse1 has the following preferences:

  1. shift 1 and shift 7
  2. shift 4 and shift 8
  3. shift 2 and shift 6

If the nurse gets assigned to shift 1, he must also work shift 7. If the nurse gets assigned to shift 4, he must also work shift 8. If the nurse gets assigned to shift 2 he must work shift 6.

This holds true for all nurses in the hospital.

I can schedule individual shifts, but I don’t know how to create the requests matrix or link the shifts.

Any ideas?

Thanks.

2 Answers

The simple answer is just use the same Boolean variables for the 2 shifts.

Now, if you want to implement the following constraint

b1 <=> b2 && b3

I suggest reading the following recipe. Note that it can be expanded to more than 2 variables in the conjunction.

For each nurse, I ended up creating a new BoolVar for each choice, a BoolAnd for each shift pair, and a BoolOr for all three shift choices.

first_choice = model.NewBoolVar("first_choice")
second_choice = model.NewBoolVar("second_choice")
third_choice = model.NewBoolVar("third_choice")

model.AddBoolAnd([shifts[(nurse, first_a)], shifts[(nurse, first_b)]]).OnlyEnforceIf(first_choice)
model.AddBoolAnd([shifts[(nurse, second_a)], shifts[(nurse, secon_b_b)]]).OnlyEnforceIf(second_choice)
model.AddBoolAnd([shifts[(nurse, third_a)], shifts[(nurse, third_b)]]).OnlyEnforceIf(third_choice)

model.AddBoolOr([first_choice, second_choice, third_choice])

This seems to have done the job.

Related