Try our new documentation site (beta).


' Copyright 2016, Gurobi Optimization, Inc.

' A simple sensitivity analysis example which reads a MIP model
' from a file and solves it. Then each binary variable is set
' to 1-X, where X is its value in the optimal solution, and
' the impact on the objective function value is reported.

Imports System
Imports Gurobi

Class sensitivity_vb
    Shared Sub Main(args As String())
        If args.Length < 1 Then
            Console.Out.WriteLine("Usage: sensitivity_vb filename")
        End If


            ' Create environment

            Dim env As New GRBEnv()

            ' Read and solve model

            Dim model As New GRBModel(env, args(0))

            If model.Get(GRB.IntAttr.IsMIP) = 0 Then
                Console.WriteLine("Model is not a MIP")
            End If


            If model.Get(GRB.IntAttr.Status) <> GRB.Status.OPTIMAL Then
                Console.WriteLine("Optimization ended with status " & _
            End If

            ' Store the optimal solution

            Dim origObjVal As Double = model.Get(GRB.DoubleAttr.ObjVal)
            Dim vars As GRBVar() = model.GetVars()
            Dim origX As Double() = model.Get(GRB.DoubleAttr.X, vars)

            ' Disable solver output for subsequent solves

            model.GetEnv().Set(GRB.IntParam.OutputFlag, 0)

            ' Iterate through unfixed, binary variables in model

            For i As Integer = 0 To vars.Length - 1
                Dim v As GRBVar = vars(i)
                Dim vType As Char = v.Get(GRB.CharAttr.VType)

                If v.Get(GRB.DoubleAttr.LB) = 0 AndAlso _
                   v.Get(GRB.DoubleAttr.UB) = 1 AndAlso _
                   (vType = GRB.BINARY OrElse vType = GRB.INTEGER) Then

                    ' Set variable to 1-X, where X is its value in optimal solution

                    If origX(i) < 0.5 Then
                        v.Set(GRB.DoubleAttr.LB, 1.0)
                        v.Set(GRB.DoubleAttr.Start, 1.0)
                        v.Set(GRB.DoubleAttr.UB, 0.0)
                        v.Set(GRB.DoubleAttr.Start, 0.0)
                    End If

                    ' Update MIP start for the other variables

                    For j As Integer = 0 To vars.Length - 1
                        If j <> i Then
                            vars(j).Set(GRB.DoubleAttr.Start, origX(j))
                        End If

                    ' Solve for new value and capture sensitivity information


                    If model.Get(GRB.IntAttr.Status) = GRB.Status.OPTIMAL Then
                        Console.WriteLine("Objective sensitivity for variable " & _
                                          v.Get(GRB.StringAttr.VarName) & _
                                          " is " & _
                                          (model.Get(GRB.DoubleAttr.ObjVal) - origObjVal))
                        Console.WriteLine("Objective sensitivity for variable " & _
                                          v.Get(GRB.StringAttr.VarName) & _
                                          " is infinite")
                    End If

                    ' Restore the original variable bounds

                    v.Set(GRB.DoubleAttr.LB, 0.0)
                    v.Set(GRB.DoubleAttr.UB, 1.0)
                End If

            ' Dispose of model and environment


        Catch e As GRBException
            Console.WriteLine("Error code: " + e.ErrorCode)
        End Try
    End Sub
End Class

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.