dense_vb.vb


Gurobi优化版权2023年,LL狗万app足彩C的”这个例子中制定和解决以下简单的QP模型:' '减少x + y + x ^ 2 + x * y + y ^ 2 + y * z + z ^ 2》受到x + 2 y + 3 z > = 4 x + y > = 1的x, y, z非负的密度矩阵的示例展示了使用存储和Q”(和密集的其他相关数据的向量)。我们不建议您使用密集矩阵,但是如果您已经有了这种格式的数据,那么这个示例可能会有所帮助。导入Gurobi类dense_vb受保护共享函数_ dense_optimize(env As GRBEnv, _ rows As Integer, _ cols As Integer, _ c As Double(), _ Q As Double(,), _ A As Double(,), _ sense As Char(), _ rhs As Double(), _ lb As Double(), _ ub As Double(), _ vtype As Char(), _ solution As Double()) As Boolean Dim success As Boolean = False Try Dim model As New GRBModel(env) '将变量添加到模型Dim变量中,如GRBVar() = model。AddVars(lb, ub, Nothing, vtype, Nothing) '填充一个矩阵For i As Integer = 0 To rows - 1 Dim expr As New GRBLinExpr() For j As Integer = 0 To cols - 1如果A(i, j) <> 0则expr。AddTerm(A(i, j), vars(j)) End If下一个模型。AddConstr(expr, sense(i), rhs(i), "") Next '填充目标Dim obj作为新的GRBQuadExpr()如果Q不是无,那么For i作为整数= 0到cols - 1 For j作为整数= 0到cols - 1如果Q(i, j) <> 0,那么obj。AddTerm(Q(i, j), vars(i), vars(j)) End If Next Next For j As Integer = 0 To cols - 1 If c(j) <> Then obj.AddTerm(c(j), vars(j)) End If Next model. setobjobjective (obj) End If ' Solve model model. optimize () '提取溶液If模型。Status = GRB.Status.OPTIMAL然后success = True For j As Integer = 0 To cols - 1 solution(j) = vars(j)。X Next End If model.Dispose() Catch e As grbeexception Console。WriteLine("错误代码:" & e.ErrorCode & ")。“& e.Message)结束尝试返回成功结束函数公共共享子主要(arg作为字符串())尽可能暗env新GRBEnv()的c双()= New双(){1 1 0}暗问,双新双(,)(,)={{1 1 0},{0,1,1},{0,0,1}}昏暗,双新双(,)(,)={{1,2,3},{1 1 0}}昏暗的感觉,Char () = New Char(){”>“c”>“c}的皇家园艺作为双()= New双(){4 1}昏暗的磅作为双()= New双(){0,0,0} Dim success As Boolean Dim sol As Double() = New Double(2) {} success = dense_optimize(env, 2,3, c, Q, A, sense, rhs, lb, Nothing, _ Nothing, sol)如果success则Console。WriteLine("x: " & sol(0) & ", y: " & sol(1) & ", z: " & sol(2)) End If ' Dispose of environment .Dispose() Catch e As grbeexception Console。WriteLine("错误代码:" & e.ErrorCode & ")。 " & e.Message) End Try End Sub End Class