22#include "absl/base/attributes.h"
32constexpr double kInf = std::numeric_limits<double>::infinity();
34#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
38 : storage_(other.storage_), terms_(other.terms_), offset_(other.offset_) {
39 ++num_calls_copy_constructor_;
42ABSL_CONST_INIT
thread_local int
43 LinearExpression::num_calls_default_constructor_ = 0;
44ABSL_CONST_INIT
thread_local int LinearExpression::num_calls_copy_constructor_ =
46ABSL_CONST_INIT
thread_local int LinearExpression::num_calls_move_constructor_ =
48ABSL_CONST_INIT
thread_local int
49 LinearExpression::num_calls_initializer_list_constructor_ = 0;
51void LinearExpression::ResetCounters() {
52 num_calls_default_constructor_ = 0;
53 num_calls_copy_constructor_ = 0;
54 num_calls_move_constructor_ = 0;
55 num_calls_initializer_list_constructor_ = 0;
59double LinearExpression::Evaluate(
61 double result = offset_;
62 for (
const auto& variable :
SortedKeys(terms_)) {
63 const auto found = variable_values.find(variable);
64 CHECK(found != variable_values.end())
65 << internal::kObjectsFromOtherModelStorage;
66 result += terms_.at(variable) * found->second;
71double LinearExpression::EvaluateWithDefaultZero(
73 double result = offset_;
74 for (
const auto& variable :
SortedKeys(terms_)) {
87 const std::vector<Variable> sorted_variables =
SortedKeys(expression.terms_);
89 for (
const auto v : sorted_variables) {
90 const double coeff = expression.terms_.at(v);
107 }
else if (lb == -
kInf) {
109 }
else if (ub ==
kInf) {
118double QuadraticExpression::Evaluate(
120 double result = offset();
121 for (
const auto& variable :
SortedKeys(linear_terms_)) {
122 const auto found = variable_values.find(variable);
123 CHECK(found != variable_values.end())
124 << internal::kObjectsFromOtherModelStorage;
125 result += linear_terms_.at(variable) * found->second;
127 for (
const auto& variables :
SortedKeys(quadratic_terms_)) {
128 const auto found_first = variable_values.find(variables.first());
129 CHECK(found_first != variable_values.end())
130 << internal::kObjectsFromOtherModelStorage;
131 const auto found_second = variable_values.find(variables.second());
132 CHECK(found_second != variable_values.end())
133 << internal::kObjectsFromOtherModelStorage;
134 result += quadratic_terms_.at(variables) * found_first->second *
135 found_second->second;
140double QuadraticExpression::EvaluateWithDefaultZero(
142 double result = offset();
143 for (
const auto& variable :
SortedKeys(linear_terms_)) {
144 result += linear_terms_.at(variable) *
147 for (
const auto& variables :
SortedKeys(quadratic_terms_)) {
148 result += quadratic_terms_.at(variables) *
166 const Variable first_variable = vs.first();
167 const Variable second_variable = vs.second();
168 if (first_variable == second_variable) {
169 ostr << first_variable <<
"²";
171 ostr << first_variable <<
"*" << second_variable;
191 }
else if (lb == -
kInf) {
193 }
else if (ub ==
kInf) {
202#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
203QuadraticExpression::QuadraticExpression() { ++num_calls_default_constructor_; }
205QuadraticExpression::QuadraticExpression(
const QuadraticExpression& other)
206 : storage_(other.storage_),
207 quadratic_terms_(other.quadratic_terms_),
208 linear_terms_(other.linear_terms_),
209 offset_(other.offset_) {
210 ++num_calls_copy_constructor_;
213ABSL_CONST_INIT
thread_local int
214 QuadraticExpression::num_calls_default_constructor_ = 0;
215ABSL_CONST_INIT
thread_local int
216 QuadraticExpression::num_calls_copy_constructor_ = 0;
217ABSL_CONST_INIT
thread_local int
218 QuadraticExpression::num_calls_move_constructor_ = 0;
219ABSL_CONST_INIT
thread_local int
220 QuadraticExpression::num_calls_initializer_list_constructor_ = 0;
221ABSL_CONST_INIT
thread_local int
222 QuadraticExpression::num_calls_linear_expression_constructor_ = 0;
224void QuadraticExpression::ResetCounters() {
225 num_calls_default_constructor_ = 0;
226 num_calls_copy_constructor_ = 0;
227 num_calls_move_constructor_ = 0;
228 num_calls_initializer_list_constructor_ = 0;
229 num_calls_linear_expression_constructor_ = 0;
LinearExpression()=default
const QuadraticTermMap< double > & quadratic_terms() const
const VariableMap< double > & linear_terms() const
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
absl::flat_hash_map< Variable, V > VariableMap
std::vector< typename Map::key_type > SortedKeys(const Map &map)
In SWIG mode, we don't want anything besides these top-level includes.
std::ostream & operator<<(std::ostream &out, const std::pair< First, Second > &p)
A LinearExpression with upper and lower bounds.
LinearExpression expression
A QuadraticExpression with upper and lower bounds.
QuadraticExpression expression