92#ifndef OR_TOOLS_MATH_OPT_CPP_VARIABLE_AND_EXPRESSIONS_H_
93#define OR_TOOLS_MATH_OPT_CPP_VARIABLE_AND_EXPRESSIONS_H_
97#include <initializer_list>
104#include "absl/container/flat_hash_map.h"
105#include "absl/log/check.h"
106#include "absl/log/log.h"
107#include "absl/strings/string_view.h"
122 ElementType::kVariable, Variable,
125 ModelStorageElementEquality::kWithoutEquality> {
132 inline absl::string_view
name()
const;
155 inline operator bool()
const;
212#ifndef MATH_OPT_USE_EXPRESSION_COUNTERS
270 template <
typename Iterable>
271 inline void AddSum(
const Iterable& items);
277 template <
typename Iterable>
316 template <
typename LeftIterable,
typename RightIterable>
318 const RightIterable& right);
324 template <
typename LeftIterable,
typename RightIterable>
326 const RightIterable& right);
330 inline double offset()
const;
346#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
347 static thread_local int num_calls_default_constructor_;
348 static thread_local int num_calls_copy_constructor_;
349 static thread_local int num_calls_move_constructor_;
350 static thread_local int num_calls_initializer_list_constructor_;
352 static void ResetCounters();
357 friend std::ostream&
operator<<(std::ostream& ostr,
359 friend QuadraticExpression;
365 double offset_ = 0.0;
391template <
typename Iterable>
420template <
typename LeftIterable,
typename RightIterable>
422 const RightIterable& right);
641 template <
typename H>
648inline std::ostream&
operator<<(std::ostream& ostr,
649 const QuadraticTermKey& key);
651inline bool operator==(QuadraticTermKey lhs, QuadraticTermKey rhs);
652inline bool operator!=(QuadraticTermKey lhs, QuadraticTermKey rhs);
719#ifndef MATH_OPT_USE_EXPRESSION_COUNTERS
742 inline double offset()
const;
789 template <
typename Iterable>
790 inline void AddSum(
const Iterable& items);
813 template <
typename Iterable>
854 template <
typename LeftIterable,
typename RightIterable>
856 const RightIterable& right);
882 template <
typename LeftIterable,
typename RightIterable>
884 const RightIterable& right);
901#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
902 static thread_local int num_calls_default_constructor_;
903 static thread_local int num_calls_copy_constructor_;
904 static thread_local int num_calls_move_constructor_;
905 static thread_local int num_calls_initializer_list_constructor_;
906 static thread_local int num_calls_linear_expression_constructor_;
908 static void ResetCounters();
913 friend std::ostream&
operator<<(std::ostream& ostr,
922 double offset_ = 0.0;
1245 return "[variable deleted from model]";
1257 : variable(
std::move(variable)), coefficient(coefficient) {}
1274 term *= coefficient;
1279 term *= coefficient;
1284 return LinearTerm(std::move(variable), coefficient);
1288 return LinearTerm(std::move(variable), coefficient);
1292 term /= coefficient;
1297 return LinearTerm(std::move(variable), 1 / coefficient);
1307 terms_(std::move(other.terms_)),
1308 offset_(std::exchange(other.offset_, 0.0)) {
1309 other.terms_.clear();
1310#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
1311 ++num_calls_move_constructor_;
1319 terms_ = std::move(other.terms_);
1320 other.terms_.clear();
1321 offset_ = std::exchange(other.offset_, 0.0);
1328#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
1329 ++num_calls_initializer_list_constructor_;
1331 for (
const auto& term :
terms) {
1335 terms_[term.variable] += term.coefficient;
1340 : LinearExpression({}, offset) {}
1349 expr.offset_ = -expr.offset_;
1350 for (
auto& term : expr.terms_) {
1351 term.second = -term.second;
1365 return LinearTerm(lhs, 1.0) + LinearTerm(rhs, 1.0);
1368LinearExpression
operator+(
const LinearTerm& lhs,
const double rhs) {
1369 return LinearExpression({lhs}, rhs);
1372LinearExpression
operator+(
const double lhs,
const LinearTerm& rhs) {
1373 return LinearExpression({rhs}, lhs);
1377 return lhs + LinearTerm(rhs, 1.0);
1381 return LinearTerm(lhs, 1.0) + rhs;
1384LinearExpression
operator+(
const LinearTerm& lhs,
const LinearTerm& rhs) {
1385 return LinearExpression({lhs, rhs}, 0);
1388LinearExpression
operator+(LinearExpression lhs,
const double rhs) {
1393LinearExpression
operator+(
const double lhs, LinearExpression rhs) {
1399 return std::move(lhs) +
LinearTerm(rhs, 1.0);
1406LinearExpression
operator+(LinearExpression lhs,
const LinearTerm& rhs) {
1411LinearExpression
operator+(LinearTerm lhs, LinearExpression rhs) {
1430 return LinearTerm(lhs, 1.0) - LinearTerm(rhs, 1.0);
1433LinearExpression
operator-(
const LinearTerm& lhs,
const double rhs) {
1434 return LinearExpression({lhs}, -rhs);
1437LinearExpression
operator-(
const double lhs,
const LinearTerm& rhs) {
1438 return LinearExpression({-rhs}, lhs);
1442 return lhs - LinearTerm(rhs, 1.0);
1446 return LinearTerm(lhs, 1.0) - rhs;
1449LinearExpression
operator-(
const LinearTerm& lhs,
const LinearTerm& rhs) {
1450 return LinearExpression({lhs, -rhs}, 0);
1453LinearExpression
operator-(LinearExpression lhs,
const double rhs) {
1458LinearExpression
operator-(
const double lhs, LinearExpression rhs) {
1459 auto ret = -std::move(rhs);
1465 return std::move(lhs) -
LinearTerm(rhs, 1.0);
1469 return LinearTerm(lhs, 1.0) - std::move(rhs);
1472LinearExpression
operator-(LinearExpression lhs,
const LinearTerm& rhs) {
1477LinearExpression
operator-(LinearTerm lhs, LinearExpression rhs) {
1478 auto ret = -std::move(rhs);
1507 if (!other.terms_.empty()) {
1509 for (
const auto& [v, coeff] : other.terms_) {
1513 offset_ += other.offset_;
1525 return *
this += LinearTerm(variable, 1.0);
1535 if (!other.terms_.empty()) {
1537 for (
const auto& [v, coeff] : other.terms_) {
1541 offset_ -= other.offset_;
1553 return *
this -= LinearTerm(variable, 1.0);
1563 for (
auto& term : terms_) {
1564 term.second *= value;
1571 for (
auto& term : terms_) {
1572 term.second /= value;
1577template <
typename Iterable>
1579 for (
const auto& item : items) {
1584template <
typename Iterable>
1591template <
typename Iterable>
1598template <
typename LeftIterable,
typename RightIterable,
typename Expression>
1599void AddInnerProduct(
const LeftIterable& left,
const RightIterable& right,
1603 auto l =
begin(left);
1604 auto r =
begin(right);
1605 const auto l_end =
end(left);
1606 const auto r_end =
end(right);
1607 for (; l != l_end && r != r_end; ++l, ++r) {
1608 expr += (*l) * (*r);
1611 <<
"left had more elements than right, sizes should be equal";
1613 <<
"right had more elements than left, sizes should be equal";
1618template <
typename LeftIterable,
typename RightIterable>
1620 const RightIterable& right) {
1624template <
typename LeftIterable,
typename RightIterable>
1626 const RightIterable& right) {
1628 result.AddInnerProduct(left, right);
1632template <
typename LeftIterable,
typename RightIterable>
1634 const RightIterable& right) {
1649 : lhs(
std::move(lhs)), rhs(
std::move(rhs)) {}
1651inline VariablesEquality::operator bool()
const {
1652 return lhs.
typed_id() == rhs.typed_id() && lhs.storage() == rhs.storage();
1659 return internal::VariablesEquality(lhs, rhs);
1674 : expression(
std::move(expression)), lower_bound(lower_bound) {}
1676UpperBoundedLinearExpression::UpperBoundedLinearExpression(
1677 LinearExpression expression,
const double upper_bound)
1678 : expression(
std::move(expression)), upper_bound(upper_bound) {}
1680BoundedLinearExpression::BoundedLinearExpression(LinearExpression expression,
1681 const double lower_bound,
1682 const double upper_bound)
1683 : expression(
std::move(expression)),
1684 lower_bound(lower_bound),
1685 upper_bound(upper_bound) {}
1714 const double constant) {
1724 const double constant) {
1734 const double constant) {
1735 return LinearTerm(variable, 1.0) >= constant;
1740 return constant <=
LinearTerm(variable, 1.0);
1744 const double constant) {
1754 const double constant) {
1764 const double constant) {
1765 return LinearTerm(variable, 1.0) <= constant;
1770 return constant >=
LinearTerm(variable, 1.0);
1780BoundedLinearExpression
operator>=(
const double lhs,
1781 LowerBoundedLinearExpression rhs) {
1805 std::move(lhs), -std::numeric_limits<double>::infinity(),
1813 std::move(lhs), 0.0,
1814 std::numeric_limits<double>::infinity());
1817BoundedLinearExpression
operator<=(LinearExpression lhs,
1818 const LinearTerm& rhs) {
1820 return BoundedLinearExpression(
1821 std::move(lhs), -std::numeric_limits<double>::infinity(),
1825BoundedLinearExpression
operator>=(LinearExpression lhs,
1826 const LinearTerm& rhs) {
1828 return BoundedLinearExpression(
1829 std::move(lhs), 0.0,
1830 std::numeric_limits<double>::infinity());
1833BoundedLinearExpression
operator<=(
const LinearTerm& lhs,
1834 LinearExpression rhs) {
1836 return BoundedLinearExpression(
1837 std::move(rhs), 0.0,
1838 std::numeric_limits<double>::infinity());
1841BoundedLinearExpression
operator>=(
const LinearTerm& lhs,
1842 LinearExpression rhs) {
1844 return BoundedLinearExpression(
1845 std::move(rhs), -std::numeric_limits<double>::infinity(),
1850 return std::move(lhs) <= LinearTerm(rhs, 1.0);
1854 return std::move(lhs) >= LinearTerm(rhs, 1.0);
1858 return LinearTerm(lhs, 1.0) <= std::move(rhs);
1862 return LinearTerm(lhs, 1.0) >= std::move(rhs);
1865BoundedLinearExpression
operator<=(
const LinearTerm& lhs,
1866 const LinearTerm& rhs) {
1869 -std::numeric_limits<double>::infinity(),
1873BoundedLinearExpression
operator>=(
const LinearTerm& lhs,
1874 const LinearTerm& rhs) {
1877 std::numeric_limits<double>::infinity());
1885 return lhs >= LinearTerm(rhs, 1.0);
1893 return LinearTerm(lhs, 1.0) >= rhs;
1897 return LinearTerm(lhs, 1.0) <= LinearTerm(rhs, 1.0);
1901 return LinearTerm(lhs, 1.0) >= LinearTerm(rhs, 1.0);
1904BoundedLinearExpression
operator==(LinearExpression lhs,
1905 const LinearExpression& rhs) {
1926 return std::move(lhs) ==
LinearTerm(rhs, 1.0);
1930 return LinearTerm(lhs, 1.0) == std::move(rhs);
1945BoundedLinearExpression
operator==(
const LinearTerm& lhs,
1946 const LinearTerm& rhs) {
1957 return LinearTerm(lhs, 1.0) == rhs;
1965BoundedLinearExpression
operator==(
const double lhs,
const LinearTerm& rhs) {
1985 if (variable_ids_.first > variable_ids_.second) {
1988 swap(variable_ids_.first, variable_ids_.second);
1994 : QuadraticTermKey(first_variable.storage(), {first_variable.typed_id(),
1995 second_variable.typed_id()}) {
1996 CHECK_EQ(first_variable.storage(), second_variable.storage())
1997 << internal::kObjectsFromOtherModelStorage;
2002template <
typename H>
2004 return H::combine(std::move(h), key.typed_id().first.value(),
2005 key.typed_id().second.value(), key.storage());
2019 return !(lhs == rhs);
2027 const double coefficient)
2028 : first_variable_(
std::move(first_variable)),
2029 second_variable_(
std::move(second_variable)),
2030 coefficient_(coefficient) {
2031 CHECK_EQ(first_variable_.storage(), second_variable_.storage())
2036Variable QuadraticTerm::first_variable()
const {
return first_variable_; }
2037Variable QuadraticTerm::second_variable()
const {
return second_variable_; }
2039QuadraticTermKey QuadraticTerm::GetKey()
const {
2040 return QuadraticTermKey(
2041 first_variable_.storage(),
2042 std::make_pair(first_variable_.typed_id(), second_variable_.typed_id()));
2052 quadratic_terms_(std::move(other.quadratic_terms_)),
2053 linear_terms_(std::move(other.linear_terms_)),
2054 offset_(std::exchange(other.offset_, 0.0)) {
2055 other.quadratic_terms_.clear();
2056 other.linear_terms_.clear();
2057#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
2058 ++num_calls_move_constructor_;
2066 quadratic_terms_ = std::move(other.quadratic_terms_);
2067 other.quadratic_terms_.clear();
2068 linear_terms_ = std::move(other.linear_terms_);
2069 other.linear_terms_.clear();
2070 offset_ = std::exchange(other.offset_, 0.0);
2075 const std::initializer_list<QuadraticTerm> quadratic_terms,
2076 const std::initializer_list<LinearTerm> linear_terms,
const double offset)
2078#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
2079 ++num_calls_initializer_list_constructor_;
2083 linear_terms_[term.variable] += term.coefficient;
2088 quadratic_terms_[key] += term.coefficient();
2093 : QuadraticExpression({}, {}, offset) {}
2099 : QuadraticExpression({}, {term}, 0.0) {}
2103 linear_terms_(std::move(expr.terms_)),
2104 offset_(expr.offset_) {
2105#ifdef MATH_OPT_USE_EXPRESSION_COUNTERS
2106 ++num_calls_linear_expression_constructor_;
2116 return linear_terms_;
2120 return quadratic_terms_;
2138QuadraticExpression
operator+(
const double lhs, QuadraticExpression rhs) {
2167QuadraticExpression
operator+(
const LinearExpression& lhs,
2168 QuadraticExpression rhs) {
2173QuadraticExpression
operator+(
const QuadraticTerm& lhs,
const double rhs) {
2185QuadraticExpression
operator+(
const QuadraticTerm& lhs, LinearExpression rhs) {
2186 QuadraticExpression expr(std::move(rhs));
2191QuadraticExpression
operator+(
const QuadraticTerm& lhs,
2192 const QuadraticTerm& rhs) {
2193 return QuadraticExpression({lhs, rhs}, {}, 0.0);
2202QuadraticExpression
operator+(QuadraticExpression lhs,
const double rhs) {
2212QuadraticExpression
operator+(QuadraticExpression lhs,
const LinearTerm& rhs) {
2217QuadraticExpression
operator+(QuadraticExpression lhs,
2223QuadraticExpression
operator+(QuadraticExpression lhs,
2239 term.coefficient_ *= -1.0;
2244QuadraticExpression
operator-(QuadraticExpression expr) {
2245 expr.offset_ = -expr.offset_;
2246 for (
auto& term : expr.linear_terms_) {
2247 term.second = -term.second;
2249 for (
auto& term : expr.quadratic_terms_) {
2250 term.second = -term.second;
2255QuadraticExpression
operator-(
const double lhs,
const QuadraticTerm& rhs) {
2256 return QuadraticExpression({-rhs}, {}, lhs);
2260 auto expr = -std::move(rhs);
2270 return LinearTerm(lhs, 1.0) - std::move(rhs);
2278 auto expr = -std::move(rhs);
2283QuadraticExpression
operator-(LinearExpression lhs,
const QuadraticTerm& rhs) {
2284 QuadraticExpression expr(std::move(lhs));
2289QuadraticExpression
operator-(
const LinearExpression& lhs,
2290 QuadraticExpression rhs) {
2291 auto expr = -std::move(rhs);
2304QuadraticExpression
operator-(
const QuadraticTerm& lhs,
const LinearTerm& rhs) {
2314QuadraticExpression
operator-(
const QuadraticTerm& lhs,
2315 const QuadraticTerm& rhs) {
2316 return QuadraticExpression({lhs, -rhs}, {}, 0.0);
2350QuadraticExpression
operator-(QuadraticExpression lhs,
2351 const QuadraticTerm& rhs) {
2356QuadraticExpression
operator-(QuadraticExpression lhs,
2357 const QuadraticExpression& rhs) {
2365QuadraticTerm
operator*(
const double lhs, QuadraticTerm rhs) {
2366 rhs.coefficient_ *= lhs;
2370QuadraticExpression
operator*(
const double lhs, QuadraticExpression rhs) {
2376 return QuadraticTerm(std::move(lhs), std::move(rhs), 1.0);
2385 QuadraticExpression expr;
2386 for (
const auto& [var, coeff] : rhs.terms()) {
2389 if (rhs.offset() != 0) {
2390 expr +=
LinearTerm(std::move(lhs), rhs.offset());
2396 return QuadraticTerm(std::move(lhs.variable), std::move(rhs),
2400QuadraticTerm
operator*(LinearTerm lhs, LinearTerm rhs) {
2402 lhs.coefficient * rhs.coefficient);
2405QuadraticExpression
operator*(LinearTerm lhs,
const LinearExpression& rhs) {
2407 for (
const auto& [var, coeff] : rhs.terms()) {
2408 expr +=
QuadraticTerm(lhs.variable, var, lhs.coefficient * coeff);
2410 if (rhs.offset() != 0) {
2411 expr += LinearTerm(std::move(lhs.variable), lhs.coefficient * rhs.offset());
2418 for (
const auto& [var, coeff] : lhs.terms()) {
2421 if (lhs.offset() != 0) {
2429 for (
const auto& [var, coeff] : lhs.
terms()) {
2442 for (
const auto& [var, coeff] : lhs.
terms()) {
2447 for (
const auto& [var, coeff] : rhs.
terms()) {
2451 for (
const auto& [lhs_var, lhs_coeff] : lhs.
terms()) {
2452 for (
const auto& [rhs_var, rhs_coeff] : rhs.
terms()) {
2453 expr += QuadraticTerm(lhs_var, rhs_var, lhs_coeff * rhs_coeff);
2461 lhs.coefficient_ *= rhs;
2465QuadraticExpression
operator*(QuadraticExpression lhs,
const double rhs) {
2473QuadraticTerm
operator/(QuadraticTerm lhs,
const double rhs) {
2474 lhs.coefficient_ /= rhs;
2499 linear_terms_[variable] += 1;
2505 linear_terms_[term.variable] += term.coefficient;
2510 const LinearExpression& expr) {
2511 offset_ += expr.offset();
2513 if (!expr.terms().empty()) {
2515 for (
const auto& [v, coeff] : expr.terms()) {
2516 linear_terms_[v] += coeff;
2532 offset_ += expr.
offset();
2537 linear_terms_[v] += coeff;
2540 quadratic_terms_[k] += coeff;
2554 linear_terms_[variable] -= 1;
2565 const LinearExpression& expr) {
2566 offset_ -= expr.offset();
2568 if (!expr.terms().empty()) {
2570 for (
const auto& [v, coeff] : expr.terms()) {
2571 linear_terms_[v] -= coeff;
2587 offset_ -= expr.
offset();
2592 linear_terms_[v] -= coeff;
2595 quadratic_terms_[k] -= coeff;
2602 coefficient_ *= value;
2610 for (
auto& term : linear_terms_) {
2611 term.second *= value;
2613 for (
auto& term : quadratic_terms_) {
2614 term.second *= value;
2622 coefficient_ /= value;
2630 for (
auto& term : linear_terms_) {
2631 term.second /= value;
2633 for (
auto& term : quadratic_terms_) {
2634 term.second /= value;
2641template <
typename Iterable>
2643 for (
const auto& item : items) {
2648template <
typename Iterable>
2655template <
typename LeftIterable,
typename RightIterable>
2657 const RightIterable& right) {
2661template <
typename LeftIterable,
typename RightIterable>
2663 const LeftIterable& left,
const RightIterable& right) {
2677 : expression(
std::move(expression)), lower_bound(lower_bound) {}
2680 : expression(
std::move(lb_expression.expression)),
2681 lower_bound(lb_expression.lower_bound) {}
2685 : expression(
std::move(expression)), upper_bound(upper_bound) {}
2688 : expression(
std::move(ub_expression.expression)),
2689 upper_bound(ub_expression.upper_bound) {}
2693 const double upper_bound)
2694 : expression(
std::move(expression)),
2695 lower_bound(lower_bound),
2696 upper_bound(upper_bound) {}
2698 internal::VariablesEquality var_equality)
2699 : lower_bound(0), upper_bound(0) {
2700 expression += var_equality.lhs;
2707 upper_bound(
std::numeric_limits<double>::infinity()) {}
2711 lower_bound(-
std::numeric_limits<double>::infinity()),
2715 : expression(
std::move(bounded_expression.expression)),
2716 lower_bound(bounded_expression.lower_bound),
2720 : expression(
std::move(lb_expression.expression)),
2721 lower_bound(lb_expression.lower_bound),
2722 upper_bound(
std::numeric_limits<double>::infinity()) {}
2766UpperBoundedQuadraticExpression
operator<=(
const QuadraticTerm lhs,
2776BoundedQuadraticExpression
operator>=(
const double lhs,
2777 LowerBoundedQuadraticExpression rhs) {
2778 return BoundedQuadraticExpression(std::move(rhs.expression), rhs.lower_bound,
2781BoundedQuadraticExpression
operator<=(LowerBoundedQuadraticExpression lhs,
2783 return BoundedQuadraticExpression(std::move(lhs.expression), lhs.lower_bound,
2786BoundedQuadraticExpression
operator<=(
const double lhs,
2787 UpperBoundedQuadraticExpression rhs) {
2796 std::numeric_limits<double>::infinity());
2798BoundedQuadraticExpression
operator>=(QuadraticExpression lhs,
2799 const QuadraticTerm rhs) {
2801 return BoundedQuadraticExpression(std::move(lhs), 0,
2802 std::numeric_limits<double>::infinity());
2804BoundedQuadraticExpression
operator>=(QuadraticExpression lhs,
2805 const LinearExpression& rhs) {
2808 std::numeric_limits<double>::infinity());
2810BoundedQuadraticExpression
operator>=(QuadraticExpression lhs,
2811 const LinearTerm rhs) {
2814 std::numeric_limits<double>::infinity());
2816BoundedQuadraticExpression
operator>=(QuadraticExpression lhs,
2820 std::numeric_limits<double>::infinity());
2822BoundedQuadraticExpression
operator<=(QuadraticExpression lhs,
2823 const QuadraticExpression& rhs) {
2826 std::move(lhs), -std::numeric_limits<double>::infinity(), 0);
2828BoundedQuadraticExpression
operator<=(QuadraticExpression lhs,
2829 const QuadraticTerm rhs) {
2832 std::move(lhs), -std::numeric_limits<double>::infinity(), 0);
2834BoundedQuadraticExpression
operator<=(QuadraticExpression lhs,
2835 const LinearExpression& rhs) {
2838 std::move(lhs), -std::numeric_limits<double>::infinity(), 0);
2840BoundedQuadraticExpression
operator<=(QuadraticExpression lhs,
2841 const LinearTerm rhs) {
2844 std::move(lhs), -std::numeric_limits<double>::infinity(), 0);
2846BoundedQuadraticExpression
operator<=(QuadraticExpression lhs,
2850 std::move(lhs), -std::numeric_limits<double>::infinity(), 0);
2852BoundedQuadraticExpression
operator==(QuadraticExpression lhs,
2853 const QuadraticExpression& rhs) {
2857BoundedQuadraticExpression
operator==(QuadraticExpression lhs,
2858 const QuadraticTerm rhs) {
2862BoundedQuadraticExpression
operator==(QuadraticExpression lhs,
2863 const LinearExpression& rhs) {
2865 return BoundedQuadraticExpression(std::move(lhs), 0, 0);
2867BoundedQuadraticExpression
operator==(QuadraticExpression lhs,
2868 const LinearTerm rhs) {
2870 return BoundedQuadraticExpression(std::move(lhs), 0, 0);
2877BoundedQuadraticExpression
operator==(QuadraticExpression lhs,
2887 std::move(rhs), -std::numeric_limits<double>::infinity(), 0);
2889BoundedQuadraticExpression
operator>=(
const QuadraticTerm lhs,
2890 const QuadraticTerm rhs) {
2891 return BoundedQuadraticExpression(
2892 rhs - lhs, -std::numeric_limits<double>::infinity(), 0);
2894BoundedQuadraticExpression
operator>=(
const QuadraticTerm lhs,
2895 LinearExpression rhs) {
2896 return BoundedQuadraticExpression(
2897 std::move(rhs) - lhs, -std::numeric_limits<double>::infinity(), 0);
2899BoundedQuadraticExpression
operator>=(
const QuadraticTerm lhs,
2900 const LinearTerm rhs) {
2901 return BoundedQuadraticExpression(
2902 rhs - lhs, -std::numeric_limits<double>::infinity(), 0);
2904BoundedQuadraticExpression
operator>=(
const QuadraticTerm lhs,
2906 return BoundedQuadraticExpression(
2907 rhs - lhs, -std::numeric_limits<double>::infinity(), 0);
2913 std::numeric_limits<double>::infinity());
2918 std::numeric_limits<double>::infinity());
2923 std::numeric_limits<double>::infinity());
2928 std::numeric_limits<double>::infinity());
2933 std::numeric_limits<double>::infinity());
2940BoundedQuadraticExpression
operator==(
const QuadraticTerm lhs,
2944BoundedQuadraticExpression
operator==(
const QuadraticTerm lhs,
2945 LinearExpression rhs) {
2948BoundedQuadraticExpression
operator==(
const QuadraticTerm lhs,
2949 const LinearTerm rhs) {
2950 return BoundedQuadraticExpression(rhs - lhs, 0, 0);
2952BoundedQuadraticExpression
operator==(
const QuadraticTerm lhs,
2954 return BoundedQuadraticExpression(rhs - lhs, 0, 0);
2965 std::move(rhs), -std::numeric_limits<double>::infinity(), 0);
2967BoundedQuadraticExpression
operator>=(LinearExpression lhs,
2968 const QuadraticTerm rhs) {
2969 return BoundedQuadraticExpression(
2970 rhs - std::move(lhs), -std::numeric_limits<double>::infinity(), 0);
2972BoundedQuadraticExpression
operator<=(
const LinearExpression& lhs,
2973 QuadraticExpression rhs) {
2976 std::numeric_limits<double>::infinity());
2981 std::numeric_limits<double>::infinity());
2983BoundedQuadraticExpression
operator==(
const LinearExpression& lhs,
2984 QuadraticExpression rhs) {
2986 return BoundedQuadraticExpression(std::move(rhs), 0, 0);
2988BoundedQuadraticExpression
operator==(LinearExpression lhs,
2989 const QuadraticTerm rhs) {
2990 return BoundedQuadraticExpression(rhs - std::move(lhs), 0, 0);
2997 std::move(rhs), -std::numeric_limits<double>::infinity(), 0);
2999BoundedQuadraticExpression
operator>=(
const LinearTerm lhs,
3000 const QuadraticTerm rhs) {
3001 return BoundedQuadraticExpression(
3002 rhs - lhs, -std::numeric_limits<double>::infinity(), 0);
3008 std::numeric_limits<double>::infinity());
3010BoundedQuadraticExpression
operator<=(
const LinearTerm lhs,
3011 const QuadraticTerm rhs) {
3012 return BoundedQuadraticExpression(rhs - lhs, 0,
3013 std::numeric_limits<double>::infinity());
3015BoundedQuadraticExpression
operator==(
const LinearTerm lhs,
3016 QuadraticExpression rhs) {
3018 return BoundedQuadraticExpression(std::move(rhs), 0, 0);
3020BoundedQuadraticExpression
operator==(
const LinearTerm lhs,
3021 const QuadraticTerm rhs) {
3022 return BoundedQuadraticExpression(rhs - lhs, 0, 0);
3029 std::move(rhs), -std::numeric_limits<double>::infinity(), 0);
3032 const QuadraticTerm rhs) {
3033 return BoundedQuadraticExpression(
3034 rhs - lhs, -std::numeric_limits<double>::infinity(), 0);
3040 std::numeric_limits<double>::infinity());
3043 const QuadraticTerm rhs) {
3044 return BoundedQuadraticExpression(rhs - lhs, 0,
3045 std::numeric_limits<double>::infinity());
3048 QuadraticExpression rhs) {
3050 return BoundedQuadraticExpression(std::move(rhs), 0, 0);
3053 const QuadraticTerm rhs) {
3054 return BoundedQuadraticExpression(rhs - lhs, 0, 0);
3063BoundedQuadraticExpression
operator==(
const double lhs,
3064 const QuadraticTerm rhs) {
3065 return BoundedQuadraticExpression(rhs - lhs, 0, 0);
double EvaluateWithDefaultZero(const VariableMap< double > &variable_values) const
static LinearExpression InnerProduct(const LeftIterable &left, const RightIterable &right)
LinearExpression & operator*=(double value)
friend LinearExpression operator-(LinearExpression expr)
double Evaluate(const VariableMap< double > &variable_values) const
const VariableMap< double > & terms() const
Returns the terms in this expression.
void AddSum(const Iterable &items)
}
LinearExpression & operator-=(const LinearExpression &other)
LinearExpression()=default
friend std::ostream & operator<<(std::ostream &ostr, const LinearExpression &expression)
LinearExpression & operator+=(const LinearExpression &other)
void AddInnerProduct(const LeftIterable &left, const RightIterable &right)
LinearExpression & operator=(const LinearExpression &other)=default
LinearExpression(const LinearExpression &other)=default
static LinearExpression Sum(const Iterable &items)
LinearExpression & operator/=(double value)
ModelStorageElement(ModelStorageCPtr storage, IdType id)
void SetOrCheckStorage(const ModelStorageItem &item)
ModelStorageItemContainer(const NullableModelStorageCPtr storage=nullptr)
ModelStorageItemContainer & operator=(const ModelStorageItemContainer &other)=default
ModelStorageCPtr storage() const
ModelStorageItem(ModelStorageCPtr storage)
void AddInnerProduct(const LeftIterable &left, const RightIterable &right)
const QuadraticTermMap< double > & quadratic_terms() const
QuadraticExpression & operator-=(double value)
static RightIterable QuadraticExpression InnerProduct(const LeftIterable &left, const RightIterable &right)
friend std::ostream & operator<<(std::ostream &ostr, const QuadraticExpression &expr)
double EvaluateWithDefaultZero(const VariableMap< double > &variable_values) const
void AddSum(const Iterable &items)
}
const VariableMap< double > & linear_terms() const
QuadraticExpression(const QuadraticExpression &other)=default
static QuadraticExpression Sum(const Iterable &items)
QuadraticExpression & operator*=(double value)
QuadraticExpression & operator/=(double value)
double Evaluate(const VariableMap< double > &variable_values) const
QuadraticExpression()=default
friend QuadraticExpression operator-(QuadraticExpression expr)
QuadraticExpression & operator+=(double value)
QuadraticExpression & operator=(const QuadraticExpression &other)=default
Variable first() const
Returns the Variable with the smallest id.
QuadraticProductId typed_id() const
QuadraticProductId IdType
QuadraticTermKey(ModelStorageCPtr storage, QuadraticProductId id)
Variable second() const
Returns the Variable the largest id.
friend H AbslHashValue(H h, const QuadraticTermKey &key)
friend QuadraticTerm operator*(double lhs, QuadraticTerm rhs)
-------------------------— Multiplication (*) --------------------------—
friend QuadraticTerm operator-(QuadraticTerm term)
------------------------— Subtraction (-) ------------------------------—
double coefficient() const
QuadraticTerm & operator/=(double value)
QuadraticTerm & operator*=(double value)
Variable first_variable() const
QuadraticTermKey GetKey() const
This is useful for working with IdMaps.
friend QuadraticTerm operator/(QuadraticTerm lhs, double rhs)
----------------------------— Division (/) -----------------------------—
Variable second_variable() const
double lower_bound() const
double upper_bound() const
absl::string_view name() const
LinearExpression operator-() const
ModelStorageElement(ModelStorageCPtr storage, IdType id)
constexpr absl::string_view kObjectsFromOtherModelStorage
void AddInnerProduct(const LeftIterable &left, const RightIterable &right, Expression &expr)
An object oriented wrapper for quadratic constraints in ModelStorage.
absl::flat_hash_map< Variable, V > VariableMap
absl::Nonnull< const ModelStorage * > ModelStorageCPtr
LinearExpression Sum(const Iterable &items)
LinearExpression operator+(Variable lhs, double rhs)
absl::flat_hash_map< QuadraticTermKey, V > QuadraticTermMap
LinearTerm operator*(double coefficient, LinearTerm term)
bool operator==(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
bool operator!=(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
LowerBoundedLinearExpression operator>=(LinearExpression expression, double constant)
RightIterable LinearExpression InnerProduct(const LeftIterable &left, const RightIterable &right)
LowerBoundedLinearExpression operator<=(double constant, LinearExpression expression)
LinearTerm operator/(LinearTerm term, double coefficient)
LinearExpression operator-(LinearExpression expr)
std::pair< VariableId, VariableId > QuadraticProductId
Id type used for quadratic terms, i.e. products of two variables.
In SWIG mode, we don't want anything besides these top-level includes.
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)
H AbslHashValue(H h, const StrongIndex< StrongIndexName > &i)
– ABSL HASHING SUPPORT --------------------------------------------------—
ClosedInterval::Iterator end(ClosedInterval interval)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
ClosedInterval::Iterator begin(ClosedInterval interval)
A LinearExpression with upper and lower bounds.
BoundedLinearExpression(LinearExpression expression, double lower_bound, double upper_bound)
double upper_bound_minus_offset() const
LinearExpression expression
double lower_bound_minus_offset() const
A QuadraticExpression with upper and lower bounds.
BoundedQuadraticExpression(QuadraticExpression expression, double lower_bound, double upper_bound)
double upper_bound_minus_offset() const
QuadraticExpression expression
double lower_bound_minus_offset() const
A term in an sum of variables multiplied by coefficients.
LinearTerm(Variable variable, double coefficient)
LinearTerm & operator*=(double d)
LinearTerm operator-() const
LinearTerm & operator/=(double d)
A LinearExpression with a lower bound.
LinearExpression expression
LowerBoundedLinearExpression(LinearExpression expression, double lower_bound)
A QuadraticExpression with a lower bound.
LowerBoundedQuadraticExpression(QuadraticExpression expression, double lower_bound)
QuadraticExpression expression
A LinearExpression with an upper bound.
UpperBoundedLinearExpression(LinearExpression expression, double upper_bound)
LinearExpression expression
A QuadraticExpression with an upper bound.
UpperBoundedQuadraticExpression(QuadraticExpression expression, double upper_bound)
QuadraticExpression expression
VariablesEquality(Variable lhs, Variable rhs)