Repository navigation
Expand file tree
/
Copy pathworkforce_planning.py
More file actions
174 lines (127 loc) · 4.83 KB
/
Copy pathworkforce_planning.py
File metadata and controls
174 lines (127 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Workforce Planning Optimization - Minimize Staffing
This script optimizes workforce scheduling to minimize the total number of staff
while meeting daily demand requirements, considering 5-day work weeks with 2 consecutive days off.
"""
import pandas as pd
from pulp import LpProblem, LpMinimize, LpVariable, lpSum, LpStatus, value
import matplotlib.pyplot as plt
from itertools import chain, repeat
def ncycles(iterable, n):
"""Returns the sequence elements n times."""
return chain.from_iterable(repeat(tuple(iterable), n))
def create_schedule_constraints():
"""Create circular lists for working days and days off."""
n_days = list(range(7))
n_days_c = list(ncycles(n_days, 3))
# Working days (5 consecutive days)
list_in = [[n_days_c[j] for j in range(i, i + 5)] for i in n_days_c]
# Days off (2 consecutive days after 5 working days)
list_excl = [[n_days_c[j] for j in range(i + 1, i + 3)] for i in n_days_c]
return n_days, list_in, list_excl
def build_and_solve_model(n_days, list_excl, n_staff):
"""Build and solve the workforce optimization model."""
model = LpProblem("Minimize_Staffing", LpMinimize)
# Decision variables: number of workers starting their shift on each day
x = LpVariable.dicts("shift", n_days, lowBound=0, cat="Integer")
# Objective: minimize total number of staff
model += lpSum([x[i] for i in n_days])
# Constraints: meet daily staff demand
for d, l_excl, staff in zip(n_days, list_excl, n_staff):
model += lpSum([x[i] for i in n_days if i not in l_excl]) >= staff
model.solve()
return model, x
def extract_results(model, x, n_days, list_in, jours):
"""Extract and format optimization results."""
# Extract shift assignments
dct_work = {int(v.name[-1]): int(v.varValue) for v in model.variables()}
# Build schedule matrix
start_jours = ["Shift: " + j for j in jours]
dict_sch = {}
for day in dct_work.keys():
dict_sch[day] = [dct_work[day] if i in list_in[day] else 0 for i in n_days]
df_sch = pd.DataFrame(dict_sch).T
df_sch.columns = jours
df_sch.index = start_jours
return df_sch, dct_work
def display_results(model, df_sch, n_staff, jours):
"""Display optimization results."""
print("=" * 60)
print("WORKFORCE PLANNING OPTIMIZATION RESULTS")
print("=" * 60)
print(f"\nStatus: {LpStatus[model.status]}")
print(f"Total number of Staff = {int(value(model.objective))}")
print("\n" + "-" * 60)
print("SHIFT SCHEDULE (workers per shift per day)")
print("-" * 60)
print(df_sch.to_string())
print("\n" + "-" * 60)
print("DAILY STAFFING SUMMARY")
print("-" * 60)
df_staff = pd.DataFrame({"Days": jours, "Staff Demand": n_staff})
df_staff = df_staff.set_index("Days")
df_staff["Staff Supply"] = df_sch.sum(axis=0).values
df_staff["Extra Resources"] = df_staff["Staff Supply"] - df_staff["Staff Demand"]
print(df_staff.to_string())
return df_staff
def plot_results(df_staff, save_path=None):
"""Plot workforce demand vs supply."""
fig, ax1 = plt.subplots(figsize=(12, 6))
x_pos = range(len(df_staff))
width = 0.35
ax1.bar(
[p - width / 2 for p in x_pos],
df_staff["Staff Demand"],
width,
label="Staff Demand",
color="black",
)
ax1.bar(
[p + width / 2 for p in x_pos],
df_staff["Staff Supply"],
width,
label="Staff Supply",
color="red",
)
ax1.set_xlabel("Day of the week")
ax1.set_ylabel("Number of Workers")
ax1.set_title("Workforce: Demand vs. Supply")
ax1.set_xticks(x_pos)
ax1.set_xticklabels(df_staff.index)
ax1.legend(loc="upper left")
ax2 = ax1.twinx()
ax2.plot(
x_pos,
df_staff["Extra Resources"],
color="blue",
linewidth=3,
marker="o",
label="Extra Resources",
)
ax2.set_ylabel("Extra Resources")
ax2.legend(loc="upper right")
plt.tight_layout()
if save_path:
plt.savefig(save_path)
print(f"\nChart saved to: {save_path}")
else:
plt.show()
return fig
def main():
"""Main function to run workforce optimization."""
# Input data: staff needs per day (FTE)
n_staff = [31, 45, 40, 40, 48, 30, 25]
jours = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
# Create schedule constraints
n_days, list_in, list_excl = create_schedule_constraints()
# Build and solve model
model, x = build_and_solve_model(n_days, list_excl, n_staff)
# Extract results
df_sch, dct_work = extract_results(model, x, n_days, list_in, jours)
# Display results
df_staff = display_results(model, df_sch, n_staff, jours)
# Plot results
plot_results(df_staff, save_path="workforce_chart.png")
return df_sch, df_staff
if __name__ == "__main__":
main()