OR-Tools  9.6
model_util.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
15 
16 #include <utility>
17 
20 
22 
24  const SparseCoefficientMap& coeffs,
25  const double offset) {
26  LinearExpression expr = offset;
27  for (const auto [var_id, coeff] : coeffs.terms()) {
28  expr += coeff * Variable(&storage, var_id);
29  }
30  return expr;
31 }
32 
33 std::pair<SparseCoefficientMap, double> FromLinearExpression(
34  const LinearExpression& expression) {
35  SparseCoefficientMap coeffs;
36  for (const auto [var, coeff] : expression.terms()) {
37  coeffs.set(var.typed_id(), coeff);
38  }
39  return {coeffs, expression.offset()};
40 }
41 
42 } // namespace operations_research::math_opt
const absl::flat_hash_map< VariableId, double > & terms() const
bool set(const VariableId id, const double coeff)
IntVar * var
Definition: expr_array.cc:1874
LinearExpression ToLinearExpression(const ModelStorage &storage, const SparseCoefficientMap &coeffs, const double offset)
Definition: model_util.cc:23
std::pair< SparseCoefficientMap, double > FromLinearExpression(const LinearExpression &expression)
Definition: model_util.cc:33