22#include "absl/base/attributes.h"
27#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
35constexpr double kInf = std::numeric_limits<double>::infinity();
37#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
43 offset_(other.offset_) {
44 ++num_calls_copy_constructor_;
47ABSL_CONST_INIT
thread_local int
48 LinearExpression::num_calls_default_constructor_ = 0;
49ABSL_CONST_INIT
thread_local int LinearExpression::num_calls_copy_constructor_ =
51ABSL_CONST_INIT
thread_local int LinearExpression::num_calls_move_constructor_ =
53ABSL_CONST_INIT
thread_local int
54 LinearExpression::num_calls_initializer_list_constructor_ = 0;
56void LinearExpression::ResetCounters() {
57 num_calls_default_constructor_ = 0;
58 num_calls_copy_constructor_ = 0;
59 num_calls_move_constructor_ = 0;
60 num_calls_initializer_list_constructor_ = 0;
66 double result = offset_;
67 for (
const auto& variable :
SortedKeys(terms_)) {
68 const auto found = variable_values.find(variable);
69 CHECK(found != variable_values.end())
71 result += terms_.at(variable) * found->second;
78 double result = offset_;
79 for (
const auto& variable :
SortedKeys(terms_)) {
92 const std::vector<Variable> sorted_variables =
SortedKeys(expression.terms_);
94 for (
const auto v : sorted_variables) {
95 const double coeff = expression.terms_.at(v);
112 }
else if (lb == -
kInf) {
114 }
else if (ub ==
kInf) {
126 for (
const auto& variable :
SortedKeys(linear_terms_)) {
127 const auto found = variable_values.find(variable);
128 CHECK(found != variable_values.end())
130 result += linear_terms_.at(variable) * found->second;
132 for (
const auto& variables :
SortedKeys(quadratic_terms_)) {
133 const auto found_first = variable_values.find(variables.first());
134 CHECK(found_first != variable_values.end())
136 const auto found_second = variable_values.find(variables.second());
137 CHECK(found_second != variable_values.end())
139 result += quadratic_terms_.at(variables) * found_first->second *
140 found_second->second;
148 for (
const auto& variable :
SortedKeys(linear_terms_)) {
149 result += linear_terms_.at(variable) *
152 for (
const auto& variables :
SortedKeys(quadratic_terms_)) {
153 result += quadratic_terms_.at(variables) *
171 const Variable first_variable = vs.first();
172 const Variable second_variable = vs.second();
173 if (first_variable == second_variable) {
174 ostr << first_variable <<
"²";
176 ostr << first_variable <<
"*" << second_variable;
196 }
else if (lb == -
kInf) {
198 }
else if (ub ==
kInf) {
207#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
208QuadraticExpression::QuadraticExpression() { ++num_calls_default_constructor_; }
210QuadraticExpression::QuadraticExpression(
const QuadraticExpression& other)
211 : ModelStorageItemContainer(other),
212 quadratic_terms_(other.quadratic_terms_),
213 linear_terms_(other.linear_terms_),
214 offset_(other.offset_) {
215 ++num_calls_copy_constructor_;
218ABSL_CONST_INIT
thread_local int
219 QuadraticExpression::num_calls_default_constructor_ = 0;
220ABSL_CONST_INIT
thread_local int
221 QuadraticExpression::num_calls_copy_constructor_ = 0;
222ABSL_CONST_INIT
thread_local int
223 QuadraticExpression::num_calls_move_constructor_ = 0;
224ABSL_CONST_INIT
thread_local int
225 QuadraticExpression::num_calls_initializer_list_constructor_ = 0;
226ABSL_CONST_INIT
thread_local int
227 QuadraticExpression::num_calls_linear_expression_constructor_ = 0;
229void QuadraticExpression::ResetCounters() {
230 num_calls_default_constructor_ = 0;
231 num_calls_copy_constructor_ = 0;
232 num_calls_move_constructor_ = 0;
233 num_calls_initializer_list_constructor_ = 0;
234 num_calls_linear_expression_constructor_ = 0;
double EvaluateWithDefaultZero(const VariableMap< double > &variable_values) const
double Evaluate(const VariableMap< double > &variable_values) const
LinearExpression()=default
const QuadraticTermMap< double > & quadratic_terms() const
double EvaluateWithDefaultZero(const VariableMap< double > &variable_values) const
const VariableMap< double > & linear_terms() const
double Evaluate(const VariableMap< double > &variable_values) const
QuadraticExpression()=default
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
constexpr absl::string_view kObjectsFromOtherModelStorage
An object oriented wrapper for quadratic constraints in ModelStorage.
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