dense_vb.vb.


dense_vb.vb.


'版权所有2021,gurobi优化狗万app足彩,llc'这个例子配制并解决了以下简单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非负''该示例说明了使用密集的矩阵存储a和q'(以及其他相关的密集向量数据)。我们不推荐“您使用密集的矩阵,但如果您已以此格式已具有您的数据,此示例可能会有所帮助。导入gurobi类dense_vb保护共享函数_ dense_optimize(en en as grbenv,_行为整数,_ cols作为整数,_ c为double(),_ q为双(,),_ a为双(,),_感char(),_ rh作为double(),_ lb作为double(),_ub作为double(),_ vtype作为char(),_解决方案作为双()),作为boolean昏暗的成功作为boolean = false true dim模型作为新的grbmodel(env)'将变量添加到模型dim vars作为grbvar()= model.addvars(lb,Ub,nothing,nothing)'填充i作为整数= 0到行 -  1 dim expr of对于j作为整数= 0到cols  -  1如果a(i,j)<> 0那么expr.adterm(a(i,j),vars(j))结束,如果下一个model.addconstr(expr,感觉(i),RHS(i),“”)下一步'填充目标Dimo obj作为新的grbquadexpr()如果q Isnot,则为I作为Integer = 0到Cols  -  1,对于j作为integer = 0至cols  -  1q(i,j)<> 0然后obj.addterm(q(i,j),vars(i),vars(j))结束如果下一个接下来是j作为整数= 0到cols  -  1如果c(j) <> 0 Then obj.AddTerm(c(j), vars(j)) End If Next model.SetObjective(obj) End If ' Solve model model.Optimize() ' Extract solution If model.Status = GRB.Status.OPTIMAL Then success = True For j As Integer = 0 To cols - 1 solution(j) = vars(j).X Next End If model.Dispose() Catch e As GRBException Console.WriteLine("Error code: " & 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