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

#include <cp_model.h>

Public Member Functions

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

Static Public Member Functions

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

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;
IntegerValue y
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition cp_model.cc:331
static DoubleLinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
Constructs the scalar product of variables and coefficients.
Definition cp_model.cc:347
IntVar WithName(absl::string_view name)
Sets the name of the variable.
Definition cp_model.cc:119
int64_t a
Definition table.cc:44
GRBmodel * model
BoolVar Not(BoolVar x)
Definition cp_model.cc:87
const Variable x
Definition qp_tests.cc:127

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));
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition cp_model.cc:1185

Definition at line 350 of file cp_model.h.

Constructor & Destructor Documentation

◆ DoubleLinearExpr() [1/4]

operations_research::sat::DoubleLinearExpr::DoubleLinearExpr ( )
default

◆ DoubleLinearExpr() [2/4]

operations_research::sat::DoubleLinearExpr::DoubleLinearExpr ( BoolVar var)
explicit

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

Definition at line 325 of file cp_model.cc.

◆ DoubleLinearExpr() [3/4]

operations_research::sat::DoubleLinearExpr::DoubleLinearExpr ( IntVar var)
explicit

Constructs a linear expression from an integer variable.

Definition at line 327 of file cp_model.cc.

◆ DoubleLinearExpr() [4/4]

operations_research::sat::DoubleLinearExpr::DoubleLinearExpr ( double constant)
explicit

Constructs a constant linear expression.

Definition at line 329 of file cp_model.cc.

Member Function Documentation

◆ AddExpression()

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

Adds a linear expression to the double linear expression.

Definition at line 410 of file cp_model.cc.

◆ AddTerm() [1/2]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::AddTerm ( BoolVar var,
double coeff )

Definition at line 397 of file cp_model.cc.

◆ AddTerm() [2/2]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::AddTerm ( IntVar var,
double coeff )

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

Definition at line 391 of file cp_model.cc.

◆ coefficients()

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

Returns the vector of coefficients.

Definition at line 411 of file cp_model.h.

◆ constant()

double operations_research::sat::DoubleLinearExpr::constant ( ) const
inline

Returns the constant term.

Definition at line 417 of file cp_model.h.

◆ DebugString()

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

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

Definition at line 450 of file cp_model.cc.

◆ IsConstant()

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

Returns true if the expression has no variable.

Definition at line 414 of file cp_model.h.

◆ operator*=()

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator*= ( double coeff)

Multiply the linear expression by a constant.

Definition at line 442 of file cp_model.cc.

◆ operator+=() [1/4]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator+= ( BoolVar var)

Definition at line 377 of file cp_model.cc.

◆ operator+=() [2/4]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator+= ( const DoubleLinearExpr & expr)

Adds another linear expression to the linear expression.

Definition at line 382 of file cp_model.cc.

◆ operator+=() [3/4]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator+= ( double value)

Adds a constant value to the linear expression.

Definition at line 367 of file cp_model.cc.

◆ operator+=() [4/4]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator+= ( IntVar var)

Adds a single integer variable to the linear expression.

Definition at line 372 of file cp_model.cc.

◆ operator-=() [1/3]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator-= ( const DoubleLinearExpr & expr)

Adds another linear expression to the linear expression.

Definition at line 432 of file cp_model.cc.

◆ operator-=() [2/3]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator-= ( double value)

Adds a constant value to the linear expression.

Definition at line 422 of file cp_model.cc.

◆ operator-=() [3/3]

DoubleLinearExpr & operations_research::sat::DoubleLinearExpr::operator-= ( IntVar var)

Adds a single integer variable to the linear expression.

Definition at line 427 of file cp_model.cc.

◆ Sum() [1/2]

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

Constructs the sum of a list of Boolean variables.

Definition at line 339 of file cp_model.cc.

◆ Sum() [2/2]

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

Constructs the sum of a list of variables.

Definition at line 331 of file cp_model.cc.

◆ variables()

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

Returns the vector of variable indices.

Definition at line 408 of file cp_model.h.

◆ WeightedSum() [1/2]

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

Constructs the scalar product of Boolean variables and coefficients.

Definition at line 357 of file cp_model.cc.

◆ WeightedSum() [2/2]

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

Constructs the scalar product of variables and coefficients.

Definition at line 347 of file cp_model.cc.


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