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();
114  LinearExpr(double constant); // NOLINT
115 
116  /***
117  * Possible implicit conversions are intentional.
118  *
119  * Warning: var is not owned.
120  */
121  LinearExpr(const MPVariable* var); // NOLINT
122 
130  static LinearExpr NotVar(LinearExpr var);
131 
132  LinearExpr& operator+=(const LinearExpr& rhs);
133  LinearExpr& operator-=(const LinearExpr& rhs);
134  LinearExpr& operator*=(double rhs);
135  LinearExpr& operator/=(double rhs);
136  LinearExpr operator-() const;
137 
138  double offset() const { return offset_; }
139  const absl::flat_hash_map<const MPVariable*, double>& terms() const {
140  return terms_;
141  }
142 
148  double SolutionValue() const;
149 
150  private:
151  double offset_;
152  absl::flat_hash_map<const MPVariable*, double> terms_;
153 };
154 
155 // NOTE(user): in the ops below, the non-"const LinearExpr&" are intentional.
156 // We need to create a new LinearExpr for the result, so we lose nothing by
157 // passing one argument by value, mutating it, and then returning it. In
158 // particular, this allows (with move semantics and RVO) an optimized
159 // evaluation of expressions such as
160 // a + b + c + d
161 // (see http://en.cppreference.com/w/cpp/language/operators).
162 LinearExpr operator+(LinearExpr lhs, const LinearExpr& rhs);
163 LinearExpr operator-(LinearExpr lhs, const LinearExpr& rhs);
164 LinearExpr operator*(LinearExpr lhs, double rhs);
165 LinearExpr operator/(LinearExpr lhs, double rhs);
166 LinearExpr operator*(double lhs, LinearExpr rhs);
167 
180 class LinearRange {
181  public:
182  LinearRange() : lower_bound_(0), upper_bound_(0) {}
191  double upper_bound);
192 
193  double lower_bound() const { return lower_bound_; }
194  const LinearExpr& linear_expr() const { return linear_expr_; }
195  double upper_bound() const { return upper_bound_; }
196 
197  private:
198  double lower_bound_;
199  // invariant: linear_expr_.offset() == 0.
200  LinearExpr linear_expr_;
201  double upper_bound_;
202 };
203 
204 LinearRange operator<=(const LinearExpr& lhs, const LinearExpr& rhs);
205 LinearRange operator==(const LinearExpr& lhs, const LinearExpr& rhs);
206 LinearRange operator>=(const LinearExpr& lhs, const LinearExpr& rhs);
207 
208 // TODO(user,user): explore defining more overloads to support:
209 // solver.AddRowConstraint(0.0 <= x + y + z <= 1.0);
210 
211 } // namespace operations_research
212 
213 #endif // OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_
const LinearExpr & linear_expr() const
Definition: linear_expr.h:194
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:139
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)