gurobipy.TempConstr#

class TempConstr#

Gurobi temporary constraint object. Objects of this class are created as intermediate results when building constraints using overloaded operators. There are no member functions on this class. Instead, TempConstr objects are created by a set of functions on Var, MVar, LinExpr, QuadExpr, MLinExpr, MQuadExpr, and GenExpr objects (e.g., ==, <=, and >=). You will generally never store objects of this class in your own variables.

The TempConstr object allows you to create several different types of constraints:

  • Linear Constraint: an expression of the form Expr1 sense Expr2, where Expr1 and Expr2 are LinExpr objects, Var objects, or constants, and sense is one of ==, <= or >=. For example, x + y <= 1 + z is a linear constraint, as is x + y == 5. Note that Expr1 and Expr2 can’t both be constants.

  • Ranged Linear Constraint: an expression of the form LinExpr == [Const1, Const2], where Const1 and Const2 are constants and LinExpr is a LinExpr object. For example, x + y == [1, 2] is a ranged linear constraint.

  • Quadratic Constraint: an expression of the form Expr1 sense Expr2, where Expr1 and Expr2 are QuadExpr objects, LinExpr objects, Var objects, or constants, and sense is one of ==, <= or >=. For example, x*x + y*y <= 3 is a quadratic constraint, as is x*x + y*y <= z*z. Note that one of Expr1 or Expr2 must be a QuadExpr (otherwise, the constraint would be linear).

  • Linear Matrix Constraint: an expression of the form Expr1 sense Expr2, where one or both of Expr1 and Expr2 are MLinExpr objects and sense is one of ==, <= or >=. For example, A @ x <= 1 is a linear matrix constraint, as is A @ x == B @ y.

  • Quadratic Matrix Constraint: an expression of the form Expr1 sense Expr2, where one or both of Expr1 and Expr2 are MQuadExpr objects and sense is one of ==, <= or >=. For example, x @ Q @ y <= 3 is a quadratic constraint, as is x @ Q @ x <= y @ A @ y.

  • Absolute Value Constraint: an expression of the form x == abs_(y), where x and y must be Var objects.

  • Logical Constraint: an expression of the form x == op_(y), where x is a binary Var object, and y is a binary Var, a list of binary Var, or a tupledict of binary Var, and op_ is either and_ or or_ (or the Python-specific variants, all_ and any_).

  • Min or Max Constraint: an expression of the form x == op_(y), where x is a Var object, and y is a Var, a list of Var and constants, or a tupledict of Var, and op_ is one of min_ or max_.

  • Indicator Constraint: an expression of the form (x == b) >> (Expr1 sense Expr2), where x is a binary Var object, b is either 0 or 1; Expr1 and Expr2 are LinExpr objects, Var objects, or constants, and sense is one of ==, <= or >=. Parenthesizing both expressions is required. For example, (x == 1) >> (y + w <= 5) is an indicator constraint, indicating that whenever the binary variable x takes the value 1 then the linear constraint y + w <= 5 must hold.

Consider the following examples:

model.addConstr(x + y == 1);
model.addConstr(x + y == [1, 2]);
model.addConstr(x*x + y*y <= 1);
model.addConstr(A @ x <= 1);
model.addConstr(x @ A @ x <= 1);
model.addConstr(x == abs_(y));
model.addConstr(x == or_(y, z));
model.addConstr(x == max_(y, z));
model.addConstr((x == 1) >> (y + z <= 5));

In each case, the overloaded comparison operator creates an object of type TempConstr, which is then immediately passed to method Model.addConstr.