Search code examples
gurobijulia-jump

How to get list of basic variables from JuMP/Gurobi?


I'm solving an LP in Julia/JuMP using Gurobi as my solver. I want to know which variables in my solution are basic. How do I obtain this information?

Just checking which variables are non-zero is not sufficient, since we might be dealing with a degenerate solution (i.e., basic variables equal to zero).

I found two similar questions online, but the solutions suggested do not seem to apply to the current version of JuMP anymore (or am I missing something?):

  1. https://discourse.julialang.org/t/how-to-obtain-a-basic-solution/1784
  2. https://groups.google.com/g/julia-opt/c/vJ6QuFBfPbw?pli=1

There's a Gurobi attribute called VBasis (https://www.gurobi.com/documentation/9.0/refman/vbasis.html) that seems to be what I'm looking for, but I don't know how to access it.


Solution

  • This is poorly documented, but you can see which constraints are basic:

    model = Model(Gurobi.Optimizer)
    @variable(model, x >= 0)
    @constraint(model, c, 2x >= 1)
    @objective(model, Min, x)
    optimize!(model)
    
    julia> MOI.get(model, MOI.ConstraintBasisStatus(), c)
    NONBASIC::BasisStatusCode = 1
    
    julia> MOI.get(model, MOI.ConstraintBasisStatus(), LowerBoundRef(x))
    BASIC::BasisStatusCode = 0
    

    Note that since variables can have lower and upper bounds, we report which constraints are basic, rather than which variables are.

    Documentation: https://jump.dev/MathOptInterface.jl/stable/apireference/#MathOptInterface.ConstraintBasisStatus https://jump.dev/MathOptInterface.jl/stable/apireference/#MathOptInterface.BasisStatusCode