Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
map_filter.cc
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
15
16#include <cstdint>
17#include <utility>
18
19#include "absl/container/flat_hash_set.h"
20#include "absl/status/statusor.h"
25
27
28absl::StatusOr<MapFilter<Variable>> VariableFilterFromProto(
29 const Model& model, const SparseVectorFilterProto& proto) {
30 MapFilter<Variable> result = {.skip_zero_values = proto.skip_zero_values()};
31 if (proto.filter_by_ids()) {
32 absl::flat_hash_set<Variable> filtered;
33 for (const int64_t id : proto.filtered_ids()) {
34 if (!model.has_variable(id)) {
36 << "cannot create MapFilter<Variable> from proto, variable id: "
37 << id << " not in model";
38 }
39 filtered.insert(model.variable(id));
40 }
41 result.filtered_keys = std::move(filtered);
42 }
43 return result;
44}
45
46absl::StatusOr<MapFilter<LinearConstraint>> LinearConstraintFilterFromProto(
47 const Model& model, const SparseVectorFilterProto& proto) {
48 MapFilter<LinearConstraint> result = {.skip_zero_values =
49 proto.skip_zero_values()};
50 if (proto.filter_by_ids()) {
51 absl::flat_hash_set<LinearConstraint> filtered;
52 for (const int64_t id : proto.filtered_ids()) {
53 if (!model.has_linear_constraint(id)) {
55 << "cannot create MapFilter<LinearConstraint> from proto, "
56 "linear constraint id: "
57 << id << " not in model";
58 }
59 filtered.insert(model.linear_constraint(id));
60 }
61 result.filtered_keys = std::move(filtered);
62 }
63 return result;
64}
65
66absl::StatusOr<MapFilter<QuadraticConstraint>>
68 const SparseVectorFilterProto& proto) {
69 MapFilter<QuadraticConstraint> result = {.skip_zero_values =
70 proto.skip_zero_values()};
71 if (proto.filter_by_ids()) {
72 absl::flat_hash_set<QuadraticConstraint> filtered;
73 for (const int64_t id : proto.filtered_ids()) {
74 if (!model.has_quadratic_constraint(id)) {
76 << "cannot create MapFilter<QuadraticConstraint> from proto, "
77 "quadratic constraint id: "
78 << id << " not in model";
79 }
80 filtered.insert(model.quadratic_constraint(id));
81 }
82 result.filtered_keys = std::move(filtered);
83 }
84 return result;
85}
86
87} // namespace operations_research::math_opt
CpModelProto proto
The output proto.
GRBmodel * model
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::StatusOr< MapFilter< LinearConstraint > > LinearConstraintFilterFromProto(const Model &model, const SparseVectorFilterProto &proto)
Definition map_filter.cc:46
absl::StatusOr< MapFilter< QuadraticConstraint > > QuadraticConstraintFilterFromProto(const Model &model, const SparseVectorFilterProto &proto)
Definition map_filter.cc:67
absl::StatusOr< MapFilter< Variable > > VariableFilterFromProto(const Model &model, const SparseVectorFilterProto &proto)
Definition map_filter.cc:28
StatusBuilder InvalidArgumentErrorBuilder()
std::optional< absl::flat_hash_set< KeyType > > filtered_keys
Definition map_filter.h:98