You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm new to Pulp and have been trying to convert a simple Gurobi-based code snippet to Pulp with no success unfortunately. Any help would be highly appreciated so I can study the code and slowly transition to Pulp completely. Please find my very simple driver-scheduling script in Gurobi. Thank you for your time.
import pandas as pd
import numpy as np
from gurobipy import *
shiftList = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
workerList = ['Driver_1', 'Driver_2', 'Driver_3', 'Driver_4', 'Driver_5']
shiftReq = [2, 2, 4, 2, 5, 4, 5]
shiftRequirements = {s: shiftReq[i] for i, s in enumerate(shiftList)}
# Assume everyone is available
availability = pd.DataFrame(np.ones((len(workerList), len(shiftList))), index=workerList,
columns=shiftList)
# Create availability dictionary to be used in decision variable bounding
avail = {(w, s): availability.loc[w, s] for w in workerList for s in shiftList}
mgmtList = []
nonmgmtList = [x for x in workerList if x not in mgmtList]
# Range of shifts that every workers is required to stay between
minShifts = 2
maxShifts = 6
model = Model("Workers Scheduling")
# ub ensures that workers are only staffed when they are available
x = model.addVars(workerList, shiftList, ub=avail, vtype=GRB.BINARY, name='x')
regHours = model.addVars(workerList, name='regHrs')
# Ensure proper number of workers are scheduled
shiftReq = model.addConstrs((
(x.sum('*', s) == shiftRequirements[s] for s in shiftList)
), name='shiftRequirement')
# Decompose total shifts for each worker into regular shifts and OT shifts
regOT1 = model.addConstrs((regHours[w] == x.sum(w, '*') for w in workerList))
# Ensure each worker stays within min and max shift bounds
minShiftsConstr = model.addConstrs(((x.sum(w, '*') >= minShifts for w in workerList)),
name='minShifts')
maxShiftsConstr = model.addConstrs(((x.sum(w, '*') <= maxShifts for w in workerList)),
name='maxShifts')
# Minimize total cost, accounting for pay difference between regular time and overtime
model.ModelSense = GRB.MINIMIZE
Cost = 0
Cost += (quicksum(regHours[w] for w in workerList))
model.setObjective(Cost)
model.optimize()
sol = pd.DataFrame(data={'Solution': model.X}, index=model.VarName)
sol = sol.iloc[0:len(x)]
dashboard = pd.DataFrame(index=workerList, columns=shiftList)
for w in workerList:
for s in shiftList:
dashboard.at[w, s] = sol.loc['x[' + w + ',' + s + ']',][0]
print(dashboard)
shiftAssignments = {}
for s in shiftList:
shiftAssignments.update({s: list(dashboard[dashboard[s] == 1].loc[:, ].index)})
print(shiftAssignments)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
I'm new to Pulp and have been trying to convert a simple Gurobi-based code snippet to Pulp with no success unfortunately. Any help would be highly appreciated so I can study the code and slowly transition to Pulp completely. Please find my very simple driver-scheduling script in Gurobi. Thank you for your time.
Beta Was this translation helpful? Give feedback.
All reactions