dense_vb.vb


dense_vb.vb


“版权2021,Gurobi优化,L狗万app足彩LC”“这个例子制定和解决了以下简单的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 GRBEnv, _行整数,_整数关口,_ c双()_问双(,),_为双(,),_是Char (), () _ rhs高一倍,_磅双()、双()_乌兰巴托,_ vtype Char (),_ solution As Double()) As Boolean Dim success As Boolean = False Try Dim model As New GRBModel(env) '将变量添加到模型Dim vars中,如GRBVar() = model。AddVars(lb, ub, Nothing, vtype, Nothing) '填充矩阵为i作为整数= 0到行- 1 Dim expr作为新GRBLinExpr() For j作为整数= 0到cols - 1如果A(i, j) <> 0然后expr。AddTerm(A(i, j), vars(j))结束If下一个模型。AddConstr(expr, sense(i), rhs(i), "") Next '填充客观Dim obj作为新的GRBQuadExpr()如果Q不是什么然后For i As Integer = 0 To cols - 1 For j As Integer = 0 To 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) <> 0 Then obj.AddTerm(c(j), vars(j)) End If Next model. setobjective (obj) End If ' Solve model model. optimize () '提取解If模型。Status = GRB.Status.OPTIMAL Then success = True For j As Integer = 0 To cols - 1 solution(j) = vars(j). Then success = True For j As Integer = 0 To cols - 1 solution(j) = vars(j)。X Next End If model.Dispose() Catch e作为GRBException控制台。WriteLine("错误代码:" & e.ErrorCode & ")。 " & e.Message) End Try Return success End Function Public Shared Sub Main(args As String()) Try Dim env As New GRBEnv() Dim c As Double() = New Double() {1, 1, 0} Dim Q As Double(,) = New Double(,) {{1, 1, 0}, {0, 1, 1}, {0, 0, 1}} Dim A As Double(,) = New Double(,) {{1, 2, 3}, {1, 1, 0}} Dim sense As Char() = New Char() {">"C, ">"C} Dim rhs As Double() = New Double() {4, 1} Dim lb As Double() = New Double() {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) If success Then Console.WriteLine("x: " & sol(0) & ", y: " & sol(1) & ", z: " & sol(2)) End If ' Dispose of environment env.Dispose() Catch e As GRBException Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message) End Try End Sub End Class