Try our new documentation site (beta).
Filter Content By
Version
Text Search
${sidebar_list_label} - Back
Filter by Language
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 Expr1
, whereExpr1
andExpr2
are LinExpr 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 a LinExpr object. For example,x + y == [1, 2]
is a ranged linear constraint. - Quadratic Constraint: an expression of the form
Expr1 sense Expr2
, whereExpr1
andExpr2
are QuadExpr 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 a QuadExpr (otherwise, the constraint would be linear). - Linear Matrix Constraint: an expression of the form
Expr1 sense Expr1
, where one or both ofExpr1
andExpr2
are MLinExpr 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
are MQuadExpr 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 be Var objects. - Logical Constraint: an expression of the form
x == op_(y)
, wherex
is a binary Var object, andy
is a binary Var, a list of binary Var, or a tupledict of binary Var, andop_
is eitherand_
oror_
(or the Python-specific variants,all_
andany_
). - Min or Max Constraint: an expression of the form
x == op_(y)
, wherex
is a Var object, andy
is a Var, a list of Var and constants, or a tupledict of Var, andop_
is one ofmin_
ormax_
. - Indicator Constraint: an expression of the form
(x == b) >> (Expr1 sense Expr2)
, wherex
is a binary Var object,b
is either0
or1
;Expr1
andExpr2
are LinExpr 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 method
Model.addConstr.