C++ Reference: class LinearExpr

This documentation is automatically generated.



A dedicated container for linear expressions.

This class helps building and manipulating linear expressions. With the use of implicit constructors, it can accept integer values, Boolean and Integer variables. Note that Not(x) will be silently transformed into 1 - x when added to the linear expression.

Furthermore, static methods allows sums and scalar products, with or without an additional constant.

Usage: CpModelBuilder cp_model; IntVar x = model.NewIntVar(0, 10, "x"); IntVar y = model.NewIntVar(0, 10, "y"); BoolVar b = model.NewBoolVar().WithName("b"); BoolVar c = model.NewBoolVar().WithName("c"); LinearExpr e1(x); // e1 = x. LinearExpr e2 = LinearExpr::Sum({x, y}).AddConstant(5); // e2 = x + y + 5; LinearExpr e3 = LinearExpr::ScalProd({x, y}, {2, -1}); // e3 = 2 * x - y. LinearExpr e4(b); // e4 = b. LinearExpr e5(b.Not()); // e5 = 1 - b. // If passing a std::vector<BoolVar>, a specialized method must be called. std::vector<BoolVar> bools = {b, Not(c)}; LinearExpr e6 = LinearExpr::BooleanSum(bools); // e6 = b + 1 - c; // e7 = -3 * b + 1 - c; LinearExpr e7 = LinearExpr::BooleanScalProd(bools, {-3, 1}); This can be used implicitly in some of the CpModelBuilder methods. cp_model.AddGreaterThan(x, 5); // x > 5 cp_model.AddEquality(x, LinearExpr(y).AddConstant(5)); // x == y + 5

Method
AddConstant

Return type: LinearExpr&

Arguments: int64 value

Adds a constant value to the linear expression.

AddTerm

Return type: void

Arguments: IntVar var, int64 coeff

Adds a term (var * coeff) to the linear expression.

AddVar

Return type: void

Arguments: IntVar var

Adds a single integer variable to the linear expression.

BooleanScalProd

Return type: static LinearExpr

Arguments: absl::Span<const BoolVar> vars, absl::Span<const int64> coeffs

Constructs the scalar product of Booleans and coefficients.

BooleanSum

Return type: static LinearExpr

Arguments: absl::Span<const BoolVar> vars

Constructs the sum of a list of Booleans.

coefficients

Return type: const std::vector<int64>&

Returns the vector of coefficients.

constant

Return type: int64

Returns the constant term.

LinearExpr

LinearExpr

Arguments: BoolVar var

Constructs a linear expression from a Boolean variable. It deals with logical negation correctly.

LinearExpr

Arguments: IntVar var

Constructs a linear expression from an integer variable.

LinearExpr

Arguments: int64 constant

Constructs a constant linear expression.

ScalProd

Return type: static LinearExpr

Arguments: absl::Span<const IntVar> vars, absl::Span<const int64> coeffs

Constructs the scalar product of variables and coefficients.

Sum

Return type: static LinearExpr

Arguments: absl::Span<const IntVar> vars

Constructs the sum of a list of variables.

Term

Return type: static LinearExpr

Arguments: IntVar var, int64 coefficient

Construncts var * coefficient.

variables

Return type: const std::vector<IntVar>&

Returns the vector of variables.