How to get number of binary variables and number of continous variables after doing optimisation?
AnsweredI run a model using Gurobi. I want to find the number of binary variables and continuous variables of the model. How do I do it?
0

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.BINARY0
Please sign in to leave a comment.
Comments
1 comment