OR-Tools  8.2
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_
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
double SolutionValue() const
Evaluates the value of this expression at the solution found.
Definition: linear_expr.cc:75
std::string ToString() const
A human readable representation of this.
Definition: linear_expr.cc:119
LinearExpr operator-() const
Definition: linear_expr.cc:66
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Definition: linear_expr.h:143
LinearExpr & operator*=(double rhs)
Definition: linear_expr.cc:48
LinearExpr & operator+=(const LinearExpr &rhs)
Definition: linear_expr.cc:32
LinearExpr & operator-=(const LinearExpr &rhs)
Definition: linear_expr.cc:40
LinearExpr & operator/=(double rhs)
Definition: linear_expr.cc:61
static LinearExpr NotVar(LinearExpr var)
Returns 1-var.
Definition: linear_expr.cc:69
An expression of the form:
Definition: linear_expr.h:192
const LinearExpr & linear_expr() const
Definition: linear_expr.h:206
The class for variables of a Mathematical Programming (MP) model.
IntVar * var
Definition: expr_array.cc:1858
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:146
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
LinearExpr operator*(LinearExpr lhs, double rhs)
Definition: linear_expr.cc:154
LinearExpr operator/(LinearExpr lhs, double rhs)
Definition: linear_expr.cc:158
LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:150
LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:183
LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:177
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: linear_expr.cc:180