Workforce5.py.


Workforce5.py.


#!/usr/bin/env python3.7 #版权所有2021,Gurobi优化狗万app足彩,LLC #分配工人轮班;每个工人可能在特定的一天上班,也可能不上班。采用多目标优化方法求解模型。最高优先级的目标使宽松工作的总数最小化(即,未覆盖工作的总数量)。次要目标#使所有工人的最大和最小轮班数之间的差值最小化。第二个优化是允许#降低第一个目标较小值的10%和2 * /导入gurobipy gp gurobipy进口伽马线暴进口sys # #示例数据集的天,工人转变=[“Mon1”、“Tue2”,“Wed3”、“Thu4”,“Fri5”、“Sat6”,“Sun7”,“Mon8”,“Tue9”,“Wed10”,“Thu11”,“Fri12”,“Sat13”,“Sun14”]工人=[“艾米”、“Bob”、“凯西”、“丹”,“Ed”,“弗雷德”、“古”、“托比”】#每个转变所需的工人数量S =[3、2、4、4、5、6、5、2、2、3、4、6、7、5]shiftRequirements = {S:[我]因为我,年代在列举(转变)}#工人的可用性:0如果工人没有转变= [[0,1,1,0,1,0,1,0,1,1,1,1,1,1),(1,- 1,0,0,1,1,0,1,0,0,1,0,1,0],[0,0,1,1,- 1,0,1,1,1,1,1,1,1,1],[0,1,1,0,1,1,0,1,1,1,1,1,1,1],[1,1,1,1,- 1,0,1,1,1,0,1,0,1,1],[1,1,1,0,0,1,0,1,1,0,0,1,1,1],[0,1,1,1,0,1,1,0,1,1,1,0,1,1]、[1,1,1,0,1,1,1,1,1,1,1,1,1,1],)可用性= {(w s):我的[j][我],在列举(转变)j, w在列举(工人)}试题:#与上下文管理器创建模型。 Upon exit from this block, # model.dispose is called automatically, and memory consumed by the model # is released. # # The model is created in the default environment, which will be created # automatically upon model construction. For safe release of resources # tied to the default environment, disposeDefaultEnv is called below. with gp.Model("workforce5") as model: # Initialize assignment decision variables: # x[w][s] == 1 if worker w is assigned to shift s. # This is no longer a pure assignment model, so we must # use binary variables. x = model.addVars(availability.keys(), ub=availability, vtype=GRB.BINARY, name='x') # Slack variables for each shift constraint so that the shifts can # be satisfied slacks = model.addVars(Shifts, name='Slack') # Variable to represent the total slack totSlack = model.addVar(name='totSlack') # Variables to count the total shifts worked by each worker totShifts = model.addVars(Workers, name='TotShifts') # Constraint: assign exactly shiftRequirements[s] workers # to each shift s, plus the slack model.addConstrs( (x.sum('*', s) + slacks[s] == shiftRequirements[s] for s in Shifts), name='shiftRequirement') # Constraint: set totSlack equal to the total slack model.addConstr(totSlack == slacks.sum(), name='totSlack') # Constraint: compute the total number of shifts for each worker model.addConstrs((totShifts[w] == x.sum(w, '*') for w in Workers), name='totShifts') # Constraint: set minShift/maxShift variable to less/greater than the # number of shifts among all workers minShift = model.addVar(name='minShift') maxShift = model.addVar(name='maxShift') model.addGenConstrMin(minShift, totShifts, name='minShift') model.addGenConstrMax(maxShift, totShifts, name='maxShift') # Set global sense for ALL objectives model.ModelSense = GRB.MINIMIZE # Set up primary objective model.setObjectiveN(totSlack, index=0, priority=2, abstol=2.0, reltol=0.1, name='TotalSlack') # Set up secondary objective model.setObjectiveN(maxShift - minShift, index=1, priority=1, name='Fairness') # Save problem model.write('workforce5.lp') # Optimize model.optimize() status = model.Status if status in (GRB.INF_OR_UNBD, GRB.INFEASIBLE, GRB.UNBOUNDED): print('Model cannot be solved because it is infeasible or unbounded') sys.exit(0) if status != GRB.OPTIMAL: print('Optimization was stopped with status ' + str(status)) sys.exit(0) # Print total slack and the number of shifts worked for each worker print('') print('Total slack required: ' + str(totSlack.X)) for w in Workers: print(w + ' worked ' + str(totShifts[w].X) + ' shifts') print('') except gp.GurobiError as e: print('Error code ' + str(e.errno) + ": " + str(e)) except AttributeError as e: print('Encountered an attribute error: ' + str(e)) finally: # Safely release memory and/or server side resources consumed by # the default environment. gp.disposeDefaultEnv()