Concepts
Features
Reference
Gurobi variable object. Variables are always associated with aparticular model. You create a variable object by adding a variable to amodel (usingGRBModel.addVar), rather than by using aGRBVar constructor.
The methods on variable objects are used to get and set variableattributes. For example, solution information can be queried by callingget (GRB.DoubleAttr.X). Note, however,that it is generally more efficient to query attributes for a set ofvariables at once. This is done using the attribute query method on theGRBModel object (GRBModel.get).
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.
Query the value of an attribute.
attr – The attribute being queried.
The current value of the requested attribute.
// Get VType valuecharvtype=var.get(GRB.CharAttr.VType);// Get lower bound valuedoublelb=var.get(GRB.DoubleAttr.LB);// Get VBasis valueintvbasis=var.get(GRB.IntAttr.VBasis);// Get variable nameStringname=var.get(GRB.StringAttr.VarName);
This method 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
// Get variable indexintindex=var.index();
Check whether two variable objects refer to the same variable.
otherVar – The other variable.
Boolean result indicates whether the two variable objects referto the same model variable.
// Compare to a second variablebooleanisSame=var.sameAs(var2);
Set the value of an attribute.
attr – The attribute being modified.
newval – The desired new value of the attribute.
// Set VType valuevar.set(GRB.CharAttr.VType,'B');// Set lower bound valuevar.set(GRB.DoubleAttr.LB,0.1);// Set VBasis valuevar.set(GRB.IntAttr.VBasis,-1);// Set variable namevar.set(GRB.StringAttr.VarName,"newName");
Help and Feedback