C++ Reference

C++ Reference: CP-SAT

Detailed Description

A dedicated container for linear expressions with double coefficients.

This is currently only usable to define a floating point objective.

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");
DoubleLinearExpr e1(x); // e1 = x.
// e2 = x + y + 5
DoubleLinearExpr e2 = DoubleLinearExpr::Sum({x, y}).AddConstant(5.0);
// e3 = 2 * x - y
DoubleLinearExpr e4(b); // e4 = b.
DoubleLinearExpr 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)};
DoubleLinearExpr e6 = DoubleLinearExpr::Sum(bools); // e6 = b + 1 - c;
// e7 = -3.0 * b + 1.5 - 1.5 * c;
static DoubleLinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
Constructs the scalar product of variables and coefficients.
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
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 in the objective definition.

// Minimize 3.4 * y + 5.2
cp_model.Minimize(DoubleLinearExpr::Term(y, 3.4).AddConstant(5.2));

Definition at line 345 of file cp_model.h.

Public Member Functions

 DoubleLinearExpr ()
 
 DoubleLinearExpr (BoolVar var)
 Constructs a linear expression from a Boolean variable. More...
 
 DoubleLinearExpr (IntVar var)
 Constructs a linear expression from an integer variable. More...
 
 DoubleLinearExpr (double constant)
 Constructs a constant linear expression. More...
 
DoubleLinearExproperator+= (double value)
 Adds a constant value to the linear expression. More...
 
DoubleLinearExproperator+= (IntVar var)
 Adds a single integer variable to the linear expression. More...
 
DoubleLinearExproperator+= (BoolVar var)
 
DoubleLinearExproperator+= (const DoubleLinearExpr &expr)
 Adds another linear expression to the linear expression. More...
 
DoubleLinearExprAddTerm (IntVar var, double coeff)
 Adds a term (var * coeff) to the linear expression. More...
 
DoubleLinearExprAddTerm (BoolVar var, double coeff)
 
DoubleLinearExprAddExpression (const LinearExpr &exprs, double coeff=1.0)
 Adds a linear expression to the double linear expression. More...
 
DoubleLinearExproperator-= (double value)
 Adds a constant value to the linear expression. More...
 
DoubleLinearExproperator-= (IntVar var)
 Adds a single integer variable to the linear expression. More...
 
DoubleLinearExproperator-= (const DoubleLinearExpr &expr)
 Adds another linear expression to the linear expression. More...
 
DoubleLinearExproperator*= (double coeff)
 Multiply the linear expression by a constant. More...
 
const std::vector< int > & variables () const
 Returns the vector of variable indices. More...
 
const std::vector< double > & coefficients () const
 Returns the vector of coefficients. More...
 
const bool IsConstant () const
 
double constant () const
 Returns the constant term. More...
 
std::string DebugString (const CpModelProto *proto=nullptr) const
 Debug string. See the documentation for LinearExpr::DebugString(). More...
 

Static Public Member Functions

static DoubleLinearExpr Sum (absl::Span< const IntVar > vars)
 Constructs the sum of a list of variables. More...
 
static DoubleLinearExpr Sum (absl::Span< const BoolVar > vars)
 Constructs the sum of a list of Boolean variables. More...
 
static DoubleLinearExpr WeightedSum (absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
 Constructs the scalar product of variables and coefficients. More...
 
static DoubleLinearExpr WeightedSum (absl::Span< const BoolVar > vars, absl::Span< const double > coeffs)
 Constructs the scalar product of Boolean variables and coefficients. More...
 

Constructor & Destructor Documentation

◆ DoubleLinearExpr() [1/4]

◆ DoubleLinearExpr() [2/4]

DoubleLinearExpr ( BoolVar  var)
explicit

Constructs a linear expression from a Boolean variable.

It deals with logical negation correctly.

◆ DoubleLinearExpr() [3/4]

DoubleLinearExpr ( IntVar  var)
explicit

Constructs a linear expression from an integer variable.

◆ DoubleLinearExpr() [4/4]

DoubleLinearExpr ( double  constant)
explicit

Constructs a constant linear expression.

Member Function Documentation

◆ AddExpression()

DoubleLinearExpr& AddExpression ( const LinearExpr exprs,
double  coeff = 1.0 
)

Adds a linear expression to the double linear expression.

◆ AddTerm() [1/2]

DoubleLinearExpr& AddTerm ( BoolVar  var,
double  coeff 
)

◆ AddTerm() [2/2]

DoubleLinearExpr& AddTerm ( IntVar  var,
double  coeff 
)

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

◆ coefficients()

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

Returns the vector of coefficients.

Definition at line 406 of file cp_model.h.

◆ constant()

double constant ( ) const
inline

Returns the constant term.

Definition at line 412 of file cp_model.h.

◆ DebugString()

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

Debug string. See the documentation for LinearExpr::DebugString().

◆ IsConstant()

const bool IsConstant ( ) const
inline

Definition at line 409 of file cp_model.h.

◆ operator*=()

DoubleLinearExpr& operator*= ( double  coeff)

Multiply the linear expression by a constant.

◆ operator+=() [1/4]

DoubleLinearExpr& operator+= ( BoolVar  var)

◆ operator+=() [2/4]

DoubleLinearExpr& operator+= ( const DoubleLinearExpr expr)

Adds another linear expression to the linear expression.

◆ operator+=() [3/4]

DoubleLinearExpr& operator+= ( double  value)

Adds a constant value to the linear expression.

◆ operator+=() [4/4]

DoubleLinearExpr& operator+= ( IntVar  var)

Adds a single integer variable to the linear expression.

◆ operator-=() [1/3]

DoubleLinearExpr& operator-= ( const DoubleLinearExpr expr)

Adds another linear expression to the linear expression.

◆ operator-=() [2/3]

DoubleLinearExpr& operator-= ( double  value)

Adds a constant value to the linear expression.

◆ operator-=() [3/3]

DoubleLinearExpr& operator-= ( IntVar  var)

Adds a single integer variable to the linear expression.

◆ Sum() [1/2]

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

Constructs the sum of a list of Boolean variables.

◆ Sum() [2/2]

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

Constructs the sum of a list of variables.

◆ variables()

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

Returns the vector of variable indices.

Definition at line 403 of file cp_model.h.

◆ WeightedSum() [1/2]

static DoubleLinearExpr WeightedSum ( absl::Span< const BoolVar vars,
absl::Span< const double >  coeffs 
)
static

Constructs the scalar product of Boolean variables and coefficients.

◆ WeightedSum() [2/2]

static DoubleLinearExpr WeightedSum ( absl::Span< const IntVar vars,
absl::Span< const double >  coeffs 
)
static

Constructs the scalar product of variables and coefficients.


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