Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::sat::LinearExpr Class Reference

#include <cp_model.h>

Public Member Functions

 LinearExpr ()=default
 Creates an empty linear expression with value zero.
 
 LinearExpr (BoolVar var)
 NOLINTBEGIN(google-explicit-constructor)
 
 LinearExpr (IntVar var)
 Constructs a linear expression from an integer variable.
 
 LinearExpr (int64_t constant)
 Constructs a constant linear expression.
 
LinearExproperator+= (const LinearExpr &other)
 Operators.
 
LinearExproperator-= (const LinearExpr &other)
 
LinearExproperator*= (int64_t factor)
 
const std::vector< int > & variables () const
 Returns the vector of variable indices.
 
const std::vector< int64_t > & coefficients () const
 Returns the vector of coefficients.
 
bool IsConstant () const
 Returns true if the expression has no variables.
 
int64_t constant () const
 Returns the constant term.
 
std::string DebugString (const CpModelProto *proto=nullptr) const
 

Static Public Member Functions

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

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;
IntegerValue y
IntVar WithName(absl::string_view name)
Sets the name of the variable.
Definition cp_model.cc:119
static LinearExpr Sum(absl::Span< const IntVar > vars)
NOLINTEND(google-explicit-constructor)
Definition cp_model.cc:207
LinearExpr()=default
Creates an empty linear expression with value zero.
GRBmodel * model
BoolVar Not(BoolVar x)
Definition cp_model.cc:87
const Variable x
Definition qp_tests.cc:127

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

cp_model.AddGreaterThan(x, 5);
cp_model.AddEquality(x, y + 5);
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition cp_model.cc:855
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition cp_model.cc:825

Definition at line 246 of file cp_model.h.

Constructor & Destructor Documentation

◆ LinearExpr() [1/4]

operations_research::sat::LinearExpr::LinearExpr ( )
default

Creates an empty linear expression with value zero.

◆ LinearExpr() [2/4]

operations_research::sat::LinearExpr::LinearExpr ( BoolVar var)

NOLINTBEGIN(google-explicit-constructor)

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

We add 1 - var instead.

Definition at line 176 of file cp_model.cc.

◆ LinearExpr() [3/4]

operations_research::sat::LinearExpr::LinearExpr ( IntVar var)

Constructs a linear expression from an integer variable.

Definition at line 190 of file cp_model.cc.

◆ LinearExpr() [4/4]

operations_research::sat::LinearExpr::LinearExpr ( int64_t constant)

Constructs a constant linear expression.

Definition at line 196 of file cp_model.cc.

Member Function Documentation

◆ coefficients()

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

Returns the vector of coefficients.

Definition at line 297 of file cp_model.h.

◆ constant()

int64_t operations_research::sat::LinearExpr::constant ( ) const
inline

Returns the constant term.

Definition at line 303 of file cp_model.h.

◆ DebugString()

std::string operations_research::sat::LinearExpr::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 280 of file cp_model.cc.

◆ FromProto()

LinearExpr operations_research::sat::LinearExpr::FromProto ( const LinearExpressionProto & proto)
static

Constructs a linear expr from its proto representation.

Definition at line 198 of file cp_model.cc.

◆ IsConstant()

bool operations_research::sat::LinearExpr::IsConstant ( ) const
inline

Returns true if the expression has no variables.

Definition at line 300 of file cp_model.h.

◆ operator*=()

LinearExpr & operations_research::sat::LinearExpr::operator*= ( int64_t factor)

Definition at line 274 of file cp_model.cc.

◆ operator+=()

LinearExpr & operations_research::sat::LinearExpr::operator+= ( const LinearExpr & other)

Operators.

Definition at line 255 of file cp_model.cc.

◆ operator-=()

LinearExpr & operations_research::sat::LinearExpr::operator-= ( const LinearExpr & other)

Definition at line 264 of file cp_model.cc.

◆ Sum() [1/2]

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

Constructs the sum of a list of Boolean variables.

Definition at line 215 of file cp_model.cc.

◆ Sum() [2/2]

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

NOLINTEND(google-explicit-constructor)

Constructs the sum of a list of variables.

Definition at line 207 of file cp_model.cc.

◆ Term() [1/2]

LinearExpr operations_research::sat::LinearExpr::Term ( BoolVar var,
int64_t coefficient )
static

Constructs bool * coefficient.

Definition at line 249 of file cp_model.cc.

◆ Term() [2/2]

LinearExpr operations_research::sat::LinearExpr::Term ( IntVar var,
int64_t coefficient )
static

Constructs var * coefficient.

Definition at line 243 of file cp_model.cc.

◆ variables()

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

Returns the vector of variable indices.

Definition at line 294 of file cp_model.h.

◆ WeightedSum() [1/2]

LinearExpr operations_research::sat::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 233 of file cp_model.cc.

◆ WeightedSum() [2/2]

LinearExpr operations_research::sat::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 223 of file cp_model.cc.


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