GRBVar#

GRBVar#

Gurobi variable object. Variables are always associated with a particular model. You create a variable object by adding a variable to a model (using GRBModel.AddVar), rather than by using a GRBVar constructor.

The methods on variable objects are used to get and set variable attributes. For example, solution information can be queried by calling Get (GRB.DoubleAttr.X). Note, however, that it is generally more efficient to query attributes for a set of variables at once. This is done using the attribute query method on the GRBModel object (GRBModel.Get).

Example:
// Add continuous variable in [0,1] called "var" to model
GRBVar var = model.AddVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "var");
char Get(GRB.CharAttr attr)#

Query the value of a char-valued attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

Example:
// Get VType value
char vtype = var.Get(GRB.CharAttr.VType);
double Get(GRB.DoubleAttr attr)#

Query the value of a double-valued attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

Example:
// Get lower bound value
double lb = var.Get(GRB.DoubleAttr.LB);
int Get(GRB.IntAttr attr)#

Query the value of an int-valued attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

Example:
// Get VBasis value
int vbasis = var.Get(GRB.IntAttr.VBasis);
string Get(GRB.StringAttr attr)#

Query the value of a string-valued attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

Example:
// Get variable name
string name = var.Get(GRB.StringAttr.VarName);
int Index#

This property returns the current index, or order, of the variable in the underlying constraint matrix.

Note that the index of a variable may change after subsequent model modifications.

Returns:

-2: removed, -1: not in model, otherwise: index of the variable in the model

Example:
int index = var.Index;
bool SameAs(GRBVar var2)#

Check whether two variable objects refer to the same variable.

Parameters:

var2 – The other variable.

Returns:

Boolean result indicates whether the two variable objects refer to the same model variable.

Example:
// Compare to a second variable
bool issame = var.SameAs(var2);
void Set(GRB.CharAttr attr, char newvalue)#

Set the value of a char-valued attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value of the attribute.

Example:
// Set VType value
var.Set(GRB.CharAttr.VType, GRB.BINARY);
void Set(GRB.DoubleAttr attr, double newvalue)#

Set the value of a double-valued attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value of the attribute.

Example:
// Set lower bound value
var.Set(GRB.DoubleAttr.LB, 0.1);
void Set(GRB.IntAttr attr, int newvalue)#

Set the value of an int-valued attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value of the attribute.

Example:
// Set VBasis value
var.Set(GRB.IntAttr.VBasis, -1);
void Set(GRB.StringAttr attr, string newvalue)#

Set the value of a string-valued attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value of the attribute.

Example:
// Set variable name
var.Set(GRB.StringAttr.VarName, "newName");