79 #ifndef OR_TOOLS_MATH_OPT_CPP_VARIABLE_AND_EXPRESSIONS_H_ 80 #define OR_TOOLS_MATH_OPT_CPP_VARIABLE_AND_EXPRESSIONS_H_ 84 #include <initializer_list> 91 #include "absl/container/flat_hash_map.h" 113 inline int64_t
id()
const;
121 inline const std::string&
name()
const;
129 template <
typename H>
140 template <
typename V>
184 #ifndef USE_LINEAR_EXPRESSION_COUNTERS 186 #else // USE_LINEAR_EXPRESSION_COUNTERS 191 #endif // USE_LINEAR_EXPRESSION_COUNTERS 238 template <
typename Iterable>
239 inline void AddSum(
const Iterable& items);
277 template <
typename LeftIterable,
typename RightIterable>
279 const RightIterable& right);
283 inline double offset()
const;
300 inline const absl::flat_hash_map<VariableId, double>&
raw_terms()
const;
302 #ifdef USE_LINEAR_EXPRESSION_COUNTERS 303 static thread_local
int num_calls_default_constructor_;
304 static thread_local
int num_calls_copy_constructor_;
305 static thread_local
int num_calls_move_constructor_;
306 static thread_local
int num_calls_initializer_list_constructor_;
308 static void ResetCounters();
309 #endif // USE_LINEAR_EXPRESSION_COUNTERS 313 friend std::ostream&
operator<<(std::ostream& ostr,
317 double offset_ = 0.0;
340 template <
typename Iterable>
366 template <
typename LeftIterable,
typename RightIterable>
368 const RightIterable& right);
431 inline operator bool()
const;
593 : model_(
model), id_(id) {
624 template <
typename H>
626 return H::combine(std::move(h), variable.id_.value(), variable.model_);
632 ostr << variable.
name();
691 #ifdef USE_LINEAR_EXPRESSION_COUNTERS 692 ++num_calls_initializer_list_constructor_;
693 #endif // USE_LINEAR_EXPRESSION_COUNTERS 694 for (
const auto& term :
terms) {
697 terms_[term.variable] += term.coefficient;
711 expr.offset_ = -expr.offset_;
712 for (
auto term : expr.terms_) {
713 term.second = -term.second;
821 auto ret = -std::move(rhs);
840 auto ret = -std::move(rhs);
866 terms_.Add(other.terms_);
867 offset_ += other.offset_;
886 terms_.Subtract(other.terms_);
887 offset_ -= other.offset_;
907 for (
auto term : terms_) {
908 term.second *=
value;
915 for (
auto term : terms_) {
916 term.second /=
value;
921 template <
typename Iterable>
923 for (
const auto& item : items) {
928 template <
typename Iterable>
935 template <
typename LeftIterable,
typename RightIterable>
937 const RightIterable& right) {
940 auto l = begin(left);
941 auto r = begin(right);
942 auto l_end = end(left);
943 auto r_end = end(right);
944 for (; l != l_end && r != r_end; ++l, ++r) {
945 *
this += (*l) * (*r);
948 <<
"left had more elements than right, sizes should be equal";
950 <<
"right had more elements than left, sizes should be equal";
953 template <
typename LeftIterable,
typename RightIterable>
955 const RightIterable& right) {
969 return terms_.raw_map();
979 : lhs(std::move(lhs)), rhs(std::move(rhs)) {}
981 inline VariablesEquality::operator bool()
const {
982 return lhs.
typed_id() == rhs.typed_id() && lhs.model() == rhs.model();
993 return !(lhs == rhs);
1013 : expression(std::move(expression)),
1019 : expression({{eq.
lhs, 1.0}, {eq.
rhs, -1.0}}, 0.0),
1025 : expression(std::move(lb_expression.expression)),
1027 upper_bound(std::numeric_limits<double>::infinity()) {}
1031 : expression(std::move(ub_expression.expression)),
1032 lower_bound(-std::numeric_limits<double>::infinity()),
1044 const double constant) {
1054 const double constant) {
1064 const double constant) {
1065 return LinearTerm(variable, 1.0) >= constant;
1070 return constant <=
LinearTerm(variable, 1.0);
1074 const double constant) {
1084 const double constant) {
1094 const double constant) {
1095 return LinearTerm(variable, 1.0) <= constant;
1100 return constant >=
LinearTerm(variable, 1.0);
1135 std::move(lhs), -std::numeric_limits<double>::infinity(),
1143 std::move(lhs), 0.0,
1144 std::numeric_limits<double>::infinity());
1151 std::move(lhs), -std::numeric_limits<double>::infinity(),
1159 std::move(lhs), 0.0,
1160 std::numeric_limits<double>::infinity());
1167 std::move(rhs), 0.0,
1168 std::numeric_limits<double>::infinity());
1175 std::move(rhs), -std::numeric_limits<double>::infinity(),
1180 return std::move(lhs) <=
LinearTerm(rhs, 1.0);
1184 return std::move(lhs) >=
LinearTerm(rhs, 1.0);
1188 return LinearTerm(lhs, 1.0) <= std::move(rhs);
1192 return LinearTerm(lhs, 1.0) >= std::move(rhs);
1199 -std::numeric_limits<double>::infinity(),
1207 std::numeric_limits<double>::infinity());
1256 return std::move(lhs) ==
LinearTerm(rhs, 1.0);
1260 return LinearTerm(lhs, 1.0) == std::move(rhs);
1311 #endif // OR_TOOLS_MATH_OPT_CPP_VARIABLE_AND_EXPRESSIONS_H_ LinearTerm & operator *=(double d)
LowerBoundedLinearExpression operator>=(LinearExpression expression, double constant)
double Evaluate(const VariableMap< double > &variable_values) const
LinearExpression & operator *=(double value)
LinearTerm operator/(LinearTerm term, double coefficient)
const std::string & variable_name(VariableId id) const
LinearTerm(Variable variable, double coefficient)
bool operator==(const LinearConstraint &lhs, const LinearConstraint &rhs)
void AddSum(const Iterable &items)
LinearExpression operator-(LinearExpression expr)
friend H AbslHashValue(H h, const Variable &variable)
const absl::flat_hash_map< VariableId, double > & raw_terms() const
bool operator!=(const LinearConstraint &lhs, const LinearConstraint &rhs)
double lower_bound_minus_offset() const
friend LinearExpression operator-(LinearExpression expr)
void set_upper_bound(double upper_bound) const
std::ostream & operator<<(std::ostream &ostr, const LinearConstraint &linear_constraint)
void set_is_integer(bool is_integer) const
LinearExpression expression
IndexedModel * model() const
Variable(IndexedModel *model, VariableId id)
LinearExpression expression
void set_variable_lower_bound(VariableId id, double lower_bound)
LowerBoundedLinearExpression(LinearExpression expression, double lower_bound)
double upper_bound() const
void set_continuous() const
IndexedModel * model() const
VariablesEquality(Variable lhs, Variable rhs)
LinearExpression()=default
BoundedLinearExpression(LinearExpression expression, double lower_bound, double upper_bound)
LinearExpression & operator/=(double value)
H AbslHashValue(H h, const LinearConstraint &linear_constraint)
LinearExpression expression
double variable_lower_bound(VariableId id) const
VariableId typed_id() const
LinearExpression operator+(Variable lhs, double rhs)
#define DCHECK(condition)
bool is_variable_integer(VariableId id) const
double upper_bound_minus_offset() const
void set_variable_is_integer(VariableId id, bool is_integer)
double lower_bound() const
const std::string & name() const
UpperBoundedLinearExpression(LinearExpression expression, double upper_bound)
void set_variable_upper_bound(VariableId id, double upper_bound)
const VariableMap< double > & terms() const
Collection of objects used to extend the Constraint Solver library.
LinearTerm operator-() const
LinearExpression Sum(const Iterable &items)
LinearExpression & operator-=(const LinearExpression &other)
void AddInnerProduct(const LeftIterable &left, const RightIterable &right)
double EvaluateWithDefaultZero(const VariableMap< double > &variable_values) const
friend std::ostream & operator<<(std::ostream &ostr, const LinearExpression &expression)
void set_lower_bound(double lower_bound) const
friend std::ostream & operator<<(std::ostream &ostr, const Variable &variable)
LinearTerm operator *(double coefficient, LinearTerm term)
LinearTerm & operator/=(double d)
double variable_upper_bound(VariableId id) const
LinearExpression & operator+=(const LinearExpression &other)
LinearExpression InnerProduct(const LeftIterable &left, const RightIterable &right)
LowerBoundedLinearExpression operator<=(double constant, LinearExpression expression)