0
votes

I am trying to use get familiar with google or-tools. I tried a simplified version of the Employee scheduling python example.

from __future__ import print_function
import sys
from ortools.constraint_solver import pywrapcp


def main():
    # Creates the solver.
    solver = pywrapcp.Solver("employee_scheduling")

    num_nurses = 3
    num_shifts = 3
    num_days = 1

    # [START]
    # Create shift variables.
    shifts = {}

    for j in range(num_nurses):
        for i in range(num_days):
            shifts[(j, i)] = solver.IntVar(
                0, num_shifts - 1, "shifts(%i,%i)" % (j, i))
    shifts_flat = [shifts[(j, i)] for j in range(num_nurses)
                for i in range(num_days)]

    # Create nurse variables.
    nurses = {}

    for j in range(num_shifts):
        for i in range(num_days):
            nurses[(j, i)] = solver.IntVar(
                0, num_nurses - 1, "shift%d day%d" % (j, i))
    # Set relationships between shifts and nurses.
    for day in range(num_days):
        nurses_for_day = [nurses[(j, day)] for j in range(num_shifts)]

        for j in range(num_nurses):
            s = shifts[(j, day)]
            solver.Add(s.IndexOf(nurses_for_day) == j)

    # Create the decision builder.
    db = solver.Phase(shifts_flat, solver.CHOOSE_FIRST_UNBOUND,
                    solver.ASSIGN_MIN_VALUE)
    # Create the solution collector.
    solution = solver.Assignment()
    solution.Add(shifts_flat)
    collector = solver.AllSolutionCollector(solution)

    solver.Solve(db, [collector])
    print("Solutions found:", collector.SolutionCount())
    print("Time:", solver.WallTime(), "ms")
    print()


if __name__ == "__main__":
    main()

As you can see the only constraints I have kept is the relationships between shifts and nurses. With num_nurses = 3, num_shifts = 3 and num_days = 1, the solver is able to find 6 solutions. However, if I change num_shifts to 2, the solver returns 0 solutions. Shouldn't this have 3 solutions as well (assign one nurse, leave the other two unassigned) ?

2

2 Answers

0
votes

As it turns out, this is a limitation with the way employee_scheduling is written right now. A rewrite of it is currently underway and should be done in a couple of weeks.

https://github.com/google/or-tools/issues/932

0
votes

I recommend this version of shift scheduling that implements a few different constraints:

https://github.com/google/or-tools/blob/master/examples/python/shift_scheduling_sat.py