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, LinExpr, QuadExpr, 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, 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).

  • 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(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.

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search