OR-Tools  9.6
model_util.h
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 
14 #ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_UTIL_MODEL_UTIL_H_
15 #define OR_TOOLS_MATH_OPT_CONSTRAINTS_UTIL_MODEL_UTIL_H_
16 
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/algorithm/container.h"
21 #include "absl/strings/string_view.h"
25 
27 
28 // A default way to describe a constraint that has been deleted from its
29 // associated model.
30 constexpr absl::string_view kDeletedConstraintDefaultDescription =
31  "[constraint deleted from model]";
32 
33 // Converts data from "raw ID" format to a LinearExpression, in the C++ API,
34 // associated with `storage`.
36  const SparseCoefficientMap& coeffs,
37  double offset);
38 
39 // Converts a `LinearExpression` to the associated "raw ID" format.
40 std::pair<SparseCoefficientMap, double> FromLinearExpression(
41  const LinearExpression& expression);
42 
43 template <typename IdType>
44 std::vector<Variable> AtomicConstraintNonzeroVariables(
45  const ModelStorage& storage, const IdType id) {
46  const std::vector<VariableId> raw_vars =
47  storage.constraint_data(id).RelatedVariables();
48  std::vector<Variable> vars;
49  vars.reserve(raw_vars.size());
50  for (const VariableId raw_var : raw_vars) {
51  vars.push_back(Variable(&storage, raw_var));
52  }
53  return vars;
54 }
55 
56 // Duck-types on `ConstraintType` having a typedef for the associated `IdType`,
57 // and having a `(const ModelStorage*, IdType)` constructor.
58 template <typename ConstraintType>
59 std::vector<ConstraintType> AtomicConstraints(const ModelStorage& storage) {
60  using IdType = typename ConstraintType::IdType;
61  std::vector<ConstraintType> result;
62  result.reserve(storage.num_constraints<IdType>());
63  for (const IdType con_id : storage.Constraints<IdType>()) {
64  result.push_back(ConstraintType(&storage, con_id));
65  }
66  return result;
67 }
68 
69 // * Duck-types on `ConstraintType` having a `typed_id()` getter.
70 template <typename ConstraintType>
71 std::vector<ConstraintType> SortedAtomicConstraints(
72  const ModelStorage& storage) {
73  std::vector<ConstraintType> constraints =
74  AtomicConstraints<ConstraintType>(storage);
75  absl::c_sort(constraints,
76  [](const ConstraintType& l, const ConstraintType& r) {
77  return l.typed_id() < r.typed_id();
78  });
79  return constraints;
80 }
81 
82 } // namespace operations_research::math_opt
83 
84 #endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_UTIL_MODEL_UTIL_H_
const AtomicConstraintTraits< IdType >::ConstraintData & constraint_data(IdType id) const
std::vector< IdType > Constraints() const
LinearExpression ToLinearExpression(const ModelStorage &storage, const SparseCoefficientMap &coeffs, const double offset)
Definition: model_util.cc:23
std::vector< ConstraintType > AtomicConstraints(const ModelStorage &storage)
Definition: model_util.h:59
std::vector< Variable > AtomicConstraintNonzeroVariables(const ModelStorage &storage, const IdType id)
Definition: model_util.h:44
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition: model_util.h:30
std::vector< ConstraintType > SortedAtomicConstraints(const ModelStorage &storage)
Definition: model_util.h:71
std::pair< SparseCoefficientMap, double > FromLinearExpression(const LinearExpression &expression)
Definition: model_util.cc:33