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,
TempConstrobjects are created by a set of functions onVar,MVar,LinExpr,QuadExpr,MLinExpr,MQuadExpr, andGenExprobjects (e.g.,==,<=, and>=). You will generally never store objects of this class in your own variables.The
TempConstrobject allows you to create several different types of constraints:Linear Constraint: an expression of the form
Expr1 sense Expr2, whereExpr1andExpr2areLinExprobjects,Varobjects, or constants, andsenseis one of==,<=or>=. For example,x + y <= 1 + zis a linear constraint, as isx + y == 5. Note thatExpr1andExpr2can’t both be constants.Ranged Linear Constraint: an expression of the form
LinExpr == [Const1, Const2], whereConst1andConst2are constants andLinExpris aLinExprobject. For example,x + y == [1, 2]is a ranged linear constraint.Quadratic Constraint: an expression of the form
Expr1 sense Expr2, whereExpr1andExpr2areQuadExprobjects,LinExprobjects,Varobjects, or constants, andsenseis one of==,<=or>=. For example,x*x + y*y <= 3is a quadratic constraint, as isx*x + y*y <= z*z. Note that one ofExpr1orExpr2must be aQuadExpr(otherwise, the constraint would be linear).Linear Matrix Constraint: an expression of the form
Expr1 sense Expr2, where one or both ofExpr1andExpr2areMLinExprobjects andsenseis one of==,<=or>=. For example,A @ x <= 1is a linear matrix constraint, as isA @ x == B @ y.Quadratic Matrix Constraint: an expression of the form
Expr1 sense Expr2, where one or both ofExpr1andExpr2areMQuadExprobjects andsenseis one of==,<=or>=. For example,x @ Q @ y <= 3is a quadratic constraint, as isx @ Q @ x <= y @ A @ y.Absolute Value Constraint: an expression of the form
x == abs_(y), wherexandymust beVarobjects.Logical Constraint: an expression of the form
x == op_(y), wherexis a binaryVarobject, andyis a binaryVar, a list of binaryVar, or atupledictof binaryVar, andop_is eitherand_oror_.Min or Max Constraint: an expression of the form
x == op_(y), wherexis aVarobject, andyis aVar, a list ofVarand constants, or atupledictofVar, andop_is one ofmin_ormax_.Indicator Constraint: an expression of the form
(x == b) >> (Expr1 sense Expr2), wherexis a binaryVarobject,bis either0or1;Expr1andExpr2areLinExprobjects,Varobjects, or constants, andsenseis one of==,<=or>=. Parenthesizing both expressions is required. For example,(x == 1) >> (y + w <= 5)is an indicator constraint, indicating that whenever the binary variablextakes the value1then the linear constrainty + w <= 5must 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 methodModel.addConstr.