22 #include "absl/strings/str_join.h"
37 for (
const auto& kv : rhs.terms_) {
38 terms_[kv.first] += kv.second;
40 offset_ += rhs.offset_;
45 for (
const auto& kv : rhs.terms_) {
46 terms_[kv.first] -= kv.second;
48 offset_ -= rhs.offset_;
56 }
else if (rhs != 1) {
57 for (
auto& kv : terms_) {
67 return (*
this) *= 1 / rhs;
80 double solution = offset_;
81 for (
const auto& pair : terms_) {
82 solution += pair.first->solution_value() * pair.second;
89 void AppendTerm(
const double coef,
const std::string& var_name,
90 const bool is_first, std::string* s) {
93 absl::StrAppend(s, var_name);
94 }
else if (
coef == -1.0) {
95 absl::StrAppend(s,
"-", var_name);
97 absl::StrAppend(s,
coef,
"*", var_name);
100 const std::string op =
coef < 0 ?
"-" :
"+";
101 const double abs_coef = std::abs(
coef);
102 if (abs_coef == 1.0) {
103 absl::StrAppend(s,
" ", op,
" ", var_name);
105 absl::StrAppend(s,
" ", op,
" ", abs_coef,
"*", var_name);
110 void AppendOffset(
const double offset,
const bool is_first, std::string* s) {
112 absl::StrAppend(s, offset);
115 const std::string op = offset < 0 ?
"-" :
"+";
116 absl::StrAppend(s,
" ", op,
" ", std::abs(offset));
124 std::vector<const MPVariable*> vars_in_order;
125 for (
const auto& var_val_pair : terms_) {
126 vars_in_order.push_back(var_val_pair.first);
128 std::sort(vars_in_order.begin(), vars_in_order.end(),
130 return v->index() < u->index();
133 bool is_first =
true;
136 DCHECK(!
var->name().empty());
137 AppendTerm(terms_.at(
var),
var->name(), is_first, &result);
140 AppendOffset(offset_, is_first, &result);
174 linear_expr_(linear_expr),
176 lower_bound_ -= linear_expr_.
offset();
177 upper_bound_ -= linear_expr_.
offset();
178 linear_expr_ -= linear_expr_.
offset();
182 return LinearRange(-std::numeric_limits<double>::infinity(), lhs - rhs, 0);
188 return LinearRange(0, lhs - rhs, std::numeric_limits<double>::infinity());
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
double SolutionValue() const
Evaluates the value of this expression at the solution found.
std::string ToString() const
A human readable representation of this.
LinearExpr operator-() const
LinearExpr & operator*=(double rhs)
LinearExpr & operator+=(const LinearExpr &rhs)
LinearExpr & operator-=(const LinearExpr &rhs)
LinearExpr & operator/=(double rhs)
static LinearExpr NotVar(LinearExpr var)
Returns 1-var.
An expression of the form:
The class for variables of a Mathematical Programming (MP) model.
This file allows you to write natural code (like a mathematical equation) to model optimization probl...
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
Collection of objects used to extend the Constraint Solver library.
LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
LinearExpr operator*(LinearExpr lhs, double rhs)
LinearExpr operator/(LinearExpr lhs, double rhs)
LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)
LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)
LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)