23#include "absl/strings/str_join.h"
24#include "absl/strings/string_view.h"
39 for (
const auto& kv : rhs.terms_) {
40 terms_[kv.first] += kv.second;
42 offset_ += rhs.offset_;
47 for (
const auto& kv : rhs.terms_) {
48 terms_[kv.first] -= kv.second;
50 offset_ -= rhs.offset_;
58 }
else if (rhs != 1) {
59 for (
auto& kv : terms_) {
69 return (*
this) *= 1 / rhs;
83 for (
const auto& pair : terms_) {
84 solution += pair.first->solution_value() * pair.second;
91void AppendTerm(
const double coef, absl::string_view var_name,
92 const bool is_first, std::string* s) {
95 absl::StrAppend(s, var_name);
96 }
else if (
coef == -1.0) {
97 absl::StrAppend(s,
"-", var_name);
99 absl::StrAppend(s,
coef,
"*", var_name);
102 const std::string op =
coef < 0 ?
"-" :
"+";
103 const double abs_coef = std::abs(
coef);
104 if (abs_coef == 1.0) {
105 absl::StrAppend(s,
" ", op,
" ", var_name);
107 absl::StrAppend(s,
" ", op,
" ", abs_coef,
"*", var_name);
112void AppendOffset(
const double offset,
const bool is_first, std::string* s) {
114 absl::StrAppend(s, offset);
117 const std::string op = offset < 0 ?
"-" :
"+";
118 absl::StrAppend(s,
" ", op,
" ", std::abs(offset));
126 std::vector<const MPVariable*> vars_in_order;
127 for (
const auto& var_val_pair : terms_) {
128 vars_in_order.push_back(var_val_pair.first);
130 std::sort(vars_in_order.begin(), vars_in_order.end(),
132 return v->index() < u->index();
135 bool is_first =
true;
138 DCHECK(!
var->name().empty());
139 AppendTerm(terms_.at(
var),
var->name(), is_first, &result);
142 AppendOffset(offset_, is_first, &result);
176 linear_expr_(linear_expr),
178 lower_bound_ -= linear_expr_.
offset();
179 upper_bound_ -= linear_expr_.
offset();
180 linear_expr_ -= linear_expr_.
offset();
184 return LinearRange(-std::numeric_limits<double>::infinity(), lhs - rhs, 0);
190 return LinearRange(0, lhs - rhs, std::numeric_limits<double>::infinity());
double SolutionValue() const
LinearExpr operator-() const
LinearExpr & operator/=(double rhs)
std::string ToString() const
LinearExpr & operator+=(const LinearExpr &rhs)
static LinearExpr NotVar(LinearExpr var)
static
LinearExpr & operator-=(const LinearExpr &rhs)
LinearExpr & operator*=(double rhs)
The class for variables of a Mathematical Programming (MP) model.
In SWIG mode, we don't want anything besides these top-level includes.
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)
LinearExpr operator*(LinearExpr lhs, double rhs)
LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)
LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)
LinearExpr operator/(LinearExpr lhs, double rhs)
LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)