Concepts
Features
Reference
Gurobi variable object. Variables are always associated with aparticular model. You create a variable object by adding a variable to amodel (usingModel.addVar), rather than by using aVarconstructor.
Variable objects have a number of attributes. Somevariable attributes can only be queried, while others can also be set.Recall that the Gurobi Optimizer employs a lazy update approach, sochanges to attributes don’t take effect until the next call toModel.update,Model.optimize, orModel.write on the associated model.
We should point out a few things about variable attributes. Consider thelb attribute. Its value can be queried usingvar.lb. The Gurobilibrary ignores letter case in attribute names, so it can also bequeried asvar.LB. It can be set using a standard assignmentstatement (e.g.,var.lb=0). However, as mentioned earlier,attribute modification is done in a lazy fashion, so you won’t see theeffect of the change immediately. And some attributes can not be set(e.g., thex attribute), so attempts to assign new values to themwill raise an exception.
You can also useVar.getAttr/Var.setAttr to access attributes. The attributename can be passed to these routines as a string, or you can use theconstants defined in theGRB.Attr class (e.g.,GRB.Attr.LB).
The full list of attributescan be found in theAttributes section of thisdocument. Examples of how to query and set attributes can also be foundinthis section.
To build expressions using variable objects, you generally use operatoroverloading. You can build eitherlinear orquadratic expressions:
expr1=x+2*y+3*z+4.0expr2=x**2+2*x*y+3*z+4.0
The first expression is linear, while the second is quadratic. Anexpression is typically then passed tosetObjective (to set the optimizationobjective) oraddConstr (to add aconstraint).
Query the value of a variable attribute.
Raises anAttributeError if the requested attribute doesn’t exist orcan’t be queried. Raises aGurobiError if there is a problem withtheVar object (e.g., it was removed from the model).
attrname – The attribute being queried.
The current value of the requested attribute.
print(var.getAttr(GRB.Attr.X))print(var.getAttr("x"))
Check whether two variable objects refer to the same variable.
var2 – The other variable.
Boolean result indicates whether the two variable objects referto the same model variable.
print(model.getVars()[0].sameAs(model.getVars()[1]))
This property returns the current index, or order, of the variable in theunderlying constraint matrix.
Note that the index of a variable may change after subsequent modelmodifications.
-2: removed, -1: not in model, otherwise: index of the variablein the model
v=model.getVars()[0]print(v.index)# Index will be 0
Set the value of a variable attribute. Note that, due to our lazy update approach,the change won’t actually take effect until you update the model (usingModel.update), optimize the model (usingModel.optimize), or write the model to disk (usingModel.write).
Raises anAttributeError if the specified attribute doesn’t exist orcan’t be set. Raises aGurobiError if there is a problem with theVar object (e.g., it was removed from the model).
attrname – The attribute being modified.
newvalue – The desired new value of the attribute.
var.setAttr(GRB.Attr.UB,0.0)var.setAttr("ub",0.0)
Returns anNLExpr representing this variable. Usingthis property is as advanced technique and is not required in thevast majority of cases. SeeMore control over expression creation for furtherdetails.
Help and Feedback