workforce2.py


# !/usr/bin/env python3.7 # 2023年版权,Gurobi优化狗万app足彩,LLC #分配工人的变化;每个工人可能是也可能不是在#特别的一天。如果问题不能解决,使用IIS迭代#找到所有矛盾的约束。进口gurobipy从gurobipy gp进口伽马线暴进口sys #每个转变所需的工人数量变化,shiftRequirements = gp。multidict ({“Mon1”: 3,“Tue2”: 2,“Wed3”: 4,“Thu4”: 4,“Fri5”: 5“Sat6”: 6“Sun7”: 5“Mon8”: 2,“Tue9”: 2,“Wed10”: 3,“Thu11”: 4,“Fri12”: 6“Sat13”: 7,“Sun14”: 5}) #每个工人数量支付一个轮班工作者工作,工资= gp。multidict({“鲍勃”:“艾米”:10日12日“凯西”:10“丹”:8“Ed”: 8,“弗雷德”:9,“顾”:11日})# = gp工人可用性可用性。tuplelist([(“艾米”、“Tue2”)(“艾米”、“Wed3”)(“艾米”、“Fri5”)(“艾米”、“Sun7”)(“艾米”、“Tue9”)(“艾米”、“Wed10”)(“艾米”、“Thu11”)(“艾米”、“Fri12”)(“艾米”、“Sat13”)(“艾米”、“Sun14”)(“鲍勃”、“Mon1”)(“鲍勃”、“Tue2”)(“鲍勃”、“Fri5”)(“鲍勃”、“Sat6”)(“鲍勃”、“Mon8”)(“鲍勃”、“Thu11”)(“鲍勃”、“Sat13”)(“凯西”,“Wed3”)(“凯西”,“Thu4”)(“凯西”,“Fri5”)(“凯西”,“Sun7”)(“凯西”,“Mon8”)(“凯西”,“Tue9”)(“凯西”,“Wed10”)(“凯西”,“Thu11”)(“凯西”,“Fri12”)(“凯西”,“Sat13”)(“凯西”,“Sun14”)(“丹”、“Tue2”)(“丹”、“Wed3”)(“丹”、“Fri5”)(“丹”、“Sat6”)(“丹”、“Mon8”)(“丹”、“Tue9”)(“丹”、“Wed10”)(“丹”、“Thu11”)(“丹”、“Fri12”)(“丹”、“Sat13”)(“丹”、“Sun14”) (“Ed”,“Mon1”) (“Ed”,“Tue2”) (“Ed”,“Wed3”) (“Ed”,“Thu4”) (“Ed”,“Fri5”) (“Ed”,“Sun7”) (“Ed”,“Mon8”) (“Ed”,“Tue9”) (“Ed”,“Thu11”) (“Ed”,“Sat13”) (“Ed”,“Sun14”)(“弗雷德”、“Mon1”)(“弗雷德”、“Tue2”)(“弗雷德”、“Wed3”)(“弗雷德”、“Sat6”)(“弗雷德”、“Mon8”)(“弗雷德”、“Tue9”)(“弗雷德”、“Fri12”)(“弗雷德”、“Sat13”)(“弗雷德”、“Sun14”)(“古”、“Mon1”)(“古”、“Tue2”)(“古”、“Wed3”)(“古”、“Fri5”)(“古”、“Sat6”)(“古”、“Sun7”)(“古”、“Mon8”)(“古”、“Tue9”)(“古”、“Wed10”)(“古”、“Thu11”)(“古”、“Fri12”)(“古”、“Sat13”)(“古”、“Sun14”)]) #模型m = gp.Model(“转让”)#赋值变量:x [w s] = = 1如果工人w分配转变。#自赋值模型总是产生整数解,我们使用#连续变量作为LP和解决。x = m。addVars(可用性、乌兰巴托= 1名= " x ") #目标是最小化总工资成本m.setObjective (gp。quicksum(支付[w] * x [w s] w,年代的可用性),GRB.MINIMIZE) #约束:分配完全shiftRequirements [s]工人每个年代reqCts = m.addConstrs(转变(x。总和(‘*’s) = = shiftRequirements [s] s轮班),“_”)#优化m.optimize = m()状态。状态如果状态= =伽马线暴。UNBOUNDED: print('The model cannot be solved because it is unbounded') sys.exit(0) if status == GRB.OPTIMAL: print('The optimal objective is %g' % m.ObjVal) sys.exit(0) if status != GRB.INF_OR_UNBD and status != GRB.INFEASIBLE: print('Optimization was stopped with status %d' % status) sys.exit(0) # do IIS print('The model is infeasible; computing IIS') removed = [] # Loop until we reduce to a model that can be solved while True: m.computeIIS() print('\nThe following constraint cannot be satisfied:') for c in m.getConstrs(): if c.IISConstr: print('%s' % c.ConstrName) # Remove a single constraint from the model removed.append(str(c.ConstrName)) m.remove(c) break print('') m.optimize() status = m.Status if status == GRB.UNBOUNDED: print('The model cannot be solved because it is unbounded') sys.exit(0) if status == GRB.OPTIMAL: break if status != GRB.INF_OR_UNBD and status != GRB.INFEASIBLE: print('Optimization was stopped with status %d' % status) sys.exit(0) print('\nThe following constraints were removed to get a feasible LP:') print(removed)