Scheduling with ease: Cost optimization tutorial for Python

And to ensure that the shop runs smoothly, each shift requires at least one manager.Structuring the problemBefore diving into the code, let’s add structure to our task by defining our objective, variables, and constraints.Objective functionIn simple words, we want to design the lowest cost schedule, accounting for both regular time and overtime..We can define this mathematically as:Where w is our list of workers, RegCost and OTCost are the dollar costs of a regular and overtime shift for each worker, respectively, and RegShifts and OTShifts are the total number of regular and overtime shifts for each worker, respectively.VariablesWe will create a list of variables for every worker/shift combination (e.g. [‘Employee1’,‘Monday1'], [‘Employee2’,‘Monday1’], etc.)..Since loading data into Python is out of the scope of this tutorial, we’ll move through this part quickly.Here’s a recap of what we now have:A list of our 14 shifts (two shifts per day for one week) and our 10 employees (lines 7–9)The number of workers needed for each shift (lines 12–13)The availability of each worker for each shift (lines 17–23)A list of who are managers and a list of who are not managers (lines 26–27)The cost of a shift for each worker, both regular and overtime (lines 31–36)A few global assumptions for min and max shifts and how many shifts are allowed before triggering overtime (lines 40–43)Initialize the modelNote: As can be seen in the above code, we are using a package called Gurobi..By setting the upper bound equal to the values in avail , we are able to embed the constraint that certain worker/shift combinations must equal 0 (i.e. when that worker is unavailable).Next, we have to create variables to handle the regular and overtime hours..The one exception is that we set overtimeTrigger to be a binary variable (0 when there is no overtime for a given worker this week and 1 when there is overtime).Add constraintsSimilarly, let’s turn each constraint outlined above into code, using the addConstrs (adding multiple constraints at a time) and addConstr (adding one constraint at a time) functions.First, we specify that the sum of assigned workers (1 for each scheduled worker, 0 for each non-scheduled worker) for each shift equals the total shift requirement:Next, we deal with the split between regular time and overtime..First, we specify that the number of regular shifts plus the number of overtime shifts is equal to the total number of shifts for each worker..Then, we ensure that the number of regular shifts is less than or equal to the number of shifts specified as our overtime trigger..We can handle this quite simply by defining a cost function that sums the total number of regular shifts times the cost of a regular shift for each worker and the total number of overtime shifts times the cost of an overtime shift for each worker.. More details

Leave a Reply