OR-Tools  8.2
LinearExpr

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;
A Boolean variable.
Definition: cp_model.h:67
Wrapper class around the cp_model proto.
Definition: cp_model.h:599
An integer variable.
Definition: cp_model.h:146
A dedicated container for linear expressions.
Definition: cp_model.h:248
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64 > coeffs)
Constructs the scalar product of variables and coefficients.
Definition: cp_model.cc:151
LinearExpr & AddConstant(int64 value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:185
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64 > coeffs)
Constructs the scalar product of Booleans and coefficients.
Definition: cp_model.cc:175
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:143
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
Definition: cp_model.cc:167
GRBmodel * model
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.
Definition: cp_model.cc:63

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
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition: cp_model.cc:486
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition: cp_model.cc:456

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]

Definition at line 135 of file cp_model.cc.

◆ LinearExpr() [2/4]

LinearExpr ( BoolVar  var)

Constructs a linear expression from a Boolean variable.

It deals with logical negation correctly.

Definition at line 137 of file cp_model.cc.

◆ LinearExpr() [3/4]

LinearExpr ( IntVar  var)

Constructs a linear expression from an integer variable.

Definition at line 139 of file cp_model.cc.

◆ LinearExpr() [4/4]

LinearExpr ( int64  constant)

Constructs a constant linear expression.

Definition at line 141 of file cp_model.cc.

Member Function Documentation

◆ AddConstant()

LinearExpr & AddConstant ( int64  value)

Adds a constant value to the linear expression.

Definition at line 185 of file cp_model.cc.

◆ AddTerm()

void AddTerm ( IntVar  var,
int64  coeff 
)

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

Definition at line 192 of file cp_model.cc.

◆ AddVar()

void AddVar ( IntVar  var)

Adds a single integer variable to the linear expression.

Definition at line 190 of file cp_model.cc.

◆ BooleanScalProd()

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

Constructs the scalar product of Booleans and coefficients.

Definition at line 175 of file cp_model.cc.

◆ BooleanSum()

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

Constructs the sum of a list of Booleans.

Definition at line 167 of file cp_model.cc.

◆ 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()

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

Constructs the scalar product of variables and coefficients.

Definition at line 151 of file cp_model.cc.

◆ Sum()

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

Constructs the sum of a list of variables.

Definition at line 143 of file cp_model.cc.

◆ Term()

LinearExpr Term ( IntVar  var,
int64  coefficient 
)
static

Construncts var * coefficient.

Definition at line 161 of file cp_model.cc.

◆ 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 files: