C++ Reference: class LinearConstraintBuilder

Note: This documentation is automatically generated.

Allow to build a LinearConstraint while making sure there is no duplicate variables. Note that we do not simplify literal/variable that are currently fixed here.

All the functions manipulate a linear expression with an offset. The final constraint bounds will include this offset.

TODO(user): Rename to LinearExpressionBuilder?
Method
AddConstant

Return type: void

Arguments: IntegerValue value

Adds the corresponding term to the current linear expression.

AddDecomposedProduct

Return type: ABSL_MUST_USE_RESULT bool

Arguments: const std::vector<LiteralValueValue>& product

Add the corresponding decomposed products (obtained from TryToDecomposeProduct). The code assumes all literals to be in an exactly_one relation. It returns false if one literal does not have an integer view, as it actually calls AddLiteralTerm().

AddLinearExpression

Return type: void

Arguments: const LinearExpression& expr

AddLinearExpression

Return type: void

Arguments: const LinearExpression& expr, IntegerValue coeff

AddLiteralTerm

Return type: ABSL_MUST_USE_RESULT bool

Arguments: Literal lit, IntegerValue coeff

Add literal * coeff to the constaint. Returns false and do nothing if the given literal didn't have an integer view.

AddQuadraticLowerBound

Return type: void

Arguments: AffineExpression left, AffineExpression right, IntegerTrail* integer_trail, bool* is_quadratic = nullptr

Add an under linearization of the product of two affine expressions. If at least one of them is fixed, then we add the exact product (which is linear). Otherwise, we use McCormick relaxation: left * right = (left_min + delta_left) * (right_min + delta_right) = left_min * right_min + delta_left * right_min + delta_right * left_min + delta_left * delta_right which is >= (by ignoring the quatratic term) right_min * left + left_min * right - right_min * left_min TODO(user): We could use (max - delta) instead of (min + delta) for each expression instead. This would depend on the LP value of the left and right.

AddTerm

Return type: void

Arguments: IntegerVariable var, IntegerValue coeff

AddTerm

Return type: void

Arguments: AffineExpression expr, IntegerValue coeff

Build

Return type: LinearConstraint

Builds and returns the corresponding constraint in a canonical form. All the IntegerVariable will be positive and appear in increasing index order. The bounds can be changed here or taken at construction. TODO(user): this doesn't invalidate the builder object, but if one wants to do a lot of dynamic editing to the constraint, then then underlying algorithm needs to be optimized for that.

BuildConstraint

Return type: LinearConstraint

Arguments: IntegerValue lb, IntegerValue ub

BuildExpression

Return type: LinearExpression

Returns the linear expression part of the constraint only, without the bounds.

Clear

Return type: void

Clears all added terms and constants. Keeps the original bounds.

LinearConstraintBuilder

Return type: explicit

Arguments: const Model* model

We support "sticky" kMinIntegerValue for lb and kMaxIntegerValue for ub for one-sided constraints. Assumes that the 'model' has IntegerEncoder. The bounds can either be specified at construction or during the Build() call.

LinearConstraintBuilder

Arguments: const Model* model, IntegerValue lb, IntegerValue ub

LinearConstraintBuilder

Warning: this version without encoder cannot be used to add literals, so one shouldn't call AddLiteralTerm() on it. All other functions works. TODO(user): Have a subclass so we can enforce than caller using AddLiteralTerm() must construct the Builder with an encoder.