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 
78 #include "absl/container/flat_hash_map.h"
79 
81 
82 // NOTE(user): forward declaration is necessary due to cyclic dependencies,
83 // MPVariable is defined in linear_solver.h, which depends on LinearExpr.
84 class MPVariable;
85 
110 class LinearExpr {
111  public:
112  LinearExpr();
116  LinearExpr(double constant); // NOLINT
117 
118  /***
119  * Possible implicit conversions are intentional.
120  *
121  * Warning: var is not owned.
122  */
123  LinearExpr(const MPVariable* var); // NOLINT
124 
132  static LinearExpr NotVar(LinearExpr var);
133 
134  LinearExpr& operator+=(const LinearExpr& rhs);
135  LinearExpr& operator-=(const LinearExpr& rhs);
136  LinearExpr& operator*=(double rhs);
137  LinearExpr& operator/=(double rhs);
138  LinearExpr operator-() const;
139 
140  double offset() const { return offset_; }
141  const absl::flat_hash_map<const MPVariable*, double>& terms() const {
142  return terms_;
143  }
144 
150  double SolutionValue() const;
151 
152  private:
153  double offset_;
154  absl::flat_hash_map<const MPVariable*, double> terms_;
155 };
156 
157 // NOTE(user): in the ops below, the non-"const LinearExpr&" are intentional.
158 // We need to create a new LinearExpr for the result, so we lose nothing by
159 // passing one argument by value, mutating it, and then returning it. In
160 // particular, this allows (with move semantics and RVO) an optimized
161 // evaluation of expressions such as
162 // a + b + c + d
163 // (see http://en.cppreference.com/w/cpp/language/operators).
164 LinearExpr operator+(LinearExpr lhs, const LinearExpr& rhs);
165 LinearExpr operator-(LinearExpr lhs, const LinearExpr& rhs);
166 LinearExpr operator*(LinearExpr lhs, double rhs);
167 LinearExpr operator/(LinearExpr lhs, double rhs);
168 LinearExpr operator*(double lhs, LinearExpr rhs);
169 
180 class LinearRange {
181  public:
182  LinearRange() : lower_bound_(0), upper_bound_(0) {}
183  // The bounds of the linear range are updated so that they include the offset
184  // from "linear_expr", i.e., we form the range:
185  // lower_bound - offset <= linear_expr - offset <= upper_bound - offset.
187  double upper_bound);
188 
189  double lower_bound() const { return lower_bound_; }
190  const LinearExpr& linear_expr() const { return linear_expr_; }
191  double upper_bound() const { return upper_bound_; }
192 
193  private:
194  double lower_bound_;
195  // invariant: linear_expr_.offset() == 0.
196  LinearExpr linear_expr_;
197  double upper_bound_;
198 };
199 
200 LinearRange operator<=(const LinearExpr& lhs, const LinearExpr& rhs);
201 LinearRange operator==(const LinearExpr& lhs, const LinearExpr& rhs);
202 LinearRange operator>=(const LinearExpr& lhs, const LinearExpr& rhs);
203 
204 // TODO(user,user): explore defining more overloads to support:
205 // solver.AddRowConstraint(0.0 <= x + y + z <= 1.0);
206 
207 } // namespace operations_research
208 
209 #endif // OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_
const LinearExpr & linear_expr() const
Definition: linear_expr.h:190
LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)
LinearExpr operator *(LinearExpr lhs, double rhs)
LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)
An expression of the form:
Definition: linear_expr.h:180
double SolutionValue() const
Evaluates the value of this expression at the solution found.
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)
LinearExpr & operator *=(double rhs)
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:110
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Definition: linear_expr.h:141
LinearExpr & operator+=(const LinearExpr &rhs)
LinearExpr & operator/=(double rhs)
LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)
LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)
The class for variables of a Mathematical Programming (MP) model.
static LinearExpr NotVar(LinearExpr var)
Returns 1-var.
LinearExpr & operator-=(const LinearExpr &rhs)
LinearExpr operator-() const
LinearExpr operator/(LinearExpr lhs, double rhs)