Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#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) |
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 747 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 2156 of file variable_and_expressions.h.
|
inline |
Users should prefer the default constructor and operator overloads to build expressions.
Definition at line 2179 of file variable_and_expressions.h.
|
inline |
Definition at line 2197 of file variable_and_expressions.h.
|
inline |
Definition at line 2200 of file variable_and_expressions.h.
|
inline |
Definition at line 2203 of file variable_and_expressions.h.
|
inline |
Definition at line 2206 of file variable_and_expressions.h.
|
inline |
Definition at line 2215 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 2767 of file variable_and_expressions.h.
|
inline |
}
Definition at line 2753 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 118 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 140 of file variable_and_expressions.cc.
|
inlinestatic |
Definition at line 2773 of file variable_and_expressions.h.
|
inline |
Definition at line 2222 of file variable_and_expressions.h.
|
inline |
Definition at line 2220 of file variable_and_expressions.h.
|
inline |
Definition at line 2719 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2620 of file variable_and_expressions.h.
|
inline |
Definition at line 2614 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2641 of file variable_and_expressions.h.
|
inline |
Definition at line 2633 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 2602 of file variable_and_expressions.h.
|
inline |
Definition at line 2608 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2675 of file variable_and_expressions.h.
|
inline |
Definition at line 2669 of file variable_and_expressions.h.
|
inline |
See comment in LinearExpression::operator+=.
Definition at line 2696 of file variable_and_expressions.h.
|
inline |
Definition at line 2688 of file variable_and_expressions.h.
|
inline |
Definition at line 2657 of file variable_and_expressions.h.
|
inline |
Definition at line 2663 of file variable_and_expressions.h.
|
inline |
Definition at line 2739 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 2168 of file variable_and_expressions.h.
|
inline |
Definition at line 2226 of file variable_and_expressions.h.
|
inline |
Definition at line 2218 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 2760 of file variable_and_expressions.h.
|
friend |
We have 6 types that we must consider arithmetic among:
Definition at line 2353 of file variable_and_expressions.h.
|
friend |
Definition at line 155 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 822 of file variable_and_expressions.h.