Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# qp_vb.vb

    ' Copyright 2025, Gurobi Optimization, LLC

    ' This example formulates and solves the following simple QP model:
    '
    '     minimize    x^2 + x*y + y^2 + y*z + z^2 + 2 x
    '     subject to  x + 2 y + 3 z >= 4
    '                 x +   y       >= 1
    '                 x, y, z non-negative
    '
    '   It solves it once as a continuous model, and once as an integer model.
    '

    Imports Gurobi

    Class qp_vb
        Shared Sub Main()
            Try
                Dim env As New GRBEnv("qp.log")
                Dim model As New GRBModel(env)

                ' Create variables

                Dim x As GRBVar = model.AddVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "x")
                Dim y As GRBVar = model.AddVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "y")
                Dim z As GRBVar = model.AddVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "z")

                ' Set objective

                Dim obj As New GRBQuadExpr()
                obj = x*x + x*y + y*y + y*z + z*z + 2*x
                model.SetObjective(obj)

                ' Add constraint: x + 2 y + 3 z >= 4

                model.AddConstr(x + 2 * y + 3 * z >= 4.0, "c0")

                ' Add constraint: x + y >= 1

                model.AddConstr(x + y >= 1.0, "c1")

                ' Optimize model

                model.Optimize()

                Console.WriteLine(x.VarName & " " & x.X)
                Console.WriteLine(y.VarName & " " & y.X)
                Console.WriteLine(z.VarName & " " & z.X)

                Console.WriteLine("Obj: " & model.ObjVal & " " & obj.Value)

                ' Change variable types to integer

                x.VType = GRB.INTEGER
                y.VType = GRB.INTEGER
                z.VType = GRB.INTEGER

                ' Optimize model

                model.Optimize()

                Console.WriteLine(x.VarName & " " & x.X)
                Console.WriteLine(y.VarName & " " & y.X)
                Console.WriteLine(z.VarName & " " & z.X)

                Console.WriteLine("Obj: " & model.ObjVal & " " & obj.Value)

                ' 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

Help and Feedback

Press `/` or click to search this documentation | Gurobi website | Gurobi community forum
Go to other Gurobi documentation | Gurobi download | Contact support