Skip to main content

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

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • Lennart Lahrs
    • 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()
    m.addVar(vtype=GRB.CONTINUOUS)
    m.addVar(vtype=GRB.INTEGER)
    m.addVar(vtype=GRB.BINARY)
    m.optimize()
    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
    0

Post is closed for comments.