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

# gurobipy.Constr

class Constr

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

  Constraint objects have a number of attributes. Some constraint attributes can only be queried, while others can also be set. Recall that the Gurobi Optimizer employs a lazy update approach, so changes to attributes don’t take effect until the next call to `Model.update`, `Model.optimize`, or `Model.write` on the associated model.

  We should point out a few things about constraint attributes. Consider the `rhs` attribute. Its value can be queried using `constr.rhs`. The Gurobi library ignores letter case in attribute names, so it can also be queried as `constr.rhs`. It can be set using a standard assignment statement (e.g., `constr.rhs = 0`). However, as mentioned earlier, attribute modification is done in a lazy fashion, so you won’t see the effect of the change immediately. And some attributes can not be set (e.g., the Pi attribute), so attempts to assign new values to them will raise an exception.

  You can also use `Constr.getAttr`/ `Constr.setAttr` to access attributes. The attribute name can be passed to these routines as a string, or you can use the constants defined in the `GRB.Attr` class (e.g., `GRB.Attr.RHS`).

  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.

  getAttr(attrname)

    Query the value of a constraint attribute.

    Raises an `AttributeError` if the requested attribute doesn’t exist or can’t be queried. Raises a `GurobiError` if there is a problem with the `Constr` object (e.g., it was removed from the model).

    Parameters:

      attrname – The attribute being queried.

    Returns:

      The current value of the requested attribute.

    Example:

          print(constr.getAttr(GRB.Attr.Slack))
          print(constr.getAttr("slack"))

  property 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:

          c = model.getConstrs()[0]
          print(c.index) # Index will be 0

  sameAs(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:

          print(model.getConstrs()[0].sameAs(model.getConstrs()[1]))

  setAttr(attrname, newvalue)

    Set the value of a constraint attribute. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using `Model.update`), optimize the model (using `Model.optimize`), or write the model to disk (using `Model.write`).

    Raises an `AttributeError` if the specified attribute doesn’t exist or can’t be set. Raises a `GurobiError` if there is a problem with the `Constr` object (e.g., it was removed from the model).

    Parameters:

      - attrname – The attribute being modified.

      - newvalue – The desired new value of the attribute.

    Example:

          constr.setAttr(GRB.Attr.RHS, 0.0)
          constr.setAttr("rhs", 0.0)

Help and Feedback

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