Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
linear_expr.h
Go to the documentation of this file.
1// Copyright 2010-2024 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
84namespace 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.
88class MPVariable;
89
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
165std::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).
174LinearExpr operator+(LinearExpr lhs, const LinearExpr& rhs);
175LinearExpr operator-(LinearExpr lhs, const LinearExpr& rhs);
176LinearExpr operator*(LinearExpr lhs, double rhs);
177LinearExpr operator/(LinearExpr lhs, double rhs);
178LinearExpr operator*(double lhs, LinearExpr rhs);
179
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
216LinearRange operator<=(const LinearExpr& lhs, const LinearExpr& rhs);
217LinearRange operator==(const LinearExpr& lhs, const LinearExpr& rhs);
218LinearRange operator>=(const LinearExpr& lhs, const LinearExpr& rhs);
219
220// TODO(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 & operator/=(double rhs)
const absl::flat_hash_map< const MPVariable *, double > & terms() const
LinearExpr & operator+=(const LinearExpr &rhs)
static LinearExpr NotVar(LinearExpr var)
static
LinearExpr & operator-=(const LinearExpr &rhs)
LinearExpr & operator*=(double rhs)
const LinearExpr & linear_expr() const
The class for variables of a Mathematical Programming (MP) model.
IntVar * var
In SWIG mode, we don't want anything besides these top-level includes.
LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)
LinearExpr operator*(LinearExpr lhs, double rhs)
LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)
LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)
LinearExpr operator/(LinearExpr lhs, double rhs)
LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)