OR-Tools  8.1
linear_expr.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_
15 #define OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_
16 
79 #include <ostream>
80 #include <string>
81 
82 #include "absl/container/flat_hash_map.h"
83 
84 namespace operations_research {
85 
86 // NOTE(user): forward declaration is necessary due to cyclic dependencies,
87 // MPVariable is defined in linear_solver.h, which depends on LinearExpr.
88 class MPVariable;
89 
114 class LinearExpr {
115  public:
116  LinearExpr();
118  LinearExpr(double constant); // NOLINT
119 
120  /***
121  * Possible implicit conversions are intentional.
122  *
123  * Warning: var is not owned.
124  */
125  LinearExpr(const MPVariable* var); // NOLINT
126 
135 
136  LinearExpr& operator+=(const LinearExpr& rhs);
137  LinearExpr& operator-=(const LinearExpr& rhs);
138  LinearExpr& operator*=(double rhs);
139  LinearExpr& operator/=(double rhs);
140  LinearExpr operator-() const;
141 
142  double offset() const { return offset_; }
143  const absl::flat_hash_map<const MPVariable*, double>& terms() const {
144  return terms_;
145  }
146 
152  double SolutionValue() const;
153 
158  std::string ToString() const;
159 
160  private:
161  double offset_;
162  absl::flat_hash_map<const MPVariable*, double> terms_;
163 };
164 
165 std::ostream& operator<<(std::ostream& stream, const LinearExpr& linear_expr);
166 
167 // NOTE(user): in the ops below, the non-"const LinearExpr&" are intentional.
168 // We need to create a new LinearExpr for the result, so we lose nothing by
169 // passing one argument by value, mutating it, and then returning it. In
170 // particular, this allows (with move semantics and RVO) an optimized
171 // evaluation of expressions such as
172 // a + b + c + d
173 // (see http://en.cppreference.com/w/cpp/language/operators).
174 LinearExpr operator+(LinearExpr lhs, const LinearExpr& rhs);
175 LinearExpr operator-(LinearExpr lhs, const LinearExpr& rhs);
176 LinearExpr operator*(LinearExpr lhs, double rhs);
177 LinearExpr operator/(LinearExpr lhs, double rhs);
178 LinearExpr operator*(double lhs, LinearExpr rhs);
179 
192 class LinearRange {
193  public:
194  LinearRange() : lower_bound_(0), upper_bound_(0) {}
203  double upper_bound);
204 
205  double lower_bound() const { return lower_bound_; }
206  const LinearExpr& linear_expr() const { return linear_expr_; }
207  double upper_bound() const { return upper_bound_; }
208 
209  private:
210  double lower_bound_;
211  // invariant: linear_expr_.offset() == 0.
212  LinearExpr linear_expr_;
213  double upper_bound_;
214 };
215 
216 LinearRange operator<=(const LinearExpr& lhs, const LinearExpr& rhs);
217 LinearRange operator==(const LinearExpr& lhs, const LinearExpr& rhs);
218 LinearRange operator>=(const LinearExpr& lhs, const LinearExpr& rhs);
219 
220 // TODO(user,user): explore defining more overloads to support:
221 // solver.AddRowConstraint(0.0 <= x + y + z <= 1.0);
222 
223 } // namespace operations_research
224 
225 #endif // OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_
operations_research::operator<=
LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:177
var
IntVar * var
Definition: expr_array.cc:1858
operations_research::LinearExpr
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
operations_research::operator==
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:180
operations_research::LinearExpr::offset
double offset() const
Definition: linear_expr.h:142
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::operator-
LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:150
operations_research::LinearExpr::NotVar
static LinearExpr NotVar(LinearExpr var)
Returns 1-var.
Definition: linear_expr.cc:69
operations_research::LinearExpr::LinearExpr
LinearExpr()
Definition: linear_expr.cc:26
operations_research::LinearRange::lower_bound
double lower_bound() const
Definition: linear_expr.h:205
operations_research::LinearExpr::SolutionValue
double SolutionValue() const
Evaluates the value of this expression at the solution found.
Definition: linear_expr.cc:75
operations_research::operator<<
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
Definition: constraint_solver/assignment.cc:1089
operations_research::operator*
LinearExpr operator*(LinearExpr lhs, double rhs)
Definition: linear_expr.cc:154
operations_research::LinearRange
An expression of the form:
Definition: linear_expr.h:192
operations_research::operator/
LinearExpr operator/(LinearExpr lhs, double rhs)
Definition: linear_expr.cc:158
operations_research::LinearExpr::operator/=
LinearExpr & operator/=(double rhs)
Definition: linear_expr.cc:61
operations_research::LinearExpr::terms
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Definition: linear_expr.h:143
operations_research::LinearExpr::operator-
LinearExpr operator-() const
Definition: linear_expr.cc:66
operations_research::LinearRange::LinearRange
LinearRange()
Definition: linear_expr.h:194
operations_research::MPVariable
The class for variables of a Mathematical Programming (MP) model.
Definition: linear_solver.h:1052
operations_research::operator>=
LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:183
operations_research::LinearExpr::operator-=
LinearExpr & operator-=(const LinearExpr &rhs)
Definition: linear_expr.cc:40
operations_research::LinearRange::upper_bound
double upper_bound() const
Definition: linear_expr.h:207
operations_research::LinearRange::linear_expr
const LinearExpr & linear_expr() const
Definition: linear_expr.h:206
operations_research::LinearExpr::ToString
std::string ToString() const
A human readable representation of this.
Definition: linear_expr.cc:119
operations_research::operator+
LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:146
operations_research::LinearExpr::operator*=
LinearExpr & operator*=(double rhs)
Definition: linear_expr.cc:48
operations_research::LinearExpr::operator+=
LinearExpr & operator+=(const LinearExpr &rhs)
Definition: linear_expr.cc:32