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 onVar
,MVar
,LinExpr
,QuadExpr
,MLinExpr
,MQuadExpr
, andGenExpr
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
, whereExpr1
andExpr2
areLinExpr
objects,Var
objects, or constants, andsense
is one of==
,<=
or>=
. For example,x + y <= 1 + z
is a linear constraint, as isx + y == 5
. Note thatExpr1
andExpr2
can’t both be constants.Ranged Linear Constraint: an expression of the form
LinExpr == [Const1, Const2]
, whereConst1
andConst2
are constants andLinExpr
is aLinExpr
object. For example,x + y == [1, 2]
is a ranged linear constraint.Quadratic Constraint: an expression of the form
Expr1 sense Expr2
, whereExpr1
andExpr2
areQuadExpr
objects,LinExpr
objects,Var
objects, or constants, andsense
is one of==
,<=
or>=
. For example,x*x + y*y <= 3
is a quadratic constraint, as isx*x + y*y <= z*z
. Note that one ofExpr1
orExpr2
must be aQuadExpr
(otherwise, the constraint would be linear).Linear Matrix Constraint: an expression of the form
Expr1 sense Expr2
, where one or both ofExpr1
andExpr2
areMLinExpr
objects andsense
is one of==
,<=
or>=
. For example,A @ x <= 1
is a linear matrix constraint, as isA @ x == B @ y
.Quadratic Matrix Constraint: an expression of the form
Expr1 sense Expr2
, where one or both ofExpr1
andExpr2
areMQuadExpr
objects andsense
is one of==
,<=
or>=
. For example,x @ Q @ y <= 3
is a quadratic constraint, as isx @ Q @ x <= y @ A @ y
.Absolute Value Constraint: an expression of the form
x == abs_(y)
, wherex
andy
must beVar
objects.Logical Constraint: an expression of the form
x == op_(y)
, wherex
is a binaryVar
object, andy
is a binaryVar
, a list of binaryVar
, or atupledict
of binaryVar
, andop_
is eitherand_
oror_
.Min or Max Constraint: an expression of the form
x == op_(y)
, wherex
is aVar
object, andy
is aVar
, a list ofVar
and constants, or atupledict
ofVar
, andop_
is one ofmin_
ormax_
.Indicator Constraint: an expression of the form
(x == b) >> (Expr1 sense Expr2)
, wherex
is a binaryVar
object,b
is either0
or1
;Expr1
andExpr2
areLinExpr
objects,Var
objects, or constants, andsense
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 variablex
takes the value1
then the linear constrainty + 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 methodModel.addConstr
.