workforce2_vb.vb


“版权2023,Gurobi Opt狗万app足彩imization, LLC”“分配工人轮班;每个员工都可能在某一天上班,也可能不在某一天上班。如果问题无法解决,则迭代地使用IIS来“找到所有冲突的约束”。import System. collections . generic Imports Gurobi Class workforce2_vb Shared Sub Main() Try ' Sample data ' days and workers Dim Shifts As String() = New String() {"Mon1", "Tue2", "Wed3", "Thu4", _ "Fri5", "Sat6", "Sun7", "Mon8", _ "Tue9", "Wed10", "Thu11", _ "Fri12", "Sat13", "Sun14"} Dim workers As String() = New String() {"Amy", "Bob", "Cathy", "Dan", _ "Ed", "Fred", "Gu"} Dim nShifts As Integer = Shifts。长度Dim nWorkers As Integer = Workers。长度'每个班次所需的工人数量Dim shiftreations As Double() = New Double(){3,2,4,4,5,6,_ 5,2,2,3,4,6,_ 7,5} '每个工人在一个班次工作的报酬Dim pay As Double() = New Double(){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,0,1,1,1,1,1,1,1,1,1,1}}”模式模糊env新GRBEnv()的模型作为新GRBModel (env)模型。赋值变量:x(w)(s) == 1,如果工人w被赋值给shift s。由于赋值模型总是产生整数解,我们使用连续变量并作为LP求解。Dim x As GRBVar(,) = New GRBVar(nWorkers - 1, nShifts - 1){}对于w As Integer = 0到nWorkers - 1对于s As Integer = 0到nShifts - 1 x(w, s) =模型。AddVar(0,可用性(w, s),支付(w), _ GRB。连续,_工人(w) & "。"“我们的目标是最小化总薪酬成本模型。ModelSense = GRB。最小化'约束:分配精确shifrequiremings (s)工人'到每个shift s For s As Integer = 0到nShifts - 1 Dim lhs As GRBLinExpr = 0 For w As Integer = 0到nWorkers - 1 lhs. addterm (1.0, x(w, s))下一个模型。AddConstr(lhs = shiftrequiments (s), Shifts(s)) Next '优化模型。优化()Dim status As Integer =模型。Status如果Status = GRB.Status.UNBOUNDED则为控制台。WriteLine("模型不能被解决" & _ "因为它是无界的")退出子结束If If status = GRB.Status.OPTIMAL Then控制台。WriteLine("The optimal objective is " & model.ObjVal) Exit Sub End If If (status <> GRB.Status.INF_OR_UNBD) AndAlso _ (status <> GRB.Status.INFEASIBLE) Then Console.WriteLine("Optimization was stopped with status " & status) Exit Sub End If ' Do IIS Console.WriteLine("The model is infeasible; computing IIS") Dim removed As LinkedList(Of String) = New LinkedList(Of String)() ' Loop until we reduce to a model that can be solved While True model.ComputeIIS() Console.WriteLine(vbLf & "The following constraint cannot be satisfied:") For Each c As GRBConstr In model.GetConstrs() If c.IISConstr = 1 Then Console.WriteLine(c.ConstrName) ' Remove a single constraint from the model removed.AddFirst(c.ConstrName) model.Remove(c) Exit For End If Next Console.WriteLine() model.Optimize() status = model.Status If status = GRB.Status.UNBOUNDED Then Console.WriteLine("The model cannot be solved " & _ "because it is unbounded") Exit Sub End If If status = GRB.Status.OPTIMAL Then Exit While End If If (status <> GRB.Status.INF_OR_UNBD) AndAlso _ (status <> GRB.Status.INFEASIBLE) Then Console.WriteLine("Optimization was stopped with status " & _ status) Exit Sub End If End While Console.WriteLine(vbLf & "The following constraints were removed " & _ "to get a feasible LP:") For Each s As String In removed Console.Write(s & " ") Next Console.WriteLine() ' Dispose of model and env model.Dispose() env.Dispose() Catch e As GRBException Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message) End Try End Sub End Class