Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <variable_and_expressions.h>
Public Member Functions | |
LinearExpression ()=default | |
LinearExpression (const LinearExpression &other)=default | |
LinearExpression (LinearExpression &&other) noexcept | |
LinearExpression (std::initializer_list< LinearTerm > terms, double offset) | |
LinearExpression (double offset) | |
LinearExpression (Variable variable) | |
LinearExpression (const LinearTerm &term) | |
LinearExpression & | operator= (const LinearExpression &other)=default |
LinearExpression & | operator= (LinearExpression &&other) noexcept |
LinearExpression & | operator+= (const LinearExpression &other) |
LinearExpression & | operator+= (const LinearTerm &term) |
LinearExpression & | operator+= (Variable variable) |
LinearExpression & | operator+= (double value) |
LinearExpression & | operator-= (const LinearExpression &other) |
LinearExpression & | operator-= (const LinearTerm &term) |
LinearExpression & | operator-= (Variable variable) |
LinearExpression & | operator-= (double value) |
LinearExpression & | operator*= (double value) |
LinearExpression & | operator/= (double value) |
template<typename Iterable > | |
void | AddSum (const Iterable &items) |
} | |
template<typename LeftIterable , typename RightIterable > | |
void | AddInnerProduct (const LeftIterable &left, const RightIterable &right) |
const VariableMap< double > & | terms () const |
Returns the terms in this expression. | |
double | offset () const |
double | Evaluate (const VariableMap< double > &variable_values) const |
double | EvaluateWithDefaultZero (const VariableMap< double > &variable_values) const |
const ModelStorage * | storage () const |
Static Public Member Functions | |
template<typename Iterable > | |
static LinearExpression | Sum (const Iterable &items) |
template<typename LeftIterable , typename RightIterable > | |
static LinearExpression | InnerProduct (const LeftIterable &left, const RightIterable &right) |
Public Attributes | |
* | this = item |
Friends | |
LinearExpression | operator- (LinearExpression expr) |
std::ostream & | operator<< (std::ostream &ostr, const LinearExpression &expression) |
This class represents a sum of variables multiplied by coefficient and an optional offset constant. For example: "3*x + 2*y + 5".
All operations, including constructor, will raise an assertion if the operands involve variables from different Model objects.
Contrary to Variable type, expressions owns the linear expression their represent. Hence they are usually passed by reference to prevent unnecessary copies.
(b/169415098): add a function to remove zero terms.
(b/169415834): study if exact zeros should be automatically removed.
(b/169415103): add tests that some expressions don't compile.
Definition at line 229 of file variable_and_expressions.h.
|
default |
For unit testing purpose, we define optional counters. We have to explicitly define the default constructor, copy constructor and assignment operators in that case. Else we use the defaults.
|
default |
|
inlinenoexcept |
We have to define a custom move constructor as we need to reset storage_ to nullptr.
Definition at line 1390 of file variable_and_expressions.h.
|
inline |
Usually users should use the overloads of operators to build linear expressions. For example, assuming x
and y
are Variable, then x + 2*y / + 5
will build a LinearExpression automatically.
The same variable may appear multiple times in the input list; we must accumulate the coefficients.
Definition at line 1409 of file variable_and_expressions.h.
|
inline |
Definition at line 1423 of file variable_and_expressions.h.
|
inline |
Definition at line 1426 of file variable_and_expressions.h.
|
inline |
Definition at line 1429 of file variable_and_expressions.h.
|
inline |
} In particular, the multiplication will be performed on the types of the elements in left and right (take care with low precision types), but the addition will always use double precision.
Definition at line 1703 of file variable_and_expressions.h.
|
inline |
}
Definition at line 1662 of file variable_and_expressions.h.
double operations_research::math_opt::LinearExpression::Evaluate | ( | const VariableMap< double > & | variable_values | ) | const |
Compute the numeric value of this expression when variables are substituted by their values in variable_values.
Will CHECK fail if a variable in terms() is missing from variables_values.
Definition at line 59 of file variable_and_expressions.cc.
double operations_research::math_opt::LinearExpression::EvaluateWithDefaultZero | ( | const VariableMap< double > & | variable_values | ) | const |
Compute the numeric value of this expression when variables are substituted by their values in variable_values, or zero if missing from the map.
This function won't check that the variables in the input map are indeed in the same model as the ones of the expression.
Definition at line 71 of file variable_and_expressions.cc.
|
inlinestatic |
Creates a new LinearExpression object equal to the inner product. The implementation is equivalent to: LinearExpression expr; expr.AddInnerProduct(left, right);
Definition at line 1709 of file variable_and_expressions.h.
|
inline |
Definition at line 1724 of file variable_and_expressions.h.
|
inline |
Definition at line 1645 of file variable_and_expressions.h.
|
inline |
Here we know that each key in other.terms_ has already been checked and thus we don't need to compare in the loop. Of course this only applies if the other has terms.
Definition at line 1587 of file variable_and_expressions.h.
|
inline |
Definition at line 1601 of file variable_and_expressions.h.
|
inline |
Definition at line 1612 of file variable_and_expressions.h.
|
inline |
Definition at line 1607 of file variable_and_expressions.h.
|
inline |
See operator+=.
Definition at line 1617 of file variable_and_expressions.h.
|
inline |
Definition at line 1629 of file variable_and_expressions.h.
|
inline |
Definition at line 1640 of file variable_and_expressions.h.
|
inline |
Definition at line 1635 of file variable_and_expressions.h.
|
inline |
Definition at line 1653 of file variable_and_expressions.h.
|
default |
|
inlinenoexcept |
We have to define a custom move assignment operator as we need to reset storage_ to nullptr.
Definition at line 1400 of file variable_and_expressions.h.
|
inline |
Definition at line 1726 of file variable_and_expressions.h.
|
inlinestatic |
Creates a new LinearExpression object equal to the sum. The implementation is equivalent to: LinearExpression expr; expr.AddSum(items);
Definition at line 1669 of file variable_and_expressions.h.
|
inline |
Returns the terms in this expression.
Definition at line 1722 of file variable_and_expressions.h.
|
friend |
We intentionally pass one of the LinearExpression argument by value so that we don't make unnecessary copies of temporary objects by using the move constructor and the returned values optimization (RVO).
Definition at line 1432 of file variable_and_expressions.h.
|
friend |
Definition at line 81 of file variable_and_expressions.cc.
* operations_research::math_opt::LinearExpression::this = item |
Adds each element of items to this.
Specifically, letting (i_1, i_2, ..., i_n) = items adds i_1 + i_2 + ... + i_n to this.
Example: const Variable a = ...; const Variable b = ...; const std::vector<Variable> vars = {a, b}; LinearExpression expr(8.0); expr.AddSum(vars); Results in expr having the value a + b + 8.0.
Compile time requirements:
Adds the inner product of left and right to this.
Specifically, letting (l_1, l_2 ..., l_n) = left, (r_1, r_2, ..., r_n) = right, adds l_1 * r_1 + l_2 * r_2 + ... + l_n * r_n to this.
Example: const Variable a = ...; const Variable b = ...; const std::vector<Variable> left = {a, b}; const std::vector<double> right = {10.0, 2.0}; LinearExpression expr(3.0); expr.AddInnerProduct(left, right) Results in expr having the value 10.0 * a + 2.0 * b + 3.0.
Compile time requirements:
Definition at line 292 of file variable_and_expressions.h.