Overview

Imagine managing a coffee shop that operates 24/7, requiring staff to be scheduled across various shifts. To efficiently allocate staff while minimizing costs, we can utilize linear programming. This article demonstrates how to apply linear programming using the PuLP library in Python to find the optimal staffing solution.

Data and Problem Definition

The coffee shop’s daily schedule is divided into eight time windows, each demanding a different number of staff members. These time windows are:

Time Window Staff Required
00:00 - 03:00 15
03:00 - 06:00 20
06:00 - 09:00 55
09:00 - 12:00 46
12:00 - 15:00 59
15:00 - 18:00 40
18:00 - 21:00 48
21:00 - 00:00 30

Staff members are scheduled into four shifts:

  1. Shift 1: 00:00 - 09:00
  2. Shift 2: 06:00 - 15:00
  3. Shift 3: 12:00 - 21:00
  4. Shift 4: 18:00 - 03:00

Scheduling Challenges

A simplistic approach would be to assign the maximum number of staff required in any overlapping time windows for each shift. However, this may lead to overstaffing and increased costs. An optimal solution minimizes the number of staff while meeting all time window requirements.

Linear Programming and PuLP

Linear programming (LP) is an effective method to find optimal solutions for such constraint-based problems. PuLP is a Python library that facilitates the application of LP.

Installation

To install PuLP, use:

pip install pulp

Data Preparation

We’ll download the data using gdown:

pip install gdown

Input Parameters

We’ll create a matrix to indicate which shift each time window belongs to and define other essential parameters.

Decision Variables

Decision variables represent the unknown quantities we want to determine, i.e., the number of workers per shift. In PuLP, we specify these using LpVariable.dicts:

from pulp import LpVariable

shifts = ["Shift_1", "Shift_2", "Shift_3", "Shift_4"]
workers = LpVariable.dicts("Workers", shifts, lowBound=0, cat='Integer')

Objective Function

The goal is to minimize the total number of workers while satisfying the demand for each time window:

from pulp import LpProblem, LpMinimize

prob = LpProblem("Staffing_Problem", LpMinimize)
prob += sum(workers[shift] for shift in shifts)

Constraints

We need to ensure that the number of workers in each time window meets the required demand:

# Example constraint for time window 00:00 - 03:00
prob += workers["Shift_1"] + workers["Shift_4"] >= 15

Solving the Problem

We solve the LP problem using:

from pulp import PULP_CBC_CMD

prob.solve(PULP_CBC_CMD())

Results Interpretation

Upon solving, we interpret the results to ensure the staffing meets the demands:

for v in prob.variables():
    print(v.name, "=", v.varValue)

print("Total Workers =", sum(v.varValue for v in prob.variables()))

Visualizing the Solution

Visualizing the staffing schedule can help verify the solution. We can plot the number of workers scheduled in each time window to ensure demand is met.

import matplotlib.pyplot as plt

# Example visualization code
time_windows = ["00:00-03:00", "03:00-06:00", "06:00-09:00", "09:00-12:00", 
                "12:00-15:00", "15:00-18:00", "18:00-21:00", "21:00-00:00"]
demands = [15, 20, 55, 46, 59, 40, 48, 30]
assigned_workers = [sum(workers[shift].varValue for shift in shifts) for _ in time_windows]

plt.bar(time_windows, demands, label='Demand')
plt.bar(time_windows, assigned_workers, label='Assigned Workers', alpha=0.7)
plt.xlabel('Time Window')
plt.ylabel('Number of Workers')
plt.legend()
plt.show()

Conclusion

Using PuLP for linear programming in Python, we’ve optimized the staff scheduling for a 24/7 coffee shop. This method not only meets the staffing requirements but also minimizes labor costs. Such optimization techniques can be applied to various business operations to enhance efficiency and reduce expenses.

By utilizing Python and PuLP, managers can solve complex scheduling problems with ease, ensuring optimal resource allocation and cost management.