C++ Reference

C++ Reference: CP-SAT

Detailed Description

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;
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64 > coeffs)
Constructs the scalar product of variables and coefficients.
LinearExpr & AddConstant(int64 value)
Adds a constant value to the linear expression.
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64 > coeffs)
Constructs the scalar product of Booleans and coefficients.
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.

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

Definition at line 248 of file cp_model.h.

Public Member Functions

 LinearExpr ()
 
 LinearExpr (BoolVar var)
 Constructs a linear expression from a Boolean variable. More...
 
 LinearExpr (IntVar var)
 Constructs a linear expression from an integer variable. More...
 
 LinearExpr (int64 constant)
 Constructs a constant linear expression. More...
 
LinearExprAddConstant (int64 value)
 Adds a constant value to the linear expression. More...
 
void AddVar (IntVar var)
 Adds a single integer variable to the linear expression. More...
 
void AddTerm (IntVar var, int64 coeff)
 Adds a term (var * coeff) to the linear expression. More...
 
const std::vector< IntVar > & variables () const
 Returns the vector of variables. More...
 
const std::vector< int64 > & coefficients () const
 Returns the vector of coefficients. More...
 
int64 constant () const
 Returns the constant term. More...
 

Static Public Member Functions

static LinearExpr Sum (absl::Span< const IntVar > vars)
 Constructs the sum of a list of variables. More...
 
static LinearExpr ScalProd (absl::Span< const IntVar > vars, absl::Span< const int64 > coeffs)
 Constructs the scalar product of variables and coefficients. More...
 
static LinearExpr BooleanSum (absl::Span< const BoolVar > vars)
 Constructs the sum of a list of Booleans. More...
 
static LinearExpr BooleanScalProd (absl::Span< const BoolVar > vars, absl::Span< const int64 > coeffs)
 Constructs the scalar product of Booleans and coefficients. More...
 
static LinearExpr Term (IntVar var, int64 coefficient)
 Construncts var * coefficient. More...
 

Constructor & Destructor Documentation

◆ LinearExpr() [1/4]

◆ LinearExpr() [2/4]

LinearExpr ( BoolVar  var)

Constructs a linear expression from a Boolean variable.

It deals with logical negation correctly.

◆ LinearExpr() [3/4]

LinearExpr ( IntVar  var)

Constructs a linear expression from an integer variable.

◆ LinearExpr() [4/4]

LinearExpr ( int64  constant)

Constructs a constant linear expression.

Member Function Documentation

◆ AddConstant()

LinearExpr& AddConstant ( int64  value)

Adds a constant value to the linear expression.

◆ AddTerm()

void AddTerm ( IntVar  var,
int64  coeff 
)

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

◆ AddVar()

void AddVar ( IntVar  var)

Adds a single integer variable to the linear expression.

◆ BooleanScalProd()

static LinearExpr BooleanScalProd ( absl::Span< const BoolVar vars,
absl::Span< const int64 >  coeffs 
)
static

Constructs the scalar product of Booleans and coefficients.

◆ BooleanSum()

static LinearExpr BooleanSum ( absl::Span< const BoolVar vars)
static

Constructs the sum of a list of Booleans.

◆ coefficients()

const std::vector<int64>& coefficients ( ) const
inline

Returns the vector of coefficients.

Definition at line 294 of file cp_model.h.

◆ constant()

int64 constant ( ) const
inline

Returns the constant term.

Definition at line 297 of file cp_model.h.

◆ ScalProd()

static LinearExpr ScalProd ( absl::Span< const IntVar vars,
absl::Span< const int64 >  coeffs 
)
static

Constructs the scalar product of variables and coefficients.

◆ Sum()

static LinearExpr Sum ( absl::Span< const IntVar vars)
static

Constructs the sum of a list of variables.

◆ Term()

static LinearExpr Term ( IntVar  var,
int64  coefficient 
)
static

Construncts var * coefficient.

◆ variables()

const std::vector<IntVar>& variables ( ) const
inline

Returns the vector of variables.

Definition at line 291 of file cp_model.h.


The documentation for this class was generated from the following file: