OR-Tools  9.6
LinearExpr

Detailed Description

A dedicated container for 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. It also support operator overloads to construct the linear expression naturally.

Furthermore, static methods allow to construct a linear expression from sums or scalar products.

Usage:

CpModelBuilder cp_model;
IntVar x = model.NewIntVar({0, 10}).WithName("x");
IntVar y = model.NewIntVar({0, 10}).WithName("y");
BoolVar b = model.NewBoolVar().WithName("b");
BoolVar c = model.NewBoolVar().WithName("c");
LinearExpr e1(x); // Or e1 = x.
LinearExpr e2 = x + y + 5;
LinearExpr e3 = 2 * x - y;
LinearExpr e5 = b.Not(); // 1 - b.
std::vector<BoolVar> bools = {b, Not(c)};
LinearExpr e6 = LinearExpr::Sum(bools); // b + 1 - c;
LinearExpr e7 = -3 * b + Not(c); // -3 * b + 1 - c;
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:206
LinearExpr()=default
Creates an empty linear expression with value zero.
int64_t b
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:86

This can be used implicitly in some of the CpModelBuilder methods.

cp_model.AddGreaterThan(x, 5);
cp_model.AddEquality(x, y + 5);

Definition at line 241 of file cp_model.h.

Public Member Functions

 LinearExpr ()=default
 Creates an empty linear expression with value zero. More...
 
 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_t constant)
 Constructs a constant linear expression. More...
 
LinearExproperator+= (const LinearExpr &other)
 
LinearExproperator-= (const LinearExpr &other)
 
LinearExproperator*= (int64_t factor)
 
const std::vector< int > & variables () const
 Returns the vector of variable indices. More...
 
const std::vector< int64_t > & coefficients () const
 Returns the vector of coefficients. More...
 
const bool IsConstant () const
 Returns true if the expression has no variables. More...
 
int64_t constant () const
 Returns the constant term. More...
 
std::string DebugString (const CpModelProto *proto=nullptr) const
 Debug string. More...
 

Static Public Member Functions

static LinearExpr Sum (absl::Span< const IntVar > vars)
 Constructs the sum of a list of variables. More...
 
static LinearExpr Sum (absl::Span< const BoolVar > vars)
 Constructs the sum of a list of Boolean variables. More...
 
static LinearExpr WeightedSum (absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
 Constructs the scalar product of variables and coefficients. More...
 
static LinearExpr WeightedSum (absl::Span< const BoolVar > vars, absl::Span< const int64_t > coeffs)
 Constructs the scalar product of Boolean variables and coefficients. More...
 
static LinearExpr Term (IntVar var, int64_t coefficient)
 Constructs var * coefficient. More...
 
static LinearExpr Term (BoolVar var, int64_t coefficient)
 Constructs bool * coefficient. More...
 
static LinearExpr FromProto (const LinearExpressionProto &proto)
 Constructs a linear expr from its proto representation. More...
 

Constructor & Destructor Documentation

◆ LinearExpr() [1/4]

LinearExpr ( )
default

Creates an empty linear expression with value zero.

◆ LinearExpr() [2/4]

LinearExpr ( BoolVar  var)

Constructs a linear expression from a Boolean variable.

It deals with logical negation correctly.

Definition at line 175 of file cp_model.cc.

◆ LinearExpr() [3/4]

LinearExpr ( IntVar  var)

Constructs a linear expression from an integer variable.

Definition at line 189 of file cp_model.cc.

◆ LinearExpr() [4/4]

LinearExpr ( int64_t  constant)

Constructs a constant linear expression.

Definition at line 195 of file cp_model.cc.

Member Function Documentation

◆ coefficients()

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

Returns the vector of coefficients.

Definition at line 292 of file cp_model.h.

◆ constant()

int64_t constant ( ) const
inline

Returns the constant term.

Definition at line 298 of file cp_model.h.

◆ DebugString()

std::string DebugString ( const CpModelProto *  proto = nullptr) const

Debug string.

If the CpModelBuilder is passed, the string will include variable names and domains. Otherwise, you will get a shorter string with only variable indices.

Definition at line 279 of file cp_model.cc.

◆ FromProto()

LinearExpr FromProto ( const LinearExpressionProto &  proto)
static

Constructs a linear expr from its proto representation.

Definition at line 197 of file cp_model.cc.

◆ IsConstant()

const bool IsConstant ( ) const
inline

Returns true if the expression has no variables.

Definition at line 295 of file cp_model.h.

◆ operator*=()

LinearExpr & operator*= ( int64_t  factor)

Definition at line 273 of file cp_model.cc.

◆ operator+=()

LinearExpr & operator+= ( const LinearExpr other)

Definition at line 254 of file cp_model.cc.

◆ operator-=()

LinearExpr & operator-= ( const LinearExpr other)

Definition at line 263 of file cp_model.cc.

◆ Sum() [1/2]

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

Constructs the sum of a list of Boolean variables.

Definition at line 214 of file cp_model.cc.

◆ Sum() [2/2]

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

Constructs the sum of a list of variables.

Definition at line 206 of file cp_model.cc.

◆ Term() [1/2]

LinearExpr Term ( BoolVar  var,
int64_t  coefficient 
)
static

Constructs bool * coefficient.

Definition at line 248 of file cp_model.cc.

◆ Term() [2/2]

LinearExpr Term ( IntVar  var,
int64_t  coefficient 
)
static

Constructs var * coefficient.

Definition at line 242 of file cp_model.cc.

◆ variables()

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

Returns the vector of variable indices.

Definition at line 289 of file cp_model.h.

◆ WeightedSum() [1/2]

LinearExpr WeightedSum ( absl::Span< const BoolVar vars,
absl::Span< const int64_t >  coeffs 
)
static

Constructs the scalar product of Boolean variables and coefficients.

Definition at line 232 of file cp_model.cc.

◆ WeightedSum() [2/2]

LinearExpr WeightedSum ( absl::Span< const IntVar vars,
absl::Span< const int64_t >  coeffs 
)
static

Constructs the scalar product of variables and coefficients.

Definition at line 222 of file cp_model.cc.


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