gurobipy.Column#
- class Column#
Gurobi column object. A column consists of a list of coefficient, constraint pairs. Columns are used to represent the set of constraints in which a variable participates, and the associated coefficients. They are temporary objects that typically have short lifespans.
You generally build columns using the
Column
constructor. Terms can be added to an existing column usingaddTerms
. Terms can also be removed from a column usingremove
.Individual terms in a column can be queried using the
getConstr
, andgetCoeff
methods. You can query the number of terms in the column using thesize
method.- Column(coeffs=None, constrs=None)#
Column constructor.
- Parameters:
coeffs – (optional) Lists the coefficients associated with the members of
constrs
.constrs – (optional) Constraint or constraints that participate in expression. If
constrs
is a list, thencoeffs
must contain a list of the same length. Ifconstrs
is a single constraint, thencoeffs
must be a scalar.
- Returns:
An expression object.
- Example:
constrs = model.getConstrs() c = Column() c.addTerms(3.0, constrs[0]) model.addVar(vtype=GRB.BINARY, obj=1.0, column=c) model.addVar(vtype=GRB.INTEGER, column=Column(3.0, constrs[0])) model.addVar(obj=3.0, column=Column([1.0, 2.0], constrs[1:3]))
- addTerms(coeffs, constrs)#
Add new terms into a column.
- Parameters:
coeffs – Coefficients for added constraints; either a list of coefficients or a single coefficient. The two arguments must have the same size.
constrs – Constraints to add to column; either a list of constraints or a single constraint. The two arguments must have the same size.
- Example:
col.addTerms(1.0, x) col.addTerms([2.0, 3.0], [y, z])
- clear()#
Remove all terms from a column.
- Example:
col.clear()
- copy()#
Copy a column.
- Returns:
Copy of input column.
- Example:
col0 = Column(1.0, c0) col1 = col0.copy()
- getCoeff(i)#
Retrieve the coefficient from a single term in the column.
- Returns:
Coefficient for the term at index
i
in the column.- Example:
col = Column([1.0, 2.0], [c0, c1]) print(col.getCoeff(1))
- getConstr(i)#
Retrieve the constraint object from a single term in the column.
- Returns:
Constraint for the term at index
i
in the column.- Example:
col = Column([1.0, 2.0], [c0, c1]) print(col.getConstr(1))
- remove(item)#
Remove a term from a column.
- Parameters:
item – If
item
is an integer, then the term stored at indexitem
of the column is removed. Ifitem
is a Constr, then all terms that involveitem
are removed.- Example:
col = Column([1.0, 2.0], [c0, c1]) col.remove(c0)
- size()#
Retrieve the number of terms in the column.
- Returns:
Number of terms in the column.
- Example:
print(Column([1.0, 2.0], [c0, c1]).size())