![]() |
Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
|
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 207 of file variable_and_expressions.h.
#include <variable_and_expressions.h>
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) |
Additional Inherited Members | |
Protected Member Functions inherited from operations_research::math_opt::ModelStorageItemContainer | |
ModelStorageItemContainer (const NullableModelStorageCPtr storage=nullptr) | |
ModelStorageItemContainer (const ModelStorageItemContainer &other)=default | |
ModelStorageItemContainer & | operator= (const ModelStorageItemContainer &other)=default |
ModelStorageItemContainer (ModelStorageItemContainer &&other) | |
ModelStorageItemContainer & | operator= (ModelStorageItemContainer &&other) |
void | SetOrCheckStorage (const ModelStorageItem &item) |
void | SetOrCheckStorage (const ModelStorageItemContainer &container) |
|
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 |
|
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 1335 of file variable_and_expressions.h.
|
inline |
Definition at line 1349 of file variable_and_expressions.h.
|
inline |
Definition at line 1352 of file variable_and_expressions.h.
|
inline |
Definition at line 1355 of file variable_and_expressions.h.
|
inlinenoexcept |
A moved-from LinearExpression is the zero expression: it's not associated to a storage, has no terms and its offset is zero.
Definition at line 1314 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 1629 of file variable_and_expressions.h.
|
inline |
}
Definition at line 1588 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 64 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 76 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 1635 of file variable_and_expressions.h.
|
inline |
Definition at line 1650 of file variable_and_expressions.h.
|
inline |
Definition at line 1571 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 1513 of file variable_and_expressions.h.
|
inline |
Definition at line 1527 of file variable_and_expressions.h.
|
inline |
Definition at line 1538 of file variable_and_expressions.h.
|
inline |
Definition at line 1533 of file variable_and_expressions.h.
|
inline |
See operator+=.
Definition at line 1543 of file variable_and_expressions.h.
|
inline |
Definition at line 1555 of file variable_and_expressions.h.
|
inline |
Definition at line 1566 of file variable_and_expressions.h.
|
inline |
Definition at line 1561 of file variable_and_expressions.h.
|
inline |
Definition at line 1579 of file variable_and_expressions.h.
|
default |
|
inlinenoexcept |
Definition at line 1325 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 1595 of file variable_and_expressions.h.
|
inline |
Returns the terms in this expression.
Definition at line 1648 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 1358 of file variable_and_expressions.h.
|
friend |
Definition at line 86 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 268 of file variable_and_expressions.h.