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

# GRBConstr

GRBConstr

  Gurobi constraint object. Constraints are always associated with a particular model. You create a constraint object by adding a constraint to a model (using `GRBModel.AddConstr`), rather than by using a `GRBConstr` constructor.

  Example:

        // Create variables
        GRBVar x = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x");
        GRBVar y = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y");
        // Add linear constraint x + y = 0 with name c1
        GRBConstr constr = model.AddConstr(x + y == 0, "c1");

        ' Create variables
        Dim x As GRBVar = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x")
        Dim y As GRBVar = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y")
        ' Add linear constraint x + y = 0 with name c1
        Dim constr As GRBConstr = model.AddConstr(x + y == 0, "c1")

  The methods on constraint objects are used to get and set constraint attributes. For example, constraint right-hand sides can be queried by calling `Get` (`GRB.DoubleAttr.RHS`). It can also be queried more directly using `constr.RHS` where `constr` is a `GRBConstr` object. Note, however, that it is generally more efficient to query attributes for a set of constraints 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.

    Parameters:

      attr – The attribute being queried.

    Returns:

      The current value of the requested attribute.

    Example:

          // Get constraint sense
          char sense = constr.Get(GRB.CharAttr.Sense);
          // Get RHS
          double rhs = constr.Get(GRB.DoubleAttr.RHS);
          // Get CBasis value
          int cbasis = constr.Get(GRB.IntAttr.CBasis);
          // Get constraint name
          string name = constr.Get(GRB.StringAttr.ConstrName);

          ' Get constraint sense
          Dim sense As Char = constr.Get(GRB.CharAttr.Sense)
          ' Get RHS
          Dim rhs As Double = constr.Get(GRB.DoubleAttr.RHS)
          ' Get CBasis value
          Dim cbasis As Integer = constr.Get(GRB.IntAttr.CBasis)
          ' Get constraint name
          Dim name As String = constr.Get(GRB.StringAttr.ConstrName)

  int Index

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

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

    Returns:

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

    Example:

          int index = constr.Index;

          Dim index As Integer = constr.Index

  bool SameAs(GRBConstr constr2)

    Check whether two constraint objects refer to the same constraint.

    Parameters:

      constr2 – The other constraint.

    Returns:

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

    Example:

          // Compare to a second constraint
          bool isSame = constr.SameAs(constr2);

          ' Compare to a second constraint
          Dim isSame As Boolean = constr.SameAs(constr2)

  void Set(GRB.CharAttr attr, char newvalue)
  void Set(GRB.DoubleAttr attr, double newvalue)
  void Set(GRB.IntAttr attr, int newvalue)
  void Set(GRB.StringAttr attr, string newvalue)

    Set the value of an attribute.

    Parameters:

      - attr – The attribute being modified.

      - newvalue – The desired new value of the attribute.

    Example:

          // Set constraint sense
          constr.Set(GRB.CharAttr.Sense, '>');
          // Set RHS
          constr.Set(GRB.DoubleAttr.RHS, 2.0);
          // Set CBasis value
          constr.Set(GRB.IntAttr.CBasis, 0);
          // Set constraint name
          constr.Set(GRB.StringAttr.ConstrName, "newName");

          ' Set constraint sense
          constr.Set(GRB.CharAttr.Sense, ">")
          ' Set RHS
          constr.Set(GRB.DoubleAttr.RHS, 2.0)
          ' Set CBasis value
          constr.Set(GRB.IntAttr.CBasis, 0)
          ' Set constraint name
          constr.Set(GRB.StringAttr.ConstrName, "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