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

# GRBGenConstr

GRBGenConstr

  Gurobi general constraint object. General constraints are always associated with a particular model. You create a general constraint object by adding a general constraint to a model (using one of the `GRBModel.AddGenConstr*` methods), rather than by using a `GRBGenConstr` constructor.

  Example:

        // Create binary indicator variable
        GRBVar z = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "resvar");
        // 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 constraint if resvar = 1 then x + y <= 2
        GRBGenConstr gc = model.AddGenConstrIndicator(z, 1, x + y <= 2.0, "indicatorconstr");

        ' Create binary indicator variable
        Dim z As GRBVar = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "resvar")
        ' 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 constraint if resvar = 1 then x + y <= 2
        Dim gc As GRBGenConstr = model.AddGenConstrIndicator(z, 1, x + y <= 2.0, "indicatorconstr")

  General constraint objects have a number of attributes, which can be queried with the `GRBGenConstr.Get` method. For example, the general constraint type can be queried by calling `Get` (`GRB.IntAttr.GenConstrType`). It can also be queried more directly using `genconstr.GenConstrType` where `genconstr` is a `GRBGenConstr` object.

  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.

  double Get(GRB.DoubleAttr attr)
  int Get(GRB.IntAttr attr)
  string Get(GRB.StringAttr attr)

    Query the value of a general constraint attribute.

    Parameters:

      attr – The attribute being queried.

    Returns:

      The current value of the requested attribute.

    Example:

          // Get PWL approximation error
          double funcPieceError = gc.Get(GRB.DoubleAttr.FuncPieceError);
          // Get general constraint type
          int type = gc.Get(GRB.IntAttr.GenConstrType);
          // Get general constraint name
          string name = gc.Get(GRB.StringAttr.GenConstrName);

          ' Get PWL approximation error
          Dime funcPieceError As Double = gc.Get(GRB.DoubleAttr.FuncPieceError)
          ' Get general constraint type
          Dim type As Integer = gc.Get(GRB.IntAttr.GenConstrType)
          ' Get general constraint name
          Dim name As String = gc.Get(GRB.StringAttr.GenConstrName)

  void Set(GRB.DoubleAttr attr, string newvalue)
  void Set(GRB.IntAttr attr, string newvalue)
  void Set(GRB.StringAttr attr, string newvalue)

    Set the value of a general constraint attribute.

    Parameters:

      - attr – The attribute being modified.

      - newvalue – The desired new value of the attribute.

    Example:

          // Set PWL approximation error
          gc.Set(GRB.DoubleAttr.FuncPieceError, 1e-4);
          // Set number of PWL pieces
          gc.Set(GRB.IntAttr.FuncPieces, 100);
          // Set name of general constraint
          gc.Set(GRB.StringAttr.GenConstrName, "newName");

          ' Set PWL approximation error
          gc.Set(GRB.DoubleAttr.FuncPieceError, 1e-4)
          ' Set number of PWL pieces
          gc.Set(GRB.IntAttr.FuncPieces, 100)
          ' Set name of general constraint
          gc.Set(GRB.StringAttr.GenConstrName, "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