Skip to main content

How to get number of binary variables and number of continous variables after doing optimisation?



1 comment

  • Lennart Lahrs
    Gurobi Staff Gurobi Staff

    Hi Margi,

    The Gurobi log outputs the number of continuous, integer, and binary variables.
    Note that any binary variable is also considered an integer variable. E.g.:

    Optimize a model with 0 rows, 3 columns and 0 nonzeros
    Model fingerprint: 0x5fa75176
    Variable types: 1 continuous, 2 integer (1 binary)

    Using the Gurobi Python API, you can access the information like shown below. The number of continuous variables is the total number of variables minus the number of integer variables. You can also get a list of all variables and check variable types individually.

    import gurobipy as gp
    from gurobipy import GRB

    m = gp.Model()
    assert m.NumVars == 3
    assert m.NumIntVars == 2
    assert m.NumBinVars == 1

    vars = m.getVars()
    assert vars[0].VType == GRB.CONTINUOUS
    assert vars[1].VType == GRB.INTEGER
    assert vars[2].VType == GRB.BINARY

Please sign in to leave a comment.