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 
80 #include "absl/container/flat_hash_map.h"
81 
83 
84 // NOTE(user): forward declaration is necessary due to cyclic dependencies,
85 // MPVariable is defined in linear_solver.h, which depends on LinearExpr.
86 class MPVariable;
87 
112 class LinearExpr {
113  public:
114  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 
134  static LinearExpr NotVar(LinearExpr var);
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 
154  private:
155  double offset_;
156  absl::flat_hash_map<const MPVariable*, double> terms_;
157 };
158 
159 // NOTE(user): in the ops below, the non-"const LinearExpr&" are intentional.
160 // We need to create a new LinearExpr for the result, so we lose nothing by
161 // passing one argument by value, mutating it, and then returning it. In
162 // particular, this allows (with move semantics and RVO) an optimized
163 // evaluation of expressions such as
164 // a + b + c + d
165 // (see http://en.cppreference.com/w/cpp/language/operators).
166 LinearExpr operator+(LinearExpr lhs, const LinearExpr& rhs);
167 LinearExpr operator-(LinearExpr lhs, const LinearExpr& rhs);
168 LinearExpr operator*(LinearExpr lhs, double rhs);
169 LinearExpr operator/(LinearExpr lhs, double rhs);
170 LinearExpr operator*(double lhs, LinearExpr rhs);
171 
182 class LinearRange {
183  public:
184  LinearRange() : lower_bound_(0), upper_bound_(0) {}
185  // The bounds of the linear range are updated so that they include the offset
186  // from "linear_expr", i.e., we form the range:
187  // lower_bound - offset <= linear_expr - offset <= upper_bound - offset.
189  double upper_bound);
190 
191  double lower_bound() const { return lower_bound_; }
192  const LinearExpr& linear_expr() const { return linear_expr_; }
193  double upper_bound() const { return upper_bound_; }
194 
195  private:
196  double lower_bound_;
197  // invariant: linear_expr_.offset() == 0.
198  LinearExpr linear_expr_;
199  double upper_bound_;
200 };
201 
202 LinearRange operator<=(const LinearExpr& lhs, const LinearExpr& rhs);
203 LinearRange operator==(const LinearExpr& lhs, const LinearExpr& rhs);
204 LinearRange operator>=(const LinearExpr& lhs, const LinearExpr& rhs);
205 
206 // TODO(user,user): explore defining more overloads to support:
207 // solver.AddRowConstraint(0.0 <= x + y + z <= 1.0);
208 
209 } // namespace operations_research
210 
211 #endif // OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_
const LinearExpr & linear_expr() const
Definition: linear_expr.h:192
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:182
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:112
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Definition: linear_expr.h:143
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)