![]() |
Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
|
This class represents a sum of quadratic terms, linear terms, and constant offset. For example: "3*x*y + 2*x + 1".
Mixing terms involving variables from different ModelStorage objects will lead to CHECK fails, including from the constructors.
The type owns the associated data representing the terms, and so should usually be passed by (const) reference to avoid unnecessary copies.
Note for implementers: Care must be taken to ensure that linear_terms_.storage() and quadratic_terms_.storage() do not disagree. That is, it is forbidden that both are non-null and not equal. Use CheckModelsAgree() and the initializer_list constructor to enforce this invariant in any class or friend method.
Definition at line 714 of file variable_and_expressions.h.
#include <variable_and_expressions.h>
Static Public Member Functions | |
template<typename Iterable> | |
static QuadraticExpression | Sum (const Iterable &items) |
static RightIterable QuadraticExpression | InnerProduct (const LeftIterable &left, const RightIterable &right) |
Public Attributes | |
* | this = item |
Friends | |
QuadraticExpression | operator- (QuadraticExpression expr) |
std::ostream & | operator<< (std::ostream &ostr, const QuadraticExpression &expr) |
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 |
Users should prefer the default constructor and operator overloads to build expressions.
Definition at line 2094 of file variable_and_expressions.h.
|
inline |
Definition at line 2112 of file variable_and_expressions.h.
|
inline |
Definition at line 2115 of file variable_and_expressions.h.
|
inline |
Definition at line 2118 of file variable_and_expressions.h.
|
inline |
Definition at line 2121 of file variable_and_expressions.h.
|
inline |
Definition at line 2130 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.
QuadraticExpression (no arithmetic)
Definition at line 2069 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 2680 of file variable_and_expressions.h.
|
inline |
}
Definition at line 2666 of file variable_and_expressions.h.
double operations_research::math_opt::QuadraticExpression::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 linear_terms() or quadratic_terms() is missing from variables_values.
Definition at line 123 of file variable_and_expressions.cc.
double operations_research::math_opt::QuadraticExpression::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 145 of file variable_and_expressions.cc.
|
inlinestatic |
Definition at line 2686 of file variable_and_expressions.h.
|
inline |
Definition at line 2135 of file variable_and_expressions.h.
|
inline |
Definition at line 2133 of file variable_and_expressions.h.
|
inline |
Definition at line 2632 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2533 of file variable_and_expressions.h.
|
inline |
Definition at line 2527 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2554 of file variable_and_expressions.h.
|
inline |
Definition at line 2546 of file variable_and_expressions.h.
|
inline |
In-place arithmetic operators.
These must guarantee that the underlying model storages for linear_terms_ and quadratic_terms_ agree upon exit of the function, using CheckModelsAgree(), the list initializer constructor for QuadraticExpression, or similar logic.
Definition at line 2515 of file variable_and_expressions.h.
|
inline |
Definition at line 2521 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2588 of file variable_and_expressions.h.
|
inline |
Definition at line 2582 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2609 of file variable_and_expressions.h.
|
inline |
Definition at line 2601 of file variable_and_expressions.h.
|
inline |
Definition at line 2570 of file variable_and_expressions.h.
|
inline |
Definition at line 2576 of file variable_and_expressions.h.
|
inline |
Definition at line 2652 of file variable_and_expressions.h.
|
default |
|
inlinenoexcept |
Definition at line 2082 of file variable_and_expressions.h.
|
inline |
Definition at line 2139 of file variable_and_expressions.h.
|
inlinestatic |
Returns the sum of the elements of items.
Specifically, letting (i_1, i_2, ..., i_n) = items returns i_1 + i_2 + ... + i_n.
Example: const Variable a = ...; const Variable b = ...; const std::vector<QuadraticTerm> terms = {a * a, 2 * a * b, 3 * b * a}; QuadraticExpression::Sum(vars) => a^2 + 5 a * b Note, instead of: QuadraticExpression expr(3.0); expr += QuadraticExpression::Sum(items); Prefer: expr.AddSum(items);
See QuadraticExpression::AddSum() for a precise contract on the type Iterable.
Definition at line 2673 of file variable_and_expressions.h.
|
friend |
We have 6 types that we must consider arithmetic among:
Definition at line 2266 of file variable_and_expressions.h.
|
friend |
Definition at line 160 of file variable_and_expressions.cc.
* operations_research::math_opt::QuadraticExpression::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}; const std::vector<QuadraticTerm> terms = {2 * a * b}; QuadraticExpression expr = 8; expr.AddSum(vars); expr.AddSum(terms); Results in expr having the value 2 * a * b + 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> vars = {a, b}; const std::vector<double> coeffs = {10.0, 2.0}; QuadraticExpression expr = 3.0; expr.AddInnerProduct(coeffs, vars); expr.AddInnerProduct(vars, vars); Results in expr having the value a^2 + b^2 + 10.0 * a + 2.0 * b + 3.0.
Compile time requirements:
Definition at line 787 of file variable_and_expressions.h.