Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model_util.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_MATH_OPT_CONSTRAINTS_UTIL_MODEL_UTIL_H_
15#define OR_TOOLS_MATH_OPT_CONSTRAINTS_UTIL_MODEL_UTIL_H_
16
17#include <vector>
18
19#include "absl/algorithm/container.h"
20#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.
30constexpr 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`.
35LinearExpression ToLinearExpression(const ModelStorage& storage,
36 const LinearExpressionData& expr_data);
37
38// Converts a `LinearExpression` to the associated "raw ID" format.
40
41template <typename IdType>
43 const ModelStorage& storage, const IdType id) {
44 const std::vector<VariableId> raw_vars =
45 storage.constraint_data(id).RelatedVariables();
46 std::vector<Variable> vars;
47 vars.reserve(raw_vars.size());
48 for (const VariableId raw_var : raw_vars) {
49 vars.push_back(Variable(&storage, raw_var));
50 }
51 return vars;
52}
53
54// Duck-types on `ConstraintType` having a typedef for the associated `IdType`,
55// and having a `(const ModelStorage*, IdType)` constructor.
56template <typename ConstraintType>
57std::vector<ConstraintType> AtomicConstraints(const ModelStorage& storage) {
58 using IdType = typename ConstraintType::IdType;
59 std::vector<ConstraintType> result;
60 result.reserve(storage.num_constraints<IdType>());
61 for (const IdType con_id : storage.Constraints<IdType>()) {
62 result.push_back(ConstraintType(&storage, con_id));
63 }
64 return result;
65}
66
67// * Duck-types on `ConstraintType` having a `typed_id()` getter.
68template <typename ConstraintType>
69std::vector<ConstraintType> SortedAtomicConstraints(
70 const ModelStorage& storage) {
71 std::vector<ConstraintType> constraints =
73 absl::c_sort(constraints,
74 [](const ConstraintType& l, const ConstraintType& r) {
75 return l.typed_id() < r.typed_id();
76 });
77 return constraints;
78}
79
80} // namespace operations_research::math_opt
81
82#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_UTIL_MODEL_UTIL_H_
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::vector< ConstraintType > AtomicConstraints(const ModelStorage &storage)
Definition model_util.h:57
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition model_util.h:30
std::vector< ConstraintType > SortedAtomicConstraints(const ModelStorage &storage)
Definition model_util.h:69
LinearExpression ToLinearExpression(const ModelStorage &storage, const LinearExpressionData &expr_data)
Definition model_util.cc:25
std::vector< Variable > AtomicConstraintNonzeroVariables(const ModelStorage &storage, const IdType id)
Definition model_util.h:42
LinearExpressionData FromLinearExpression(const LinearExpression &expression)
Converts a LinearExpression to the associated "raw ID" format.
Definition model_util.cc:34