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.
// Add continuous variable in [0,1] called "var" to modelGRBVarvar=model.AddVar(0.0,1.0,0.0,GRB.CONTINUOUS,"var");
' Add continuous variable in [0,1] called "var" to modelDimvarAsGRBVar=model.AddVar(0.0,1.0,0.0,GRB.CONTINUOUS,"var")
The methods on variable objects are used to get and set variableattributes. For example, solution information can be queried by callingGet (GRB.DoubleAttr.X).It can also be queried more directly usingvar.RHS wherevaris a GRBVar object. 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);
' Get VType valueDimvtypeAsChar=var.Get(GRB.CharAttr.VType)' Get lower bound valueDimlbAsDouble=var.Get(GRB.DoubleAttr.LB)' Get VBasis valueDimvbasisAsInteger=var.Get(GRB.IntAttr.VBasis)' Get variable nameDimnameAsString=var.Get(GRB.StringAttr.VarName)
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
intindex=var.Index;
DimindexAsInteger=var.Index
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.
// Compare to a second variableboolissame=var.SameAs(var2);
' Compare to a second variableDimissameAsBoolean=var.SameAs(var2)
Set the value of an attribute.
attr – The attribute being modified.
newvalue – The desired new value of the attribute.
// Set VType valuevar.Set(GRB.CharAttr.VType,GRB.BINARY);// 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");
' Set VType valuevar.Set(GRB.CharAttr.VType,GRB.BINARY)' 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