Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# 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`).

  The full list of attributes can be found in the Attributes section of this document. Examples of how to query and set attributes can also be found in this section.

  char get(GRB.CharAttr attr)
  double get(GRB.DoubleAttr attr)
  int get(GRB.IntAttr attr)
  String get(GRB.StringAttr attr)

    Query the value of an attribute.

    Arguments:

      attr – The attribute being queried.

    Return value:

      The current value of the requested attribute.

    Example:

          // Get VType value
          char vtype = var.get(GRB.CharAttr.VType);
          // Get lower bound value
          double lb = var.get(GRB.DoubleAttr.LB);
          // Get VBasis value
          int vbasis = var.get(GRB.IntAttr.VBasis);
          // Get variable name
          String name = var.get(GRB.StringAttr.VarName);

  int index()

    This method 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.

    Return value:

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

    Example:

          // Get variable index
          int index = var.index();

  boolean sameAs(GRBVar otherVar)

    Check whether two variable objects refer to the same variable.

    Arguments:

      otherVar – The other variable.

    Return value:

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

    Example:

          // Compare to a second variable
          boolean isSame = var.sameAs(var2);

  void set(GRB.CharAttr attr, char newval)
  void set(GRB.DoubleAttr attr, double newval)
  void set(GRB.IntAttr attr, int newval)
  void set(GRB.StringAttr attr, String newval)

    Set the value of an attribute.

    Arguments:

      - attr – The attribute being modified.

      - newval – The desired new value of the attribute.

    Example:

          // Set VType value
          var.set(GRB.CharAttr.VType, 'B');
          // Set lower bound value
          var.set(GRB.DoubleAttr.LB, 0.1);
          // Set VBasis value
          var.set(GRB.IntAttr.VBasis, -1);
          // Set variable name
          var.set(GRB.StringAttr.VarName, "newName");

Help and Feedback

Press `/` or click to search this documentation | Gurobi website | Gurobi community forum
Go to other Gurobi documentation | Gurobi download | Contact support