22 #include "absl/base/attributes.h"
32 constexpr
double kInf = std::numeric_limits<double>::infinity();
34 #ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
39 ++num_calls_copy_constructor_;
42 LinearExpression::LinearExpression(LinearExpression&& other)
43 : terms_(std::move(other.terms_)),
45 ++num_calls_move_constructor_;
48 LinearExpression& LinearExpression::operator=(
const LinearExpression& other) {
49 terms_ = other.terms_;
54 ABSL_CONST_INIT thread_local
int
55 LinearExpression::num_calls_default_constructor_ = 0;
56 ABSL_CONST_INIT thread_local
int LinearExpression::num_calls_copy_constructor_ =
58 ABSL_CONST_INIT thread_local
int LinearExpression::num_calls_move_constructor_ =
60 ABSL_CONST_INIT thread_local
int
61 LinearExpression::num_calls_initializer_list_constructor_ = 0;
63 void LinearExpression::ResetCounters() {
64 num_calls_default_constructor_ = 0;
65 num_calls_copy_constructor_ = 0;
66 num_calls_move_constructor_ = 0;
67 num_calls_initializer_list_constructor_ = 0;
71 double LinearExpression::Evaluate(
73 if (variable_values.
storage() !=
nullptr && storage() !=
nullptr) {
74 CHECK_EQ(variable_values.
storage(), storage())
78 for (
const auto& variable : terms_.SortedKeys()) {
79 result += terms_.raw_map().at(variable.typed_id()) *
80 variable_values.
raw_map().at(variable.typed_id());
85 double LinearExpression::EvaluateWithDefaultZero(
87 if (variable_values.
storage() !=
nullptr && storage() !=
nullptr) {
88 CHECK_EQ(variable_values.
storage(), storage())
92 for (
const auto& variable : terms_.SortedKeys()) {
94 terms_.raw_map().at(variable.typed_id()) *
106 const std::vector<Variable> sorted_variables = expression.terms_.SortedKeys();
108 for (
const auto v : sorted_variables) {
109 const double coeff = expression.terms_.at(v);
126 }
else if (lb == -
kInf) {
128 }
else if (ub ==
kInf) {
137 double QuadraticExpression::Evaluate(
139 if (variable_values.
storage() !=
nullptr && storage() !=
nullptr) {
140 CHECK_EQ(variable_values.
storage(), storage())
143 double result = offset();
144 for (
const auto& variable : linear_terms_.SortedKeys()) {
145 result += linear_terms_.raw_map().at(variable.typed_id()) *
146 variable_values.
raw_map().at(variable.typed_id());
148 for (
const auto& variables : quadratic_terms_.SortedKeys()) {
149 result += quadratic_terms_.raw_map().at(variables.typed_id()) *
150 variable_values.
raw_map().at(variables.typed_id().first) *
151 variable_values.
raw_map().at(variables.typed_id().second);
156 double QuadraticExpression::EvaluateWithDefaultZero(
158 if (variable_values.
storage() !=
nullptr && storage() !=
nullptr) {
159 CHECK_EQ(variable_values.
storage(), storage())
162 double result = offset();
163 for (
const auto& variable : linear_terms_.SortedKeys()) {
165 linear_terms_.raw_map().at(variable.typed_id()) *
168 for (
const auto& variables : quadratic_terms_.SortedKeys()) {
169 result += quadratic_terms_.raw_map().at(variables.typed_id()) *
171 variables.typed_id().first) *
173 variables.typed_id().second);
192 v.typed_id().second);
193 if (first_variable == second_variable) {
194 ostr << first_variable <<
"²";
196 ostr << first_variable <<
"*" << second_variable;
216 }
else if (lb == -
kInf) {
218 }
else if (ub ==
kInf) {
227 #ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
228 QuadraticExpression::QuadraticExpression() { ++num_calls_default_constructor_; }
230 QuadraticExpression::QuadraticExpression(
const QuadraticExpression& other)
231 : quadratic_terms_(other.quadratic_terms_),
232 linear_terms_(other.linear_terms_),
234 ++num_calls_copy_constructor_;
237 QuadraticExpression::QuadraticExpression(QuadraticExpression&& other)
238 : quadratic_terms_(std::move(other.quadratic_terms_)),
239 linear_terms_(std::move(other.linear_terms_)),
241 ++num_calls_move_constructor_;
244 QuadraticExpression& QuadraticExpression::operator=(
245 const QuadraticExpression& other) {
246 quadratic_terms_ = other.quadratic_terms_;
247 linear_terms_ = other.linear_terms_;
252 ABSL_CONST_INIT thread_local
int
253 QuadraticExpression::num_calls_default_constructor_ = 0;
254 ABSL_CONST_INIT thread_local
int
255 QuadraticExpression::num_calls_copy_constructor_ = 0;
256 ABSL_CONST_INIT thread_local
int
257 QuadraticExpression::num_calls_move_constructor_ = 0;
258 ABSL_CONST_INIT thread_local
int
259 QuadraticExpression::num_calls_initializer_list_constructor_ = 0;
260 ABSL_CONST_INIT thread_local
int
261 QuadraticExpression::num_calls_linear_expression_constructor_ = 0;
263 void QuadraticExpression::ResetCounters() {
264 num_calls_default_constructor_ = 0;
265 num_calls_copy_constructor_ = 0;
266 num_calls_move_constructor_ = 0;
267 num_calls_initializer_list_constructor_ = 0;
268 num_calls_linear_expression_constructor_ = 0;
const StorageType & raw_map() const
const ModelStorage * storage() const
LinearExpression()=default
const QuadraticTermMap< double > & quadratic_terms() const
const VariableMap< double > & linear_terms() const
const Collection::value_type::second_type & FindWithDefault(const Collection &collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
constexpr absl::string_view kObjectsFromOtherModelStorage
Collection of objects used to extend the Constraint Solver library.
std::ostream & operator<<(std::ostream &out, const std::pair< First, Second > &p)
LinearExpression expression
QuadraticExpression expression