manbet体育手机客户端


callback_vb.vb


“Copyright 2019, G狗万app足彩urobi Optimization, LLC”这个例子从一个文件中读取一个模型,设置一个回调,“监控优化过程并实现一个自定义的”终止策略,并将进度信息输出到“屏幕”和日志文件中。“终止策略中实现这个回调停止MIP模型的优化一次至少一个的以下两个条件满足:1)最优差距小于10%”2)探究出了至少10000个节点,和一个整数可行解决方案被发现。注意,终止通常是通过Gurobi参数(MIPGap、NodeLimit等)处理的。只有当可用参数没有捕捉到你想要的终止条件时,你才应该使用回调函数。进口系统进口Gurobi类callback_vb继承GRBCallback私人var GRBVar()私人lastnode双私人lastiter双公共子新(按值传递xvars作为GRBVar ()) var = xvars lastnode = lastiter = 1结束子过载保护覆盖子回调()尝试如果然后= GRB.Callback.PRESOLVE Presolve调暗正保远程教育As Integer = GetIntInfo(GRB.Callback.PRE_COLDEL) Dim rdels As Integer = GetIntInfo(GRB.Callback.PRE_ROWDEL) Console.WriteLine(cdels & " columns and " & rdels & " rows are removed") ElseIf where = GRB.Callback.SIMPLEX Then ' Simplex callback Dim itcnt As Double = GetDoubleInfo(GRB.Callback.SPX_ITRCNT) If itcnt Mod - lastiter >= 100 Then lastiter = itcnt Dim obj As Double = GetDoubleInfo(GRB.Callback.SPX_OBJVAL) Dim pinf As Double = GetDoubleInfo(GRB.Callback.SPX_PRIMINF) Dim dinf As Double = GetDoubleInfo(GRB.Callback.SPX_DUALINF) Dim ispert As Integer = GetIntInfo(GRB.Callback.SPX_ISPERT) Dim ch As Char If ispert = 0 Then ch = " "c ElseIf ispert = 1 Then ch = "S"c Else ch = "P"c End If Console.WriteLine(itcnt & " " & obj & ch & " " & pinf & " " & dinf) End If ElseIf where = GRB.Callback.MIP Then ' General MIP callback Dim nodecnt As Double = GetDoubleInfo(GRB.Callback.MIP_NODCNT) If nodecnt - lastnode >= 100 Then lastnode = nodecnt Dim objbst As Double = GetDoubleInfo(GRB.Callback.MIP_OBJBST) Dim objbnd As Double = GetDoubleInfo(GRB.Callback.MIP_OBJBND) If Math.Abs(objbst - objbnd) < 0.1 * (1.0R + Math.Abs(objbst)) Then Abort() End If Dim actnodes As Integer = CInt(GetDoubleInfo(GRB.Callback.MIP_NODLFT)) Dim itcnt As Integer = CInt(GetDoubleInfo(GRB.Callback.MIP_ITRCNT)) Dim solcnt As Integer = GetIntInfo(GRB.Callback.MIP_SOLCNT) Dim cutcnt As Integer = GetIntInfo(GRB.Callback.MIP_CUTCNT) Console.WriteLine(nodecnt & " " & actnodes & " " & itcnt & " " & _ objbst & " " & objbnd & " " & solcnt & " " & cutcnt) End If ElseIf where = GRB.Callback.MIPSOL Then ' MIP solution callback Dim obj As Double = GetDoubleInfo(GRB.Callback.MIPSOL_OBJ) Dim nodecnt As Integer = CInt(GetDoubleInfo(GRB.Callback.MIPSOL_NODCNT)) Dim x As Double() = GetSolution(vars) Console.WriteLine("**** New solution at node " & nodecnt & ", obj " & _ obj & ", x(0) = " & x(0) & "****") End If Catch e As GRBException Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message) Console.WriteLine(e.StackTrace) End Try End Sub Shared Sub Main(ByVal args As String()) If args.Length < 1 Then Console.WriteLine("Usage: callback_vb filename") Return End If Try Dim env As New GRBEnv() Dim model As New GRBModel(env, args(0)) Dim vars As GRBVar() = model.GetVars() ' Create a callback object and associate it with the model model.SetCallback(New callback_vb(vars)) model.Optimize() Dim x As Double() = model.Get(GRB.DoubleAttr.X, vars) Dim vnames As String() = model.Get(GRB.StringAttr.VarName, vars) For j As Integer = 0 To vars.Length - 1 If x(j) <> 0.0R Then Console.WriteLine(vnames(j) & " " & x(j)) End If Next ' Dispose of model and env model.Dispose() env.Dispose() Catch e As GRBException Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message) Console.WriteLine(e.StackTrace) End Try End Sub End Class