workforce5.py


# !/usr/bin/env python3.7 # 2023年版权,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在列举(工人)}试题:#与上下文管理器创建模型。在退出这个块,#模型。自动处理被称为,模型#所消耗的内存释放。# #默认环境中创建的模型,将#自动创建模型建设。 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()