3.2 Workforce shift scheduling
Preamble: Install Pyomo and a solver
The following cell sets and verifies a global SOLVER for the notebook. If run on Google Colab, the cell installs Pyomo and the HiGHS solver, while, if run elsewhere, it assumes Pyomo and HiGHS have been previously installed. It then sets to use HiGHS as solver via the appsi module and a test is performed to verify that it is available. The solver interface is stored in a global object SOLVER for later use.
Problem Statement
An article entitled "Modeling and optimization of a weekly workforce with Python and Pyomo" by Christian Carballo Lozano posted on Towards Data Science showed how to build a Pyomo model to schedule weekly shifts for a small campus food store. The article was primarily intended as a tutorial introduction to Pyomo (see the github repository for the code).
From the original article:
A new food store has been opened at the University Campus which will be open 24 hours a day, 7 days a week. Each day, there are three eight-hour shifts. Morning shift is from 6:00 to 14:00, evening shift is from 14:00 to 22:00 and night shift is from 22:00 to 6:00 of the next day. During the night there is only one worker while during the day there are two, except on Sunday that there is only one for each shift. Each worker will not exceed a maximum of 40 hours per week and have to rest for 12 hours between two shifts. As for the weekly rest days, an employee who rests one Sunday will also prefer to do the same that Saturday. In principle, there are available ten employees, which is clearly over-sized. The less the workers are needed, the more the resources for other stores.
Here we revisit the example with a new model demonstrating how to use of Pyomo decorators and of Pyomo sets, and how to use the model solution to create useful visualizations and reports for workers and managers.
Model formulation
Model sets
Assuming that we have available workers, the problem requires the assignment of these available workers to a predetermined set of shifts with specific staffing requirements. Let describe the minimum number of workers required for the day-shift pair .
There are three shifts per day, seven days per week. These observations suggest the need for three ordered sets:
Wis a set with elements representing workers.Dis the set with the 7 days of the week.Sis the set with the three daily shifts.
It is convenient to add the following additional sets to improve the readability of the model:
Tis the set of the ordered (day, shift) pairs describing all the available shifts during the week.Bis the ordered set of all overlapping 24-hour periods in the week. An element of the set contains the (day, shift) period in the corresponding period. This set will be used to limit worker assignments to no more than one for each 24-hour period.Eis the set of all (day, shift) pairs on a weekend. This set will be used to implement worker preferences on weekend scheduling.
To recap, the sets that we will be using are defined as follows:
Model parameters
Model decision variables
Note, in particular, that we have only binary decision variables.
Model objective
The model objective is to minimize the overall number of workers needed to fill the shift and work requirements while also trying to meet worker preferences regarding weekend shift assignments. This is achieved here by minimizing a weighted sum of the number of workers needed to meet all shift requirements and the number of workers assigned to weekend shifts. The resulting objective function is
where the weight is a fixed positive parameter that determines the relative importance of these two measures in a desirable shift schedule.
Model constraints
Let us now formulate all the constraints using the variables we have created.
At each day-shift we need to have enough workers to meet staffing requirements:
No worker can be assigned more than 40 hours per week, which means that the total number of day-shifts times 8 hours per each cannot be greater than 40:
No worker can be assigned more than one shift in each 24-hour period, to enforce which we need to loop over all 24h-long periods of three consecutive work-shifts and make sure that per each worker the total number of assignments is less than or equal to one:
The variable needs to correctly be equal to 1 if a given worker is assigned to any of the day-shifts during the week, and 0 otherwise. Keeping in mind that is the total number of day-shifts during the week, we can implement this relationship as follows:
Indeed, whenever any of the variables is equal to 1, has to become equal to 1, while at the same time, the number 21 is big enough for the right-hand side not to be a restriction (beyond what the other constraints do) on the total number of shifts assigned to a worked in this constraint. At the same time, because in our objective function we minimize, the variable will naturally become equal to in an optimal solution where the left-hand side would be equal to 0, so the constraint correctly enforces tracking the number of employed workers during the week.
The variable needs to be equal to 1 when worker is assigned to any of the weekend day-shifts. We can formulate a constraint that will enforce this relationship correctly in the context of our problem as follows:
Indeed, variable is forced to be equal to 1 if any day-shift on the weekend is assigned to worker and since there are 6 day-shifts on the weekend, if that happens, this constraint does not impose extra limitations (beyond what the other constraints do) on the number of shifts assigned to on the weekend. At the same time, since our objective is a minimization one, will become equal to in an optimal solution where does not have any weekend day-shifts.
Pyomo implementation
Visualizing the solution
Scheduling applications generate a considerable amount of data to be used by the participants. The following cells demonstrate the preparation of charts and reports that can be used to communicate scheduling information to the store management and shift workers.
Implementing the schedule with reports
Optimal planning models can generate large amounts of data that need to be summarized and communicated to individuals for implementation.
Creating a master schedule with categorical data
The following cell creates a pandas DataFrame comprising all active assignments from the solved model. The data consists of all (worker, day, shift) tuples for which the binary decision variable m.a equals one. The data is categorical consisting of a unique id for each worker, a day of the week, or the name of a shift. Each of the categories has a natural ordering that should be used in creating reports. This is implemented using the CategoricalDtype class.
Reports for workers
Each worker should receive a report detailing their shift assignments. The reports are created by sorting the master schedule by worker, day, and shift, then grouping by worker.
Reports for store managers
The store managers need reports listing workers by assigned day and shift.