Concepts
Features
Reference
Gurobi temporary constraint object. Objects of this class are created asintermediate results when building constraints using overloadedoperators. 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>=). Youwill generally never store objects of this class in your own variables.
TheTempConstr object allows you to create several different typesof constraints:
Linear Constraint: an expression of the formExpr1senseExpr2, 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. NotethatExpr1 andExpr2 can’t both be constants.
Ranged Linear Constraint: an expression of the formLinExpr==[Const1,Const2], whereConst1 andConst2 areconstants andLinExpr is aLinExpr object. Forexample,x+y==[1,2] is a ranged linear constraint.
Quadratic Constraint: an expression of the formExpr1senseExpr2, whereExpr1 andExpr2 areQuadExpr objects,LinExpr objects,Var objects, or constants, andsense is one of==,<= or>=. For example,x*x+y*y<=3 is aquadratic constraint, as isx*x+y*y<=z*z. Note that one ofExpr1 orExpr2 must be aQuadExpr (otherwise, theconstraint would be linear).
Linear Matrix Constraint: an expression of the formExpr1senseExpr2, where one or both ofExpr1 andExpr2areMLinExpr objects andsense is one of==,<= or>=. For example,A@x<=1 is a linear matrixconstraint, as isA@x==B@y.
Quadratic Matrix Constraint: an expression of the formExpr1senseExpr2, where one or both ofExpr1 andExpr2areMQuadExpr objects andsense is one of==,<= or>=. For example,x@Q@y<=3 is a quadraticconstraint, as isx@Q@x<=y@A@y.
Absolute Value Constraint: an expression of the formx==abs_(y), wherex andy must beVarobjects.
Logical Constraint: an expression of the formx==op_(y),wherex is a binaryVar object, andy is a binaryVar, a list of binaryVar, or atupledict of binaryVar, andop_ iseitherand_ oror_.
Min or Max Constraint: an expression of the formx==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)>>(Expr1senseExpr2), 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, indicatingthat whenever the binary variablex takes the value1 thenthe 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 oftypeTempConstr, which is then immediately passed to methodModel.addConstr.
Help and Feedback