workforce3_vb.vb


workforce3_vb.vb


“Copyright 2021, G狗万app足彩urobi Optimization, LLC”分配工人轮班;每个员工可能在“特定的一天”有空,也可能不在。如果问题无法解决,放松模型,以确定哪些约束不能满足,以及它们需要放松多少。进口系统进口Gurobi类workforce3_vb共享子Main()尝试和工人的样本数据集天暗转变为字符串()=新的字符串(){“Mon1”、“Tue2”,“Wed3”、“Thu4”_“Fri5”、“Sat6”、“Sun7”、“Mon8”_“Tue9”、“Wed10”、“Thu11”_“Fri12”、“Sat13”、“Sun14”}昏暗的工人字符串()=新的字符串(){“艾米”、“Bob”、“凯西”、“丹”,_ "Ed", "Fred", "Gu"}模糊nshift为整数= shift。Length Dim nWorkers As Integer = Workers。长度所需的工人数量每个转变昏暗shiftRequirements双()= New双(){3、2、4、4、5、6日_ 5,2,2,3,4,6,_ 7,5}的金额支付给每个工人工作一个昏暗的支付转移双()= New双(){10、12、10、8、8,9,11}的工人的可用性: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}, _ {1,1,1,1,1,1,1,1,1,1}} ' Model Dim env As New GRBEnv() Dim Model As New GRBModel(env) Model。ModelName = "assignment" '赋值变量:x[w][s] == 1,如果工人w被赋值'到移位s。由于赋值模型总是产生整数'解决方案,我们使用连续变量并作为LP求解。{/ /将GRBVar(,) = New GRBVar(nWorkers - 1, nShifts - 1) {} For w As Integer = 0 To nWorkers - 1 For s As Integer = 0 To nshift - 1 =模型。AddVar(0,可用性(w, s),支付(w), _ GRB。连续的,_ Workers(w) & "。" & Shifts(s)) Next Next ' The objective is to minimize the total pay costs model.ModelSense = GRB.MINIMIZE ' Constraint: assign exactly shiftRequirements[s] workers ' to each shift s For s As Integer = 0 To nShifts - 1 Dim lhs As GRBLinExpr = 0.0 For w As Integer = 0 To nWorkers - 1 lhs.AddTerm(1.0, x(w, s)) Next model.AddConstr(lhs = shiftRequirements(s), Shifts(s)) Next ' Optimize model.Optimize() Dim status As Integer = model.Status If status = GRB.Status.UNBOUNDED Then Console.WriteLine("The model cannot be solved " & _ "because it is unbounded") Return End If If status = GRB.Status.OPTIMAL Then Console.WriteLine("The optimal objective is " & model.ObjVal) Return End If If (status <> GRB.Status.INF_OR_UNBD) AndAlso _ (status <> GRB.Status.INFEASIBLE) Then Console.WriteLine("Optimization was stopped with status " & _ status) Return End If ' Relax the constraints to make the model feasible Console.WriteLine("The model is infeasible; relaxing the constraints") Dim orignumvars As Integer = model.NumVars model.FeasRelax(0, False, False, True) model.Optimize() status = model.Status If (status = GRB.Status.INF_OR_UNBD) OrElse _ (status = GRB.Status.INFEASIBLE) OrElse _ (status = GRB.Status.UNBOUNDED) Then Console.WriteLine("The relaxed model cannot be solved " & _ "because it is infeasible or unbounded") Return End If If status <> GRB.Status.OPTIMAL Then Console.WriteLine("Optimization was stopped with status " & status) Return End If Console.WriteLine(vbLf & "Slack values:") Dim vars As GRBVar() = model.GetVars() For i As Integer = orignumvars To model.NumVars - 1 Dim sv As GRBVar = vars(i) If sv.X > 1E-06 Then Console.WriteLine(sv.VarName & " = " & sv.X) End If Next ' Dispose of model and environment model.Dispose() env.Dispose() Catch e As GRBException Console.WriteLine("Error code: " + e.ErrorCode & ". " + e.Message) End Try End Sub End Class